Class: RightSpeed::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/right_speed/server.rb

Constant Summary collapse

DEFAULT_HOST =
"127.0.0.1"
DEFAULT_PORT =
8080
DEFAULT_WORKER_TYPE =
:roundrobin
DEFAULT_WORKERS =
Env.processors
AVAILABLE_WORKER_TYPES =
[:roundrobin, :fair, :accept]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app:, host: DEFAULT_HOST, port: DEFAULT_PORT, workers: DEFAULT_WORKERS, worker_type: DEFAULT_WORKER_TYPE, backlog: nil) ⇒ Server

Returns a new instance of Server.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/right_speed/server.rb', line 25

def initialize(
      app:,
      host: DEFAULT_HOST,
      port: DEFAULT_PORT,
      workers: DEFAULT_WORKERS,
      worker_type: DEFAULT_WORKER_TYPE,
      backlog: nil
    )
  @host = host
  @port = port
  @app = app
  @workers = workers
  @worker_type = worker_type
  @backlog = backlog
  @config_hooks = []
  @logger = nil
end

Instance Attribute Details

#config_hooksObject (readonly)

Returns the value of attribute config_hooks.



23
24
25
# File 'lib/right_speed/server.rb', line 23

def config_hooks
  @config_hooks
end

Instance Method Details

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/right_speed/server.rb', line 43

def run
  logger = RightSpeed.logger
  logger.info { "Start running with #{@workers} workers" }

  hooks = @config_hooks + (Ractor.current[RightSpeed::CONFIG_HOOK_KEY] || [])
  hooks.each do |hook|
    if hook.respond_to?(:call)
      hook.call
    end
  end

  RactorHelper.uri_hook
  RactorHelper.rack_hook

  begin
    processor = Processor.setup(app: @app, worker_type: @worker_type, workers: @workers)
    listener = Listener.setup(worker_type: @worker_type, host: @host, port: @port, backlog: nil)
    processor.configure(listener: listener)
    processor.run
    listener.wait
    processor.wait
  ensure
    listener.stop rescue nil
    processor.stop rescue nil
  end
end