forked from jcoene/riago
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.go
88 lines (72 loc) · 1.53 KB
/
driver.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package riago
import (
"github.com/3XX0/pooly"
"net"
"regexp"
"time"
)
var hostport = regexp.MustCompile("[?.*]?:.*")
type Driver struct {
connTimeout time.Duration
writeTimeout time.Duration
readTimeout time.Duration
testInterval time.Duration
}
func NewDriver() *Driver {
return new(Driver)
}
func (d *Driver) SetConnTimeout(t time.Duration) {
d.connTimeout = t
}
func (d *Driver) SetReadTimeout(t time.Duration) {
d.readTimeout = t
}
func (d *Driver) SetWriteTimeout(t time.Duration) {
d.writeTimeout = t
}
func (d *Driver) SetTestInterval(t time.Duration) {
d.testInterval = t
}
func (d *Driver) Dial(address string) (*pooly.Conn, error) {
var c net.Conn
var err error
if !hostport.MatchString(address) {
address += ":8087" // default riak pbc port
}
if d.connTimeout > 0 {
c, err = net.DialTimeout("tcp", address, d.connTimeout)
} else {
c, err = net.Dial("tcp", address)
}
if err != nil {
return nil, err
}
conn := &Conn{
conn: c.(*net.TCPConn),
writeTimeout: d.writeTimeout,
readTimeout: d.readTimeout,
}
return pooly.NewConn(conn), nil
}
func (d *Driver) Close(conn *pooly.Conn) {
c := Riak(conn)
if c != nil {
c.conn.Close()
}
}
func (d *Driver) TestOnBorrow(conn *pooly.Conn) error {
c := Riak(conn)
if !c.lastChecked.IsZero() {
if t := time.Now().Sub(c.lastChecked); t < d.testInterval {
return nil
}
}
c.lastChecked = time.Now()
return c.Ping()
}
func (d *Driver) Temporary(err error) bool {
if e, ok := err.(net.Error); ok {
return e.Temporary()
}
return false
}