Class | Thrift::ThreadPoolServer |
In: |
lib/thrift/server/thread_pool_server.rb
lib/thrift/server/thread_pool_server.rb |
Parent: | BaseServer |
# File lib/thrift/server/thread_pool_server.rb, line 24 24: def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil, num=20) 25: super(processor, server_transport, transport_factory, protocol_factory) 26: @thread_q = SizedQueue.new(num) 27: @exception_q = Queue.new 28: @running = false 29: end
# File lib/thrift/server/thread_pool_server.rb, line 24 24: def initialize(processor, server_transport, transport_factory=nil, protocol_factory=nil, num=20) 25: super(processor, server_transport, transport_factory, protocol_factory) 26: @thread_q = SizedQueue.new(num) 27: @exception_q = Queue.new 28: @running = false 29: end
exceptions that happen in worker threads will be relayed here and must be caught. ‘retry’ can be used to continue. (threads will continue to run while the exception is being handled.)
# File lib/thrift/server/thread_pool_server.rb, line 34 34: def rescuable_serve 35: Thread.new { serve } unless @running 36: @running = true 37: raise @exception_q.pop 38: end
exceptions that happen in worker threads will be relayed here and must be caught. ‘retry’ can be used to continue. (threads will continue to run while the exception is being handled.)
# File lib/thrift/server/thread_pool_server.rb, line 34 34: def rescuable_serve 35: Thread.new { serve } unless @running 36: @running = true 37: raise @exception_q.pop 38: end
exceptions that happen in worker threads simply cause that thread to die and another to be spawned in its place.
# File lib/thrift/server/thread_pool_server.rb, line 42 42: def serve 43: @server_transport.listen 44: 45: begin 46: loop do 47: @thread_q.push(:token) 48: Thread.new do 49: begin 50: loop do 51: client = @server_transport.accept 52: trans = @transport_factory.get_transport(client) 53: prot = @protocol_factory.get_protocol(trans) 54: begin 55: loop do 56: @processor.process(prot, prot) 57: end 58: rescue Thrift::TransportException, Thrift::ProtocolException => e 59: ensure 60: trans.close 61: end 62: end 63: rescue => e 64: @exception_q.push(e) 65: ensure 66: @thread_q.pop # thread died! 67: end 68: end 69: end 70: ensure 71: @server_transport.close 72: end 73: end
exceptions that happen in worker threads simply cause that thread to die and another to be spawned in its place.
# File lib/thrift/server/thread_pool_server.rb, line 42 42: def serve 43: @server_transport.listen 44: 45: begin 46: loop do 47: @thread_q.push(:token) 48: Thread.new do 49: begin 50: loop do 51: client = @server_transport.accept 52: trans = @transport_factory.get_transport(client) 53: prot = @protocol_factory.get_protocol(trans) 54: begin 55: loop do 56: @processor.process(prot, prot) 57: end 58: rescue Thrift::TransportException, Thrift::ProtocolException => e 59: ensure 60: trans.close 61: end 62: end 63: rescue => e 64: @exception_q.push(e) 65: ensure 66: @thread_q.pop # thread died! 67: end 68: end 69: end 70: ensure 71: @server_transport.close 72: end 73: end