Class: ThreadServer
- Inherits:
-
Object
- Object
- ThreadServer
- Includes:
- ServerBase
- Defined in:
- lib/macaw_framework/core/thread_server.rb
Overview
Class responsible for providing a default webserver with Ruby Threads. This Server is subject to the MRI Global Interpreter Lock, thus it will use only a single physical Thread.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
-
#initialize(macaw, endpoints_to_cache = nil, cache = nil) ⇒ Server
constructor
Create a new instance of ThreadServer.
-
#run ⇒ Object
Start running the webserver.
-
#shutdown ⇒ Object
Method Responsible for closing the TCP server.
Methods included from CacheAspect
Methods included from LoggingAspect
Constructor Details
#initialize(macaw, endpoints_to_cache = nil, cache = nil) ⇒ Server
Create a new instance of ThreadServer.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/macaw_framework/core/thread_server.rb', line 22 def initialize(macaw, endpoints_to_cache = nil, cache = nil) @port = macaw.port @bind = macaw.bind @macaw = macaw @macaw_log = macaw.macaw_log @num_threads = macaw.threads @work_queue = Queue.new set_features @cache = { cache: cache, endpoints_to_cache: endpoints_to_cache || [], cached_methods: macaw.cached_methods } @workers = [] end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
14 15 16 |
# File 'lib/macaw_framework/core/thread_server.rb', line 14 def context @context end |
Instance Method Details
#run ⇒ Object
Start running the webserver.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/macaw_framework/core/thread_server.rb', line 40 def run @server = TCPServer.new(@bind, @port) @server = OpenSSL::SSL::SSLServer.new(@server, @context) if @context @workers_mutex = Mutex.new @num_threads.times do spawn_worker end Thread.new do loop do sleep 10 maintain_worker_pool end end loop do @work_queue << @server.accept unless @is_shutting_down rescue OpenSSL::SSL::SSLError => e @macaw_log&.error("SSL error: #{e.message}") rescue IOError, Errno::EBADF break end end |
#shutdown ⇒ Object
Method Responsible for closing the TCP server.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/macaw_framework/core/thread_server.rb', line 66 def shutdown @is_shutting_down = true loop do break if @work_queue.empty? sleep 0.1 end worker_count = @workers_mutex.synchronize { @workers.size } worker_count.times { @work_queue << :shutdown } @workers.each(&:join) @server&.close end |