class Protein::Server

Public Class Methods

config(config = nil) click to toggle source
# File lib/protein/server.rb, line 17
def config(config = nil)
  @config = (@config || {}).merge(config) if config
  @config || {}
end
route(router) click to toggle source
# File lib/protein/server.rb, line 4
def route(router)
  @router = Router.define(router)
end
router() click to toggle source
# File lib/protein/server.rb, line 13
def router
  GetConst.call(@router)
end
service(service) click to toggle source
# File lib/protein/server.rb, line 8
def service(service)
  @router ||= Class.new(Router)
  @router.service(service)
end
start() click to toggle source
# File lib/protein/server.rb, line 31
def start
  worker_count = config.fetch(:concurrency, 5)
  on_worker_boot = config[:on_worker_boot]

  pids = (1..worker_count).map do |i|
    fork do
      Protein.logger.info "Starting server #{i}/#{worker_count} with PID #{Process.pid}"
      on_worker_boot.call if on_worker_boot.respond_to?(:call)
      transport_class.serve(router)
    end
  end

  Signal.trap('TERM') do
    pids.each { |pid| Process.kill(:TERM, pid) }
    pids.each { |pid| Process.wait(pid) }
  end

  Signal.trap('INT') do
    pids.each { |pid| Process.kill(:INT, pid) }
    pids.each { |pid| Process.wait(pid) }
  end

  Process.wait
end
transport(transport, opts = {}) click to toggle source
# File lib/protein/server.rb, line 22
def transport(transport, opts = {})
  @transport_class = Transport.define(transport, opts)
end
transport_class() click to toggle source
# File lib/protein/server.rb, line 26
def transport_class
  GetConst.call(@transport_class)
end