Class: Orchestrator::Device::TcpConnection
- Inherits:
-
UV::OutboundConnection
- Object
- UV::OutboundConnection
- Orchestrator::Device::TcpConnection
- Defined in:
- lib/orchestrator/device/transport_tcp.rb
Instance Method Summary collapse
- #disconnect ⇒ Object
- #on_close ⇒ Object
- #on_connect(transport) ⇒ Object
- #on_read(data, *args) ⇒ Object
- #post_init(manager, processor, tls) ⇒ Object
- #terminate ⇒ Object
- #transmit(cmd) ⇒ Object
Instance Method Details
#disconnect ⇒ Object
117 118 119 120 |
# File 'lib/orchestrator/device/transport_tcp.rb', line 117 def disconnect # Shutdown quickly close_connection end |
#on_close ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/orchestrator/device/transport_tcp.rb', line 54 def on_close unless @terminated # Clear the connection delay if in use @delaying = false if @delaying @retries += 1 the_time = @processor.thread.now boundry = @last_retry + @config[:thrashing_threshold] # ensure we are not thrashing (rapid connect then disconnect) # This equals a disconnect and requires a warning if @retries == 1 && boundry >= the_time @retries += 1 @processor.disconnected @manager.logger.warn('possible connection thrashing. Disconnecting') end if @retries == 1 @last_retry = the_time @processor.disconnected reconnect else variation = 1 + rand(2000) @connecting = @manager.get_scheduler.in(3000 + variation) do @connecting = nil reconnect end if @retries == 2 # NOTE:: edge case if disconnected on first connect @processor.disconnected if @last_retry == 0 # we mark the queue as offline if more than 1 reconnect fails @processor.queue.offline(@config[:clear_queue_on_disconnect]) end end end end |
#on_connect(transport) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/orchestrator/device/transport_tcp.rb', line 36 def on_connect(transport) if @terminated close_connection(:after_writing) else begin use_tls(@config) if @tls rescue => e @manager.logger.print_error(e, 'error starting tls') end if @config[:wait_ready] @delaying = '' else init_connection end end end |
#on_read(data, *args) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/orchestrator/device/transport_tcp.rb', line 92 def on_read(data, *args) if @delaying # Update last retry so we don't trigger multiple # calls to disconnected as connection is working @last_retry += 1 @delaying << data result = @delaying.split(@config[:wait_ready], 2) if result.length > 1 @delaying = false init_connection rem = result[-1] @processor.buffer(rem) unless rem.empty? end else @processor.buffer(data) end end |
#post_init(manager, processor, tls) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/orchestrator/device/transport_tcp.rb', line 5 def post_init(manager, processor, tls) @manager = manager @processor = processor @config = @processor.config @tls = tls # Delay retry by default if connection fails on load @retries = 1 # Connection retries @connecting = nil # Connection timer # Last retry shouldn't break any thresholds @last_retry = 0 end |
#terminate ⇒ Object
111 112 113 114 115 |
# File 'lib/orchestrator/device/transport_tcp.rb', line 111 def terminate @terminated = true @connecting.cancel if @connecting close_connection(:after_writing) if @transport.connected end |
#transmit(cmd) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/orchestrator/device/transport_tcp.rb', line 19 def transmit(cmd) return if @terminated promise = write(cmd[:data]) if cmd[:wait] promise.catch do |err| if @processor.queue.waiting == cmd # Fail fast @processor.thread.next_tick do @processor.__send__(:resp_failure, err) end else cmd[:defer].reject(err) end end end end |