-
Notifications
You must be signed in to change notification settings - Fork 530
feat(network-agent): make host port allocator range configurable #576
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: master
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 |
|---|---|---|
|
|
@@ -94,7 +94,7 @@ func NewLocalService(cfg Config) (Service, error) { | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| ports, err := newPortAllocator() | ||
| ports, err := newPortAllocator(cfg.HostPortMin, cfg.HostPortMax) | ||
|
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. Note that the ephemeral port range ( |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,8 @@ import ( | |
| ) | ||
|
|
||
| const ( | ||
| portMin uint16 = 20000 | ||
| portMax uint16 = 29999 | ||
| defaultPortMin uint16 = 20000 | ||
| defaultPortMax uint16 = 29999 | ||
| ) | ||
|
|
||
| type portAllocator struct { | ||
|
|
@@ -25,19 +25,28 @@ type portAllocator struct { | |
| assigned map[uint16]struct{} | ||
| } | ||
|
|
||
| func newPortAllocator() (*portAllocator, error) { | ||
| func newPortAllocator(min, max uint16) (*portAllocator, error) { | ||
| if min == 0 && max == 0 { | ||
| min, max = defaultPortMin, defaultPortMax | ||
| } | ||
| if min > max { | ||
| return nil, fmt.Errorf("invalid host port range: min=%d > max=%d", min, max) | ||
| } | ||
| if min < 1024 { | ||
|
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. The Consider either: |
||
| return nil, fmt.Errorf("invalid host port range: min=%d must be >= 1024", min) | ||
| } | ||
|
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. There's no validation that the configured range has usable capacity. A configuration of Consider adding a minimum range width check, e.g. |
||
| alloc := &portAllocator{ | ||
| min: portMin, | ||
| max: portMax, | ||
| next: portMin, | ||
| min: min, | ||
| max: max, | ||
| next: min, | ||
| assigned: make(map[uint16]struct{}), | ||
| } | ||
| reservedPorts, err := getReservedPorts() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, port := range reservedPorts { | ||
| if port < portMin || port > portMax { | ||
| if port < min || port > max { | ||
| continue | ||
| } | ||
| alloc.assigned[port] = struct{}{} | ||
|
|
||
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.
The
!= 0checks are applied independently to each field. If an operator sets onlyhost_port_min = 50000in TOML (leavinghost_port_maxunset → zero), the result isHostPortMin=50000, HostPortMax=29999(the default survives), which then fails innewPortAllocatorwith"invalid host port range: min=50000 > max=29999". The error is caught and the service won't start, but the message may confuse an operator who only set one value.Consider either: (a) requiring both fields to be set when either is present, or (b) improving the error message in
newPortAllocatorto clearly show that only one value was overridden.