-
Notifications
You must be signed in to change notification settings - Fork 2
/
dialer.go
54 lines (46 loc) · 1.06 KB
/
dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package proxy
import (
"context"
"errors"
"log/slog"
"net"
"time"
"github.com/moonrhythm/parapet-ingress-controller/metric"
)
type dialer struct {
inner net.Dialer
onError func(addr string)
}
func newDialer() *dialer {
return &dialer{
inner: net.Dialer{
Timeout: 2 * time.Second,
KeepAlive: 30 * time.Second,
},
}
}
func (d *dialer) DialContext(ctx context.Context, network, addr string) (conn net.Conn, err error) {
conn, err = d.inner.DialContext(ctx, network, addr)
if err != nil {
if ctx.Err() == nil { // parent context is not canceled
if d.onError != nil {
d.onError(addr)
}
slog.Error("proxy: can not connect", "addr", addr, "error", err)
}
return
}
conn = metric.BackendConnections(conn, addr)
return
}
func isDialError(err error) bool {
if err == nil {
return false
}
// go1.19 i/o timeout error will now satisfy errors.Is(err, context.DeadlineExceeded)
if errors.Is(err, context.DeadlineExceeded) {
return true
}
var netOpError *net.OpError
return errors.As(err, &netOpError) && netOpError.Op == "dial"
}