Class: Maze::TerminatingServer

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/terminating_server.rb

Overview

Receives and terminates network connections without reading any data

Constant Summary collapse

CONTINUE_RESPONSE =
"HTTP/1.1 100 CONTINUE\n\r"
BAD_REQUEST_RESPONSE =
"HTTP/1.1 400 BAD REQUEST\n\r"

Class Method Summary collapse

Class Method Details

.max_received_sizeInteger

The maximum string length to be received before disconnecting

Returns:

  • (Integer)

    The string length, defaults to 1MB



56
57
58
# File 'lib/maze/terminating_server.rb', line 56

def max_received_size
  @max_received_size ||= 1048576
end

.max_received_size=(new_max_size) ⇒ Object

Set the maximum string length to be received before disconnecting

Parameters:

  • new_max_size (Integer)

    The new maximum size



63
64
65
# File 'lib/maze/terminating_server.rb', line 63

def max_received_size=(new_max_size)
  @max_received_size = new_max_size
end

.received_request_countObject

Outputs the amount of times the server has received a connection on the last run



95
96
97
# File 'lib/maze/terminating_server.rb', line 95

def received_request_count
  @received_requests ||= 0
end

.reset_elementsObject

Resets the response string to “400/BAD REQUEST” and the read size to 1MB



82
83
84
85
# File 'lib/maze/terminating_server.rb', line 82

def reset_elements
  @response = BAD_REQUEST_RESPONSE
  @max_received_size = 1048576
end

.responseString

The response string sent to a connected client

Returns:

  • (String)

    The response string, defaults to “400/BAD REQUEST”



70
71
72
# File 'lib/maze/terminating_server.rb', line 70

def response
  @response ||= BAD_REQUEST_RESPONSE
end

.response=(new_response) ⇒ Object

Set the response string to an arbitrary value

Parameters:

  • new_response (String)

    The new response



77
78
79
# File 'lib/maze/terminating_server.rb', line 77

def response=(new_response)
  @response = new_response
end

.running?Boolean

Whether the server thread is running

Returns:

  • (Boolean)

    If the server is running



90
91
92
# File 'lib/maze/terminating_server.rb', line 90

def running?
  @thread&.alive?
end

.startObject

Starts the socket accept loop in a separate thread



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/maze/terminating_server.rb', line 15

def start
  # Only run a single server thread
  return if running?

  attempts = 0
  loop do

    @thread = Thread.new do
      # Reset the received count
      @received_requests = 0

      Socket.tcp_server_loop(Maze.config.null_port) {|socket, _client_addrinfo|
        $logger.info 'Terminating server received request'
        @received_requests += 1
        headers = receive_headers(socket)

        body_length = headers['Content-Length']
        receive_data(socket, body_length) unless body_length.nil?

        end_connection(socket)
      }
    rescue StandardError => e
      Bugsnag.notify e
      $logger.warn "Terminating server error: #{e.message}"
    end

    break if running?

    # Bail out after 3 attempts
    attempts += 1
    raise 'Too many failed attempts to start terminating server' if attempts == 3

    # Failed to start - sleep before retrying
    $logger.info 'Retrying in 3 seconds'
    sleep 1
  end
end

.stopObject

Stops the socket accept loop if alive



100
101
102
103
# File 'lib/maze/terminating_server.rb', line 100

def stop
  @thread&.kill if @thread&.alive?
  @thread = nil
end