-
Notifications
You must be signed in to change notification settings - Fork 75
use PortManagementSupport mixin to reserve port in #register #238
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||||||||
| require "logstash/util/socket_peer" | ||||||||||||||||
| require "logstash-input-tcp_jars" | ||||||||||||||||
| require 'logstash/plugin_mixins/ecs_compatibility_support' | ||||||||||||||||
| require 'logstash/plugin_mixins/port_management_support' | ||||||||||||||||
|
|
||||||||||||||||
| require "socket" | ||||||||||||||||
| require "openssl" | ||||||||||||||||
|
|
@@ -68,6 +69,8 @@ class LogStash::Inputs::Tcp < LogStash::Inputs::Base | |||||||||||||||
| # ecs_compatibility option, provided by Logstash core or the support adapter. | ||||||||||||||||
| include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1, :v8 => :v1) | ||||||||||||||||
|
|
||||||||||||||||
| include LogStash::PluginMixins::PortManagementSupport | ||||||||||||||||
|
|
||||||||||||||||
| config_name "tcp" | ||||||||||||||||
|
|
||||||||||||||||
| default :codec, "line" | ||||||||||||||||
|
|
@@ -177,15 +180,20 @@ def register | |||||||||||||||
| validate_ssl_config! | ||||||||||||||||
|
|
||||||||||||||||
| if server? | ||||||||||||||||
| @loop = InputLoop.new(@id, @host, @port, DecoderImpl.new(@codec, self), @tcp_keep_alive, java_ssl_context) | ||||||||||||||||
| @port_reservation = port_management.reserve(addr: @host, port: @port) do |reserved_addr, reserved_port| | ||||||||||||||||
| @loop = InputLoop.new(@id, reserved_addr, reserved_port, DecoderImpl.new(@codec, self), @tcp_keep_alive, java_ssl_context) | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| def run(output_queue) | ||||||||||||||||
| @output_queue = output_queue | ||||||||||||||||
| if server? | ||||||||||||||||
| @logger.info("Starting tcp input listener", :address => "#{@host}:#{@port}", :ssl_enabled => @ssl_enabled) | ||||||||||||||||
| @loop.run | ||||||||||||||||
| @port_reservation.convert do |reserved_addr, reserved_port| | ||||||||||||||||
| @logger.info("Starting tcp input listener", :address => "#{reserved_addr}:#{reserved_port}", :ssl_enabled => @ssl_enabled) | ||||||||||||||||
| @loop.start | ||||||||||||||||
| end | ||||||||||||||||
| @loop.wait_until_closed | ||||||||||||||||
|
Comment on lines
+192
to
+196
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we just do:
Suggested change
Essentially tell the global manager:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In The point of allowing the caller to execute a block while the global lock is held is to ensure that some other plugin can't use this library to create another reservation in the window between the dummy server being shut down and the caller standing up something to replace it. If we were to add a second layer of locking (e.g., each reservation having its own mutex), we could do the bind outside of the global lock but we would introduce complexity around the race conditions. |
||||||||||||||||
| else | ||||||||||||||||
| run_client() | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to use blocks here since we're not wrapping behavior:
Also we should set the reservation scope for the port alone. Not sure if it's worth differentiating the addr, we can be conservative here and allow the port to be reserved regardless of the addr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are.
PortManagementSupport::Reservation#initializereleases the reservation if an exception is raised by the block.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to spawn the server that effectively holds the reservation, we need to know the addr to bind to, so really we are reserving an addr:port pair, not just the port.