Skip to content

Commit

Permalink
Merge pull request #2493 from ganmacs/add-name-to-make-unique-thread-…
Browse files Browse the repository at this point in the history
…name

Add title to have unique thread name
  • Loading branch information
repeatedly authored Jul 12, 2019
2 parents 8688ebb + 2373dc3 commit a4867d8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_monitor_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def start

log.debug "listening monitoring http server on http://#{@bind}:#{@port}/api/plugins for worker#{fluentd_worker_id}"
api_handler = APIHandler.new(self)
create_http_server(addr: @bind, port: @port, logger: log, default_app: NotFoundJson) do |serv|
create_http_server(:in_monitor_http_server_helper, addr: @bind, port: @port, logger: log, default_app: NotFoundJson) do |serv|
serv.get('/api/plugins') { |req| api_handler.plugins_ltsv(req) }
serv.get('/api/plugins.json') { |req| api_handler.plugins_json(req) }
serv.get('/api/config') { |req| api_handler.config_ltsv(req) }
Expand Down
5 changes: 3 additions & 2 deletions lib/fluent/plugin_helper/http_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ module HttpServer
# close : correct stopped threads
# terminate: kill thread

# @param title [Symbol] the thread name. this value should be unique.
# @param addr [String] Listen address
# @param port [String] Listen port
# @param logger [Logger] logger used in this server
# @param default_app [Object] This method must have #call.
def create_http_server(addr:, port:, logger:, default_app: nil)
def create_http_server(title, addr:, port:, logger:, default_app: nil)
unless block_given?
raise ArgumentError, 'BUG: callback not specified'
end
Expand All @@ -48,7 +49,7 @@ def create_http_server(addr:, port:, logger:, default_app: nil)
end

_block_until_http_server_start do |notify|
thread_create(:plugin_helper_http_server) do
thread_create(title) do
@_http_server.start(notify)
end
end
Expand Down
18 changes: 10 additions & 8 deletions test/plugin_helper/test_http_server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def on_driver
%w[get head].each do |n|
define_method(n) do |uri, header = {}|
url = URI.parse(uri)
req = Net::HTTP.const_get(n.capitalize).new(url, header)
headers = { 'Content-Type' => 'application/x-www-form-urlencoded/' }.merge(header)
req = Net::HTTP.const_get(n.capitalize).new(url, headers)
Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
Expand All @@ -60,7 +61,8 @@ def on_driver
%w[post put patch delete options trace].each do |n|
define_method(n) do |uri, body = '', header = {}|
url = URI.parse(uri)
req = Net::HTTP.const_get(n.capitalize).new(url, header)
headers = { 'Content-Type' => 'application/x-www-form-urlencoded/' }.merge(header)
req = Net::HTTP.const_get(n.capitalize).new(url, headers)
req.body = body
Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
Expand All @@ -71,7 +73,7 @@ def on_driver
sub_test_case 'Create a HTTP server' do
test 'monunt given path' do
on_driver do |driver|
driver.create_http_server(addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
driver.create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
s.get('/example/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello get'] }
s.post('/example/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello post'] }
s.head('/example/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello head'] }
Expand Down Expand Up @@ -106,7 +108,7 @@ def on_driver

test 'when path does not start with `/` or ends with `/`' do
on_driver do |driver|
driver.create_http_server(addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
driver.create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
s.get('example/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello get'] }
s.get('/example/hello2/') { [200, { 'Content-Type' => 'text/plain' }, 'hello get'] }
end
Expand All @@ -121,7 +123,7 @@ def on_driver

test 'when error raised' do
on_driver do |driver|
driver.create_http_server(addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
driver.create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
s.get('/example/hello') { raise 'error!' }
end

Expand All @@ -132,7 +134,7 @@ def on_driver

test 'when path is not found' do
on_driver do |driver|
driver.create_http_server(addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
driver.create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
s.get('/example/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello get'] }
end

Expand All @@ -143,7 +145,7 @@ def on_driver

test 'params and body' do
on_driver do |driver|
driver.create_http_server(addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
driver.create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do |s|
s.get('/example/hello') do |req|
assert_equal(req.query_string, nil)
assert_equal(req.body, nil)
Expand Down Expand Up @@ -193,7 +195,7 @@ def on_driver
end

stub(Fluent::PluginHelper::HttpServer::Server).new(anything) { server }
driver.create_http_server(addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do
driver.create_http_server(:http_server_helper_test, addr: '127.0.0.1', port: PORT, logger: NULL_LOGGER) do
# nothing
end
driver.stop
Expand Down

0 comments on commit a4867d8

Please sign in to comment.