Class: Riser::SocketServer

Inherits:
Object
  • Object
show all
Defined in:
lib/riser/server.rb

Constant Summary collapse

NO_CALL =

:nodoc:

proc{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSocketServer

Returns a new instance of SocketServer.



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/riser/server.rb', line 609

def initialize
  @accept_polling_timeout_seconds = 0.1
  @process_num = 0
  @process_queue_size = 20
  @process_queue_polling_timeout_seconds = 0.1
  @process_send_io_polling_timeout_seconds = 0.1
  @thread_num = 4
  @thread_queue_size = 20
  @thread_queue_polling_timeout_seconds = 0.1
  @before_start = NO_CALL
  @at_fork = NO_CALL
  @at_stop = NO_CALL
  @at_stat = NO_CALL
  @preprocess = NO_CALL
  @postprocess = NO_CALL
  @after_stop = NO_CALL
  @dispatch = nil
  @dispatcher = nil
end

Instance Attribute Details

#accept_polling_timeout_secondsObject

Returns the value of attribute accept_polling_timeout_seconds.



629
630
631
# File 'lib/riser/server.rb', line 629

def accept_polling_timeout_seconds
  @accept_polling_timeout_seconds
end

#process_numObject

Returns the value of attribute process_num.



630
631
632
# File 'lib/riser/server.rb', line 630

def process_num
  @process_num
end

#process_queue_polling_timeout_secondsObject

Returns the value of attribute process_queue_polling_timeout_seconds.



632
633
634
# File 'lib/riser/server.rb', line 632

def process_queue_polling_timeout_seconds
  @process_queue_polling_timeout_seconds
end

#process_queue_sizeObject

Returns the value of attribute process_queue_size.



631
632
633
# File 'lib/riser/server.rb', line 631

def process_queue_size
  @process_queue_size
end

#process_send_io_polling_timeout_secondsObject

Returns the value of attribute process_send_io_polling_timeout_seconds.



633
634
635
# File 'lib/riser/server.rb', line 633

def process_send_io_polling_timeout_seconds
  @process_send_io_polling_timeout_seconds
end

#thread_numObject

Returns the value of attribute thread_num.



634
635
636
# File 'lib/riser/server.rb', line 634

def thread_num
  @thread_num
end

#thread_queue_polling_timeout_secondsObject

Returns the value of attribute thread_queue_polling_timeout_seconds.



636
637
638
# File 'lib/riser/server.rb', line 636

def thread_queue_polling_timeout_seconds
  @thread_queue_polling_timeout_seconds
end

#thread_queue_sizeObject

Returns the value of attribute thread_queue_size.



635
636
637
# File 'lib/riser/server.rb', line 635

def thread_queue_size
  @thread_queue_size
end

Instance Method Details

#after_stop(&block) ⇒ Object

:yields:



668
669
670
671
# File 'lib/riser/server.rb', line 668

def after_stop(&block)      # :yields:
  @after_stop = block
  nil
end

#at_fork(&block) ⇒ Object

:yields:



643
644
645
646
# File 'lib/riser/server.rb', line 643

def at_fork(&block)         # :yields:
  @at_fork = block
  nil
end

#at_stat(&block) ⇒ Object

:yields: stat_info



653
654
655
656
# File 'lib/riser/server.rb', line 653

def at_stat(&block)         # :yields: stat_info
  @at_stat = block
  nil
end

#at_stop(&block) ⇒ Object

:yields: stop_state



648
649
650
651
# File 'lib/riser/server.rb', line 648

def at_stop(&block)         # :yields: stop_state
  @at_stop = block
  nil
end

#before_start(&block) ⇒ Object

:yields: server_socket



638
639
640
641
# File 'lib/riser/server.rb', line 638

def before_start(&block)    # :yields: server_socket
  @before_start = block
  nil
end

#dispatch(&block) ⇒ Object

:yields: socket



673
674
675
676
# File 'lib/riser/server.rb', line 673

def dispatch(&block)        # :yields: socket
  @dispatch = block
  nil
end

#postprocess(&block) ⇒ Object

:yields:



663
664
665
666
# File 'lib/riser/server.rb', line 663

def postprocess(&block)     # :yields:
  @postprocess = block
  nil
end

#preprocess(&block) ⇒ Object

:yields:



658
659
660
661
# File 'lib/riser/server.rb', line 658

def preprocess(&block)      # :yields:
  @preprocess = block
  nil
end

#setup(server_socket) ⇒ Object

after this method call is completed, the object will be ready to accept ‘signal_…’ methods.



704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/riser/server.rb', line 704

def setup(server_socket)
  if (@process_num > 0) then
    @dispatcher = SocketProcessDispatcher.new('process_queue', 'thread_queue')
    @dispatcher.accept_polling_timeout_seconds = @accept_polling_timeout_seconds
    @dispatcher.process_num = @process_num
    @dispatcher.process_queue_size = @process_queue_size
    @dispatcher.process_queue_polling_timeout_seconds = @process_queue_polling_timeout_seconds
    @dispatcher.process_send_io_polling_timeout_seconds = @process_send_io_polling_timeout_seconds
    @dispatcher.thread_num = @thread_num
    @dispatcher.thread_queue_size = @thread_queue_size
    @dispatcher.thread_queue_polling_timeout_seconds = @thread_queue_polling_timeout_seconds

    @dispatcher.at_fork{
      server_socket.close
      @at_fork.call
    }
    @dispatcher.at_stop(&@at_stop)
    @dispatcher.at_stat(&@at_stat)
    @dispatcher.preprocess(&@preprocess)
    @dispatcher.postprocess(&@postprocess)
    @dispatcher.dispatch(&@dispatch)
    @dispatcher.setup
  else
    @dispatcher = SocketThreadDispatcher.new('thread_queue')
    @dispatcher.thread_num = @thread_num
    @dispatcher.thread_queue_size = @thread_queue_size
    @dispatcher.thread_queue_polling_timeout_seconds = @thread_queue_polling_timeout_seconds

    @dispatcher.at_stop(&@at_stop)
    @dispatcher.at_stat(&@at_stat)
    @dispatcher.at_stat_get(&NO_CALL)
    @dispatcher.at_stat_stop(&NO_CALL)
    @dispatcher.preprocess(&@preprocess)
    @dispatcher.postprocess(&@postprocess)
    @dispatcher.accept{
      if (server_socket.wait_readable(@accept_polling_timeout_seconds) != nil) then
        begin
          server_socket.accept_nonblock
        rescue IO::WaitReadable
          nil               # to avoid conflicting accept(2) at server restart overlap
        end
      end
    }
    @dispatcher.accept_return(&NO_CALL)
    @dispatcher.dispatch(&@dispatch)
  end

  nil
end

#signal_stat_get(reset: true) ⇒ Object

should be called from signal(2) handler



691
692
693
694
# File 'lib/riser/server.rb', line 691

def signal_stat_get(reset: true)
  @dispatcher.signal_stat_get(reset: reset) if @dispatcher
  nil
end

#signal_stat_stopObject

should be called from signal(2) handler



697
698
699
700
# File 'lib/riser/server.rb', line 697

def signal_stat_stop
  @dispatcher.signal_stat_stop if @dispatcher
  nil
end

#signal_stop_forcedObject

should be called from signal(2) handler



685
686
687
688
# File 'lib/riser/server.rb', line 685

def signal_stop_forced
  @dispatcher.signal_stop_forced if @dispatcher
  nil
end

#signal_stop_gracefulObject

should be called from signal(2) handler



679
680
681
682
# File 'lib/riser/server.rb', line 679

def signal_stop_graceful
  @dispatcher.signal_stop_graceful if @dispatcher
  nil
end

#start(server_socket) ⇒ Object

should be executed on the main thread sharing the stack with signal(2) handlers



756
757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/riser/server.rb', line 756

def start(server_socket)
  unless (@dispatcher) then
    setup(server_socket)
  end

  @before_start.call(server_socket)
  begin
    @dispatcher.start(server_socket)
  ensure
    @after_stop.call
  end

  nil
end