forked from 3XX0/pooly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
57 lines (48 loc) · 1.16 KB
/
net.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
55
56
57
package pooly
import (
"net"
"time"
)
// NetDriver is a predefined driver for handling standard net.Conn objects.
type NetDriver struct {
network string
timeout time.Duration
}
// NewNetDriver instantiates a new NetDriver, ready to be used in a PoolConfig.
func NewNetDriver(network string) *NetDriver {
return &NetDriver{network: network}
}
// SetTimeout sets the dialing timeout on a net.Conn object.
func (n *NetDriver) SetTimeout(timeout time.Duration) {
n.timeout = timeout
}
// Dial is analogous to net.Dial.
func (n *NetDriver) Dial(address string) (*Conn, error) {
var c net.Conn
var err error
if n.timeout > 0 {
c, err = net.DialTimeout(n.network, address, n.timeout)
} else {
c, err = net.Dial(n.network, address)
}
return NewConn(c), err
}
// Close is analogous to net.Close.
func (n *NetDriver) Close(c *Conn) {
nc := c.NetConn()
if nc == nil {
return
}
_ = nc.Close()
}
// TestOnBorrow does nothing.
func (n *NetDriver) TestOnBorrow(c *Conn) error {
return nil
}
// Temporary is analogous to net.Error.Temporary.
func (n *NetDriver) Temporary(err error) bool {
if e, ok := err.(net.Error); ok {
return e.Temporary()
}
return false
}