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 3 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.
13 changes: 11 additions & 2 deletions config/confignet/confignet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package confignet // import "go.opentelemetry.io/collector/config/confignet"

import (
"net"
"time"
)

// NetAddr represents a network endpoint address.
Expand All @@ -19,11 +20,15 @@ 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"`

// DialerTimeout is the maximum amount of time a dial will wait for
// a connect to complete. The default is no timeout.
DialerTimeout time.Duration `mapstructure:"dialer_timeout"`
}

// 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.DialerTimeout)
}

// Listen equivalent with net.Listen for this address.
Expand All @@ -39,11 +44,15 @@ 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"`

// DialerTimeout is the maximum amount of time a dial will wait for
// a connect to complete. The default is no timeout.
DialerTimeout time.Duration `mapstructure:"dialer_timeout"`
}
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved

// 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.DialerTimeout)
}

// Listen equivalent with net.Listen for this address.
Expand Down
44 changes: 42 additions & 2 deletions config/confignet/confignet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,53 @@
package confignet

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

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

func TestNetAddr(t *testing.T) {
func Test_SetOrder(t *testing.T) {
t.Run("TestNetAddrTimeout", testNetAddrTimeout)
t.Run("TestTcpAddrTimeout", testTcpAddrTimeout)
t.Run("TestNetAddr", testNetAddr)
t.Run("testTcpAddr", testTcpAddr)
}

func testNetAddrTimeout(t *testing.T) {
nac := &NetAddr{
Endpoint: "localhost:0",
Transport: "tcp",
DialerTimeout: time.Millisecond,
}
_, 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",
DialerTimeout: time.Millisecond,
}
_, 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",
Transport: "tcp",
Expand Down Expand Up @@ -45,7 +85,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