chore(natpmp): initialRetry -> initialConnectionDuration

This commit is contained in:
Quentin McGaw
2023-07-06 06:50:17 +00:00
parent f31a846cda
commit 075a1e2a80
6 changed files with 93 additions and 93 deletions

View File

@@ -18,7 +18,7 @@ func Test_Client_ExternalAddress(t *testing.T) {
testCases := map[string]struct {
ctx context.Context
gateway netip.Addr
initialRetry time.Duration
initialConnDuration time.Duration
exchanges []udpExchange
durationSinceStartOfEpoch time.Duration
externalIPv4Address netip.Addr
@@ -28,14 +28,14 @@ func Test_Client_ExternalAddress(t *testing.T) {
"failure": {
ctx: canceledCtx,
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
initialRetry: time.Millisecond,
initialConnDuration: time.Millisecond,
err: context.Canceled,
errMessage: "executing remote procedure call: reading from udp connection: context canceled",
},
"success": {
ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
initialRetry: time.Millisecond,
initialConnDuration: time.Millisecond,
exchanges: []udpExchange{{
request: []byte{0, 0},
response: []byte{0x0, 0x80, 0x0, 0x0, 0x0, 0x13, 0xf2, 0x4f, 0x49, 0x8c, 0x36, 0x9a},
@@ -54,7 +54,7 @@ func Test_Client_ExternalAddress(t *testing.T) {
client := Client{
serverPort: uint16(remoteAddress.Port),
initialRetry: testCase.initialRetry,
initialConnectionDuration: testCase.initialConnDuration,
maxRetries: 1,
}

View File

@@ -7,7 +7,7 @@ import (
// Client is a NAT-PMP protocol client.
type Client struct {
serverPort uint16
initialRetry time.Duration
initialConnectionDuration time.Duration
maxRetries uint
}
@@ -16,11 +16,11 @@ func New() (client *Client) {
const natpmpPort = 5351
// Parameters described in https://www.ietf.org/rfc/rfc6886.html#section-3.1
const initialRetry = 250 * time.Millisecond
const initialConnectionDuration = 250 * time.Millisecond
const maxTries = 9 // 64 seconds
return &Client{
serverPort: natpmpPort,
initialRetry: initialRetry,
initialConnectionDuration: initialConnectionDuration,
maxRetries: maxTries,
}
}

View File

@@ -12,7 +12,7 @@ func Test_New(t *testing.T) {
expectedClient := &Client{
serverPort: 5351,
initialRetry: 250 * time.Millisecond,
initialConnectionDuration: 250 * time.Millisecond,
maxRetries: 9,
}
client := New()

View File

@@ -19,7 +19,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort uint16
requestedExternalPort uint16
lifetime time.Duration
initialRetry time.Duration
initialConnDuration time.Duration
exchanges []udpExchange
durationSinceStartOfEpoch time.Duration
assignedInternalPort uint16
@@ -46,7 +46,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort: 123,
requestedExternalPort: 456,
lifetime: 1200 * time.Second,
initialRetry: time.Millisecond,
initialConnDuration: time.Millisecond,
exchanges: []udpExchange{{close: true}},
err: ErrConnectionTimeout,
errMessage: "executing remote procedure call: connection timeout: after 1ms",
@@ -58,7 +58,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort: 123,
requestedExternalPort: 456,
lifetime: 1200 * time.Second,
initialRetry: time.Second,
initialConnDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x1, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x81, 0x0, 0x0, 0x0, 0x13, 0xfe, 0xff, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
@@ -75,7 +75,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
internalPort: 123,
requestedExternalPort: 456,
lifetime: 1200 * time.Second,
initialRetry: time.Second,
initialConnDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x82, 0x0, 0x0, 0x0, 0x14, 0x3, 0x21, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
@@ -90,7 +90,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "udp",
internalPort: 123,
initialRetry: time.Second,
initialConnDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x1, 0x0, 0x0, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
response: []byte{0x0, 0x81, 0x0, 0x0, 0x0, 0x14, 0x3, 0xd5, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -103,7 +103,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
protocol: "tcp",
internalPort: 123,
initialRetry: time.Second,
initialConnDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
response: []byte{0x0, 0x82, 0x0, 0x0, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -122,7 +122,7 @@ func Test_Client_AddPortMapping(t *testing.T) {
client := Client{
serverPort: uint16(remoteAddress.Port),
initialRetry: testCase.initialRetry,
initialConnectionDuration: testCase.initialConnDuration,
maxRetries: 1,
}

View File

@@ -61,15 +61,15 @@ func (c *Client) rpc(ctx context.Context, gateway netip.Addr,
const maxResponseSize = 16
response = make([]byte, maxResponseSize)
// Retry duration doubles on every network error
// Connection duration doubles on every network error
// Note it does not double if the source IP mismatches the gateway IP.
retryDuration := c.initialRetry
connectionDuration := c.initialConnectionDuration
var totalRetryDuration time.Duration
var retryCount uint
for retryCount = 0; retryCount < c.maxRetries; retryCount++ {
deadline := time.Now().Add(retryDuration)
deadline := time.Now().Add(connectionDuration)
err = connection.SetDeadline(deadline)
if err != nil {
return nil, fmt.Errorf("setting connection deadline: %w", err)
@@ -87,8 +87,8 @@ func (c *Client) rpc(ctx context.Context, gateway netip.Addr,
}
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
totalRetryDuration += retryDuration
retryDuration *= 2
totalRetryDuration += connectionDuration
connectionDuration *= 2
continue
}
return nil, fmt.Errorf("reading from udp connection: %w", err)

View File

@@ -17,7 +17,7 @@ func Test_Client_rpc(t *testing.T) {
gateway netip.Addr
request []byte
responseSize uint
initialRetry time.Duration
initialConnectionDuration time.Duration
exchanges []udpExchange
expectedResponse []byte
err error
@@ -32,7 +32,7 @@ func Test_Client_rpc(t *testing.T) {
"request_too_small": {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0},
initialRetry: time.Second,
initialConnectionDuration: time.Second,
err: ErrRequestSizeTooSmall,
errMessage: `checking request: message size is too small: ` +
`need at least 2 bytes and got 1 byte\(s\)`,
@@ -49,7 +49,7 @@ func Test_Client_rpc(t *testing.T) {
ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0, 1},
initialRetry: time.Millisecond,
initialConnectionDuration: time.Millisecond,
exchanges: []udpExchange{
{request: []byte{0, 1}, close: true},
},
@@ -60,7 +60,7 @@ func Test_Client_rpc(t *testing.T) {
ctx: context.Background(),
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0, 0},
initialRetry: time.Second,
initialConnectionDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0, 0},
response: []byte{1},
@@ -74,7 +74,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 5,
initialRetry: time.Second,
initialConnectionDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0, 1, 2, 3}, // size 4
@@ -88,7 +88,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialRetry: time.Second,
initialConnectionDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x1, 0x82, 0x0, 0x0, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -101,7 +101,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialRetry: time.Second,
initialConnectionDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x88, 0x0, 0x0, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -114,7 +114,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialRetry: time.Second,
initialConnectionDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x82, 0x0, 0x11, 0x0, 0x14, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -127,7 +127,7 @@ func Test_Client_rpc(t *testing.T) {
gateway: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
responseSize: 16,
initialRetry: time.Second,
initialConnectionDuration: time.Second,
exchanges: []udpExchange{{
request: []byte{0x0, 0x2, 0x0, 0x0, 0x0, 0x7b, 0x1, 0xc8, 0x0, 0x0, 0x4, 0xb0},
response: []byte{0x0, 0x82, 0x0, 0x0, 0x0, 0x0, 0x4, 0x96, 0x0, 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
@@ -145,7 +145,7 @@ func Test_Client_rpc(t *testing.T) {
client := Client{
serverPort: uint16(remoteAddress.Port),
initialRetry: testCase.initialRetry,
initialConnectionDuration: testCase.initialConnectionDuration,
maxRetries: 1,
}