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

[confignet] Add DialerTimeout config option #9066

Merged
Merged
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
25 changes: 25 additions & 0 deletions .chloggen/confignet-add-dialertimeout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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: confignet

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `dialer_timeout` config option.

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

# (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: []
1 change: 1 addition & 0 deletions config/confignet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ leverage network configuration to set connection and transport information.
- `transport`: Known protocols are "tcp", "tcp4" (IPv4-only), "tcp6"
(IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4"
(IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".
- `dialer_timeout`: DialerTimeout is the maximum amount of time a dial will wait for a connect to complete. The default is no timeout.

Note that for TCP receivers only the `endpoint` configuration setting is
required.
18 changes: 16 additions & 2 deletions config/confignet/confignet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ package confignet // import "go.opentelemetry.io/collector/config/confignet"

import (
"net"
"time"
)

// DialerConfig contains options for connecting to an address.
type DialerConfig struct {
// Timeout is the maximum amount of time a dial will wait for
// a connect to complete. The default is no timeout.
Timeout time.Duration `mapstructure:"timeout"`
}

// NetAddr represents a network endpoint address.
type NetAddr struct {
// Endpoint configures the address for this network connection.
Expand All @@ -19,11 +27,14 @@ type NetAddr struct {
// Transport to use. Known protocols are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only),
// "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".
Transport string `mapstructure:"transport"`

// DialerConfig contains options for connecting to an address.
DialerConfig DialerConfig `mapstructure:"dialer"`
}

// Dial equivalent with net.Dial for this address.
func (na *NetAddr) Dial() (net.Conn, error) {
return net.Dial(na.Transport, na.Endpoint)
return net.DialTimeout(na.Transport, na.Endpoint, na.DialerConfig.Timeout)
}

// Listen equivalent with net.Listen for this address.
Expand All @@ -39,11 +50,14 @@ type TCPAddr struct {
// If the host is a literal IPv6 address it must be enclosed in square brackets, as in "[2001:db8::1]:80" or
// "[fe80::1%zone]:80". The zone specifies the scope of the literal IPv6 address as defined in RFC 4007.
Endpoint string `mapstructure:"endpoint"`

// DialerConfig contains options for connecting to an address.
DialerConfig DialerConfig `mapstructure:"dialer"`
}

// Dial equivalent with net.Dial for this address.
func (na *TCPAddr) Dial() (net.Conn, error) {
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
return net.Dial("tcp", na.Endpoint)
return net.DialTimeout("tcp", na.Endpoint, na.DialerConfig.Timeout)
}

// Listen equivalent with net.Listen for this address.
Expand Down
39 changes: 38 additions & 1 deletion config/confignet/confignet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,49 @@
package confignet

import (
"errors"
"net"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestNetAddrTimeout(t *testing.T) {
nac := &NetAddr{
Endpoint: "localhost:0",
Transport: "tcp",
DialerConfig: DialerConfig{
Timeout: -1 * time.Second,
},
}
_, err := nac.Dial()
assert.Error(t, err)
var netErr net.Error
if errors.As(err, &netErr) {
assert.True(t, netErr.Timeout())
} else {
assert.Fail(t, "error should be a net.Error")
}
}

func TestTCPAddrTimeout(t *testing.T) {
nac := &TCPAddr{
Endpoint: "localhost:0",
DialerConfig: DialerConfig{
Timeout: -1 * time.Second,
},
}
_, err := nac.Dial()
assert.Error(t, err)
var netErr net.Error
if errors.As(err, &netErr) {
assert.True(t, netErr.Timeout())
} else {
assert.Fail(t, "error should be a net.Error")
}
}

func TestNetAddr(t *testing.T) {
nas := &NetAddr{
Endpoint: "localhost:0",
Expand Down Expand Up @@ -45,7 +82,7 @@ func TestNetAddr(t *testing.T) {
assert.NoError(t, ln.Close())
}

func TestTcpAddr(t *testing.T) {
func TestTCPAddr(t *testing.T) {
nas := &TCPAddr{
Endpoint: "localhost:0",
}
Expand Down
Loading