forked from Kount/pq-timeouts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.go
43 lines (35 loc) · 1.4 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
package pqtimeouts
import (
"net"
"time"
)
type timeoutDialer struct {
netDial func(string, string) (net.Conn, error) // Allow this to be stubbed for testing
netDialTimeout func(string, string, time.Duration) (net.Conn, error) // Allow this to be stubbed for testing
readTimeout time.Duration
writeTimeout time.Duration
}
func (t timeoutDialer) Dial(network string, address string) (net.Conn, error) {
// If we don't have any timeouts set, just return a normal connection
if t.readTimeout == 0 && t.writeTimeout == 0 {
return t.netDial(network, address)
}
// Otherwise we want a timeoutConn to handle the read and write deadlines for us.
c, err := t.netDial(network, address)
if err != nil || c == nil {
return c, err
}
return &timeoutConn{conn: c, readTimeout: t.readTimeout, writeTimeout: t.writeTimeout}, nil
}
func (t timeoutDialer) DialTimeout(network string, address string, timeout time.Duration) (net.Conn, error) {
// If we don't have any timeouts set, just return a normal connection
if t.readTimeout == 0 && t.writeTimeout == 0 {
return t.netDialTimeout(network, address, timeout)
}
// Otherwise we want a timeoutConn to handle the read and write deadlines for us.
c, err := t.netDialTimeout(network, address, timeout)
if err != nil || c == nil {
return c, err
}
return &timeoutConn{conn: c, readTimeout: t.readTimeout, writeTimeout: t.writeTimeout}, nil
}