Class: Upfluence::HTTP::Server

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

Constant Summary collapse

REQUEST_CONTEXT_KEY =
:uhttp_request_context
SHUTDOWN_SIGNALS =
%w[INT TERM].freeze
DEFAULT_MIDDLEWARES =
[]
DEFAULT_OPTIONS =
{
  server:                          :puma,
  Port:                            ENV.fetch('PORT', 8080),
  Host:                            '0.0.0.0',
  threaded:                        true,
  interfaces:                      [],
  push_gateway_url:                ENV.fetch('PUSH_GATEWAY_URL', nil),
  push_gateway_interval:           15, # sec
  prometheus_endpoint:             ENV.fetch('PUSH_GATEWAY_URL', nil).eql?(nil),
  app_name:                        ENV.fetch('APP_NAME', 'uhttp-rb-server'),
  unit_name:                       ENV.fetch('UNIT_NAME', 'uhttp-rb-server-anonymous'),
  base_processor_klass:            nil,
  base_handler_klass:              nil,
  max_threads:                     ENV.fetch('HTTP_SERVER_MAX_THREADS', 5).to_i,
  request_timeout:                 ENV['HTTP_SERVER_REQUEST_TIMEOUT']&.to_i,
  enable_active_record_middleware: true,
  middlewares:                     [],
  instrumentations:                [
    Instrumentation::PumaInstrumenter.new,
    Instrumentation::GCInstrumenter.new,
    Instrumentation::ActiveRecordPoolInstrumenter.new
  ],
  admin_port:                      ENV.fetch('ADMIN_PORT', nil),
  debug:                           ENV.fetch('DEBUG', nil)
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Server

Returns a new instance of Server.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/upfluence/http/server.rb', line 57

def initialize(options = {}, &block)
  @options = DEFAULT_OPTIONS.dup.merge(options)
  opts = @options

  @base_handler = opts[:base_handler_klass].new(opts[:interfaces]) if opts[:base_handler_klass]

  @admin_builder = admin_builder(opts) if opts[:admin_port]
  @production_builder = production_builder(opts, &block)
  @handler = Rack::Handler.get(opts[:server])
  @stoppables = []
end

Class Method Details

.requestObject



102
103
104
# File 'lib/upfluence/http/server.rb', line 102

def request
  Thread.current[REQUEST_CONTEXT_KEY]
end

.request=(req) ⇒ Object



106
107
108
# File 'lib/upfluence/http/server.rb', line 106

def request=(req)
  Thread.current[REQUEST_CONTEXT_KEY] = req
end

Instance Method Details

#serveObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/upfluence/http/server.rb', line 69

def serve
  ENV['RACK_ENV'] = Upfluence.env.to_s
  @stopping = false
  signal_handlers = install_signal_handlers
  admin_thread = nil

  register_stoppable(prometheus_exporter_thread) if @options[:push_gateway_url]

  @options[:instrumentations].each do |instrumentation|
    instrumentation.start
    register_stoppable(instrumentation) if instrumentation.respond_to?(:stop)
  end

  if @admin_builder
    admin_port = @options[:admin_port].to_i

    admin_thread = Thread.new do
      run_builder(@admin_builder, Port: admin_port)
    end

    Upfluence.logger.info(
      "Admin server listening on #{@options[:Host]}:#{admin_port}"
    )
  end

  run_builder(@production_builder)
ensure
  stop
  admin_thread&.join
  restore_signal_handlers(signal_handlers) if signal_handlers
end