Class: Fluent::PluginHelper::HttpServer::Server
- Inherits:
-
Object
- Object
- Fluent::PluginHelper::HttpServer::Server
- Defined in:
- lib/fluent/plugin_helper/http_server/server.rb
Instance Method Summary collapse
-
#initialize(addr:, port:, logger:, default_app: nil, tls_context: nil) ⇒ Server
constructor
A new instance of Server.
- #start(notify = nil) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(addr:, port:, logger:, default_app: nil, tls_context: nil) ⇒ Server
Returns a new instance of Server.
33 34 35 36 37 38 39 40 41 42 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/fluent/plugin_helper/http_server/server.rb', line 33 def initialize(addr:, port:, logger:, default_app: nil, tls_context: nil) # Normalize the address: strip brackets if present # Brackets are only for URI formatting, not for socket binding @addr = if addr.start_with?('[') && addr.end_with?(']') addr[1..-2] # Remove surrounding brackets else addr end @port = port @logger = logger # TODO: support http2 scheme = tls_context ? 'https' : 'http' # Handle IPv6 addresses properly in URI construction per RFC 3986 # Add brackets to IPv6 addresses for URI compliance (ip-literal per RFC 3986) ip_literal = if @addr.include?(":") "[#{@addr}]" # IPv6 address - add brackets else @addr # IPv4 or hostname - use directly end @uri = URI("#{scheme}://#{ip_literal}:#{@port}").to_s @router = Router.new(default_app) @server_task_queue = ::Thread::Queue.new Console.logger = Fluent::Log::ConsoleAdapter.wrap(@logger) opts = if tls_context { ssl_context: tls_context } else {} end @server = Async::HTTP::Server.new(App.new(@router, @logger), Async::HTTP::Endpoint.parse(@uri, **opts)) if block_given? yield(self) end end |
Instance Method Details
#start(notify = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fluent/plugin_helper/http_server/server.rb', line 70 def start(notify = nil) Console.logger = Fluent::Log::ConsoleAdapter.wrap(@logger) @logger.debug("Start async HTTP server listening #{@uri}") Async do |task| Console.logger = Fluent::Log::ConsoleAdapter.wrap(@logger) begin @server.endpoint.accept(&@server.method(:accept)) if notify notify.push(:ready) end @server_task_queue.pop rescue => e @logger.error("Failed to run HTTP server listening #{@uri}: #{e.class}: #{e.}") raise unless notify notify.push(e) ensure task.children&.each(&:stop) end end @logger.debug('Finished HTTP server') end |
#stop ⇒ Object
95 96 97 98 |
# File 'lib/fluent/plugin_helper/http_server/server.rb', line 95 def stop @logger.debug('closing HTTP server') @server_task_queue.push(:stop) end |