Class: Wsv::Server::ConnectionThrottle
- Inherits:
-
Object
- Object
- Wsv::Server::ConnectionThrottle
- Defined in:
- lib/wsv/server/connection_throttle.rb
Overview
Caps in-flight connections at ‘max`. `try_spawn` runs the block in a new thread when capacity is available and returns true; otherwise returns false so the caller can reject the client.
Instance Method Summary collapse
-
#initialize(max:, err:) ⇒ ConnectionThrottle
constructor
A new instance of ConnectionThrottle.
- #try_spawn(&block) ⇒ Object
Constructor Details
#initialize(max:, err:) ⇒ ConnectionThrottle
Returns a new instance of ConnectionThrottle.
9 10 11 12 13 14 |
# File 'lib/wsv/server/connection_throttle.rb', line 9 def initialize(max:, err:) @max = max @err = err @mutex = Mutex.new @active = 0 end |
Instance Method Details
#try_spawn(&block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/wsv/server/connection_throttle.rb', line 16 def try_spawn(&block) return false unless reserve_slot begin Thread.new do Thread.current.report_on_exception = false block.call ensure release_slot end true rescue ThreadError => e @err.puts "wsv: thread error: #{e.}" release_slot false end end |