class Cassandra::Promise::Signal
@private
Public Class Methods
new(executor)
click to toggle source
# File lib/cassandra/future.rb 513 def initialize(executor) 514 mon_initialize 515 516 @cond = new_cond 517 @executor = executor 518 @state = :pending 519 @waiting = 0 520 @error = nil 521 @value = nil 522 @listeners = [] 523 end
Public Instance Methods
add_listener(listener)
click to toggle source
# File lib/cassandra/future.rb 644 def add_listener(listener) 645 if @state == :pending 646 synchronize do 647 if @state == :pending 648 @listeners << listener 649 650 return self 651 end 652 end 653 end 654 655 begin 656 listener.success(@value) 657 rescue 658 nil 659 end if @state == :fulfilled 660 begin 661 listener.failure(@error) 662 rescue 663 nil 664 end if @state == :broken 665 666 self 667 end
failure(error)
click to toggle source
# File lib/cassandra/future.rb 525 def failure(error) 526 raise ::ArgumentError, "error must be an exception, #{error.inspect} given" unless error.is_a?(::Exception) 527 528 return unless @state == :pending 529 530 listeners = nil 531 532 synchronize do 533 return unless @state == :pending 534 535 @error = error 536 @state = :broken 537 538 listeners = @listeners 539 @listeners = nil 540 end 541 542 @executor.execute do 543 listeners.each do |listener| 544 begin 545 listener.failure(error) 546 rescue 547 nil 548 end 549 end 550 551 synchronize do 552 @cond.broadcast if @waiting > 0 553 end 554 end 555 556 self 557 end
fallback() { |error| ... }
click to toggle source
# File lib/cassandra/future.rb 749 def fallback(&block) 750 if @state == :pending 751 synchronize do 752 if @state == :pending 753 promise = Promise.new(@executor) 754 listener = Listeners::Fallback.new(promise, &block) 755 @listeners << listener 756 return promise.future 757 end 758 end 759 end 760 761 return Future::Value.new(@value) if @state == :fulfilled 762 763 begin 764 result = yield(@error) 765 result = Future::Value.new(result) unless result.is_a?(Future) 766 result 767 rescue => e 768 Future::Error.new(e) 769 end 770 end
get(timeout = nil)
click to toggle source
@param timeout [nil, Numeric] a maximum number of seconds to block
current thread for while waiting for this future to resolve. Will wait indefinitely if passed `nil`.
@raise [ArgumentError] raised when a negative timeout is given @raise [Errors::TimeoutError] raised when wait time exceeds the timeout @raise [Exception] raises when the future has been resolved with an
error. The original exception will be raised.
@return [Object] the value that the future has been resolved with
# File lib/cassandra/future.rb 601 def get(timeout = nil) 602 timeout &&= Float(timeout) 603 604 if timeout 605 raise ::ArgumentError, "timeout cannot be negative, #{timeout.inspect} given" if timeout < 0 606 607 start = ::Time.now 608 now = start 609 deadline = start + timeout 610 end 611 612 if @state == :pending 613 synchronize do 614 if @state == :pending 615 @waiting += 1 616 while @state == :pending 617 if deadline 618 @cond.wait(deadline - now) 619 now = ::Time.now 620 break if now >= deadline 621 else 622 @cond.wait 623 end 624 end 625 @waiting -= 1 626 end 627 end 628 629 if @state == :pending 630 total_wait = deadline - start 631 raise Errors::TimeoutError, 632 "Future did not complete within #{timeout.inspect} seconds. " \ 633 "Wait time: #{total_wait.inspect}" 634 end 635 end 636 637 raise(@error, @error.message, @error.backtrace) if @state == :broken 638 639 @value 640 end
Also aliased as: join
on_complete() { |value, error| ... }
click to toggle source
# File lib/cassandra/future.rb 707 def on_complete(&block) 708 if @state == :pending 709 synchronize do 710 if @state == :pending 711 @listeners << Listeners::Complete.new(&block) 712 return self 713 end 714 end 715 end 716 717 begin 718 yield(@value, @error) 719 rescue 720 nil 721 end 722 723 self 724 end
on_failure() { |error| ... }
click to toggle source
# File lib/cassandra/future.rb 688 def on_failure(&block) 689 if @state == :pending 690 synchronize do 691 if @state == :pending 692 @listeners << Listeners::Failure.new(&block) 693 return self 694 end 695 end 696 end 697 698 begin 699 yield(@error) 700 rescue 701 nil 702 end if @state == :broken 703 704 self 705 end
on_success() { |value| ... }
click to toggle source
# File lib/cassandra/future.rb 669 def on_success(&block) 670 if @state == :pending 671 synchronize do 672 if @state == :pending 673 @listeners << Listeners::Success.new(&block) 674 return self 675 end 676 end 677 end 678 679 begin 680 yield(@value) 681 rescue 682 nil 683 end if @state == :fulfilled 684 685 self 686 end
success(value)
click to toggle source
# File lib/cassandra/future.rb 559 def success(value) 560 return unless @state == :pending 561 562 listeners = nil 563 564 synchronize do 565 return unless @state == :pending 566 567 @value = value 568 @state = :fulfilled 569 570 listeners = @listeners 571 @listeners = nil 572 end 573 574 @executor.execute do 575 listeners.each do |listener| 576 begin 577 listener.success(value) 578 rescue 579 nil 580 end 581 end 582 583 synchronize do 584 @cond.broadcast if @waiting > 0 585 end 586 end 587 588 self 589 end
then() { |value| ... }
click to toggle source
# File lib/cassandra/future.rb 726 def then(&block) 727 if @state == :pending 728 synchronize do 729 if @state == :pending 730 promise = Promise.new(@executor) 731 listener = Listeners::Then.new(promise, &block) 732 @listeners << listener 733 return promise.future 734 end 735 end 736 end 737 738 return Future::Error.new(@error) if @state == :broken 739 740 begin 741 result = yield(@value) 742 result = Future::Value.new(result) unless result.is_a?(Future) 743 result 744 rescue => e 745 Future::Error.new(e) 746 end 747 end