Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_server: Ready to support Async 2.0 gem #3842

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/fluent/plugin_helper/http_server/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def initialize(addr:, port:, logger:, default_app: nil, tls_context: nil)
scheme = tls_context ? 'https' : 'http'
@uri = URI("#{scheme}://#{@addr}:#{@port}").to_s
@router = Router.new(default_app)
@reactor = Async::Reactor.new(nil, logger: Fluent::Log::ConsoleAdapter.wrap(@logger))
@server_task = nil
Console.logger = Fluent::Log::ConsoleAdapter.wrap(@logger)

opts = if tls_context
{ ssl_context: tls_context }
Expand All @@ -54,24 +55,28 @@ def initialize(addr:, port:, logger:, default_app: nil, tls_context: nil)
end

def start(notify = nil)
Console.logger = Fluent::Log::ConsoleAdapter.wrap(@logger)
@logger.debug("Start async HTTP server listening #{@uri}")
task = @reactor.run do
@server.run

Async do |task|
Console.logger = Fluent::Log::ConsoleAdapter.wrap(@logger)
@server_task = task.async do
Console.logger = Fluent::Log::ConsoleAdapter.wrap(@logger)
@server.run
end
if notify
notify.push(:ready)
end
end

task.stop
@logger.debug('Finished HTTP server')
end

def stop
@logger.debug('closing HTTP server')

if @reactor
@reactor.stop
if @server_task
@server_task.stop
end
end

Expand Down
5 changes: 3 additions & 2 deletions test/plugin_helper/test_http_server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ def start_https_request(addr, port, verify: true, cert_path: nil, selfsigned: tr
end

client = Async::HTTP::Client.new(Async::HTTP::Endpoint.parse("https://#{addr}:#{port}", ssl_context: context))
reactor = Async::Reactor.new(nil, logger: Fluent::Log::ConsoleAdapter.wrap(NULL_LOGGER))
Console.logger = Fluent::Log::ConsoleAdapter.wrap(NULL_LOGGER)

resp = nil
error = nil

reactor.run do
Async do
Console.logger = Fluent::Log::ConsoleAdapter.wrap(NULL_LOGGER)
begin
response = yield(client)
rescue => e # Async::Reactor rescue all error. handle it by myself
Expand Down
Loading