Skip to content

Commit 276d2ea

Browse files
committed
isTemporaryNetErr didn't work when the error was wrapped
The fix was based on the below PR, but slightly changed to use net.Error as is. firecracker-microvm/firecracker-containerd#583 Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
1 parent f6a64e7 commit 276d2ea

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

vsock/dial.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ func (e connectMsgError) Temporary() bool {
239239
return false
240240
}
241241

242+
func (e connectMsgError) Timeout() bool {
243+
return false
244+
}
245+
242246
type ackError struct {
243247
cause error
244248
}
@@ -251,13 +255,12 @@ func (e ackError) Temporary() bool {
251255
return true
252256
}
253257

254-
// isTemporaryNetErr returns whether the provided error is a retriable
255-
// error, according to the interface defined here:
256-
// https://golang.org/pkg/net/#Error
257-
func isTemporaryNetErr(err error) bool {
258-
terr, ok := err.(interface {
259-
Temporary() bool
260-
})
258+
func (e ackError) Timeout() bool {
259+
return false
260+
}
261261

262-
return err != nil && ok && terr.Temporary()
262+
// isTemporaryNetErr returns whether the provided error is a retriable error.
263+
func isTemporaryNetErr(err error) bool {
264+
var netError net.Error
265+
return err != nil && errors.As(err, &netError) && netError.Temporary()
263266
}

vsock/dial_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package vsock
15+
16+
import (
17+
"errors"
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestTemporaryNetErr(t *testing.T) {
24+
assert.True(t, isTemporaryNetErr(&ackError{cause: errors.New("ack")}))
25+
26+
assert.False(t, isTemporaryNetErr(&connectMsgError{cause: errors.New("connect")}))
27+
assert.False(t, isTemporaryNetErr(errors.New("something else")))
28+
assert.False(t, isTemporaryNetErr(nil))
29+
}

0 commit comments

Comments
 (0)