Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions .chloggen/so-reuse-port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pkg/config/confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
# cspell:ignore REUSEPORT
note: Added ReusePort option to confighttp.ServerConfig to enable SO_REUSEPORT on supported platforms.

# One or more tracking issues or pull requests related to the change
issues: [14046]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
2 changes: 1 addition & 1 deletion config/confighttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/sys v0.35.0
golang.org/x/text v0.28.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250804133106-a7a43d27e69b // indirect
google.golang.org/grpc v1.76.0 // indirect
Expand Down
30 changes: 30 additions & 0 deletions config/confighttp/listen_config_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build !windows

package confighttp // import "go.opentelemetry.io/collector/config/confighttp"

import (
"net"
"syscall"

"golang.org/x/sys/unix"
)

func (sc *ServerConfig) getListenConfig() (net.ListenConfig, error) {
cfg := net.ListenConfig{}
if sc.ReusePort {
cfg.Control = func(_, _ string, c syscall.RawConn) error {
var controlErr error
err := c.Control(func(fd uintptr) {
controlErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
})
if err != nil {
return err
}
return controlErr
}
}

return cfg, nil
}
18 changes: 18 additions & 0 deletions config/confighttp/listen_config_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows

package confighttp // import "go.opentelemetry.io/collector/config/confighttp"

import (
"errors"
"net"
)

func (sc *ServerConfig) getListenConfig() (net.ListenConfig, error) {
if sc.ReusePort {
return net.ListenConfig{}, errors.New("ReusePort is not supported on this platform")
}

return net.ListenConfig{}, nil
}
Comment on lines +12 to +18
Copy link
Member

@bogdandrutu bogdandrutu Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to have this in Validate and fail during dry-run.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdandrutu The ServerConfig doesn't have a Validate yet - are you proposing I add one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, introducing Validate should be another PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, introducing Validate should be another PR.

why?

Copy link
Member

@bogdandrutu bogdandrutu Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ServerConfig doesn't have a Validate yet - are you proposing I add one?

Yes please, every time we add fields that need to be validated we should validate them and if that needs a Validate func we add it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added @bogdandrutu

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Because that's a separate change that would need its own changelog entry, and having atomic PRs facilitates reviews.

Copy link
Author

@sinkingpoint sinkingpoint Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmathieu @bogdandrutu Where are we with this? I've added the Validate for now - IMO it does make sense to have it here so there isn't a period where it isn't validated. Especially with this change being the only thing in the Validate method, bundling make sense here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If @bogdandrutu is fine with this, it can go.

13 changes: 12 additions & 1 deletion config/confighttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ type ServerConfig struct {
// KeepAlivesEnabled controls whether HTTP keep-alives are enabled.
// By default, keep-alives are always enabled. Only very resource-constrained environments should disable them.
KeepAlivesEnabled bool `mapstructure:"keep_alives_enabled,omitempty"`

// ReusePort enables the SO_REUSEPORT socket option on the listener.
// This allows multiple server instances to bind to the same address/port.
// This is useful for horizontal scaling and zero-downtime restarts.
// Note: This option is not supported on all operating systems.
ReusePort bool `mapstructure:"reuse_port,omitempty"`
}

// NewDefaultServerConfig returns ServerConfig type object with default values.
Expand Down Expand Up @@ -123,7 +129,12 @@ type AuthConfig struct {

// ToListener creates a net.Listener.
func (sc *ServerConfig) ToListener(ctx context.Context) (net.Listener, error) {
listener, err := net.Listen("tcp", sc.Endpoint)
cfg, err := sc.getListenConfig() // See listen_config_*.go for platform-specific implementations
if err != nil {
return nil, err
}

listener, err := cfg.Listen(ctx, "tcp", sc.Endpoint)
if err != nil {
return nil, err
}
Expand Down
55 changes: 55 additions & 0 deletions config/confighttp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"net/http/httptest"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -1169,3 +1170,57 @@ func TestServerMiddlewaresFieldCompatibility(t *testing.T) {
assert.Len(t, serverConfig.Middlewares, 1)
assert.Equal(t, component.MustNewID("server_middleware"), serverConfig.Middlewares[0].ID)
}

func TestServerReusePort(t *testing.T) {
if runtime.GOOS == "windows" {
sc := &ServerConfig{
Endpoint: "localhost:4318",
ReusePort: true,
}

_, err := sc.ToListener(t.Context())
require.Error(t, err, "ReusePort is not supported on Windows")
return
}

tests := []struct {
name string
reusePort bool
expectedError bool
}{
{
name: "ReusePort enabled",
reusePort: true,
expectedError: false,
},
{
name: "ReusePort disabled",
reusePort: false,
expectedError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sc := &ServerConfig{
Endpoint: "localhost:4318",
ReusePort: tt.reusePort,
}

ln1, err := sc.ToListener(t.Context())
require.NoError(t, err)
defer ln1.Close()

ln2, err := sc.ToListener(t.Context())
if tt.expectedError {
require.Error(t, err)
} else {
require.NoError(t, err)
}

if ln2 != nil {
ln2.Close()
}
})
}
}