Class: Wurk::Health::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/health.rb

Overview

The HTTP listener. Owns one TCPServer + one accept thread. Idempotent start/stop; safe to call from Launcher#run / Launcher#stop.

Constant Summary collapse

ACCEPT_TIMEOUT =
0.2
RETRY_INTERVAL =

How often a non-owner child re-attempts the shared port. Short enough that probes come back quickly after the owner dies, long enough not to spin. See #start_retry_loop.

5
REQUEST_TIMEOUT =

Overall budget for one request head. A kubelet probe sends its head in one segment; anything slower is broken or hostile.

1.0
MAX_REQUEST_BYTES =
8192
MAX_CONNECTIONS =
16
REASONS =
{ 200 => 'OK', 404 => 'Not Found', 405 => 'Method Not Allowed',
503 => 'Service Unavailable' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(launcher, port: DEFAULT_PORT, bind: DEFAULT_BIND, ready_window: DEFAULT_READY_WINDOW, retry_interval: RETRY_INTERVAL) ⇒ Server

Returns a new instance of Server.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wurk/health.rb', line 88

def initialize(launcher, port: DEFAULT_PORT, bind: DEFAULT_BIND,
               ready_window: DEFAULT_READY_WINDOW, retry_interval: RETRY_INTERVAL)
  @launcher       = launcher
  @config         = launcher.instance_variable_get(:@config)
  @port           = port
  @bind           = bind
  @ready_window   = ready_window
  @retry_interval = retry_interval
  @server         = nil
  @thread         = nil
  @retry_thread   = nil
  @done           = false
  @slots          = ::Mutex.new
  @connections    = 0
  @fleet          = nil
end

Instance Attribute Details

#bind ⇒ Object (readonly)

Returns the value of attribute bind.



86
87
88
# File 'lib/wurk/health.rb', line 86

def bind
  @bind
end

#port ⇒ Object (readonly)

Returns the value of attribute port.



86
87
88
# File 'lib/wurk/health.rb', line 86

def port
  @port
end

Instance Method Details

#retrying? ⇒ Boolean

True while a non-owner child is still polling to take the shared port over (see #start_retry_loop). Distinct from #running?, which reports the accept thread specifically.

Returns:

  • (Boolean)


133
134
135
# File 'lib/wurk/health.rb', line 133

def retrying?
  @retry_thread&.alive? == true
end

#running? ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/wurk/health.rb', line 126

def running?
  @thread&.alive? == true
end

#start ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/wurk/health.rb', line 105

def start
  # Idempotent (see class doc): a second start on a live instance would
  # re-bind the same port, hit EADDRINUSE, and null out @server/@thread —
  # leaking the original listener so stop could never close it.
  return self if running? || retrying?

  @done = false
  bind_and_serve || start_retry_loop
  self
end

#stop ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/wurk/health.rb', line 116

def stop
  @done = true
  stop_retry_loop
  srv = @server
  @server = nil
  srv&.close
  @thread&.join(2)
  @thread = nil
end