@@ -21,29 +21,30 @@ type Conn struct {
2121 mutex sync.Mutex
2222 rb * bufio.Reader
2323 wb * bufio.Writer
24+ sentinel bool
2425 RequestTimeout time.Duration
2526}
2627
2728// Dial opens a TCP connection with a redis server.
2829func Dial (address string ) (* Conn , error ) {
2930 conn := & Conn {
30- RequestTimeout : 10 * time .Second ,
31+ RequestTimeout : time . Duration ( Config . RequestTimeout ) * time .Second ,
3132 }
3233 conn .mutex .Lock ()
33- defer conn .mutex .Unlock ()
34+ defer conn .mutex .Unlock ()
3435 err := conn .connect (address , 0 )
3536 return conn , err
3637}
3738
3839// DialTimeout opens a TCP connection with a redis server with a connection timeout
3940func DialTimeout (address string , timeout time.Duration ) (* Conn , error ) {
40- conn := & Conn {
41- RequestTimeout : 10 * time .Second ,
42- }
41+ conn := & Conn {
42+ RequestTimeout : time . Duration ( Config . RequestTimeout ) * time .Second ,
43+ }
4344 conn .mutex .Lock ()
44- defer conn .mutex .Unlock ()
45- err := conn .connect (address , timeout )
46- return conn , err
45+ defer conn .mutex .Unlock ()
46+ err := conn .connect (address , timeout )
47+ return conn , err
4748}
4849
4950// Close closes the connection
@@ -92,21 +93,19 @@ func (c *Conn) connect(address string, timeout time.Duration) error {
9293}
9394
9495func (c * Conn ) fail () {
95- c .reconnect ()
96- }
97-
98- func (c * Conn ) reconnect () {
99- c .mutex .Lock ()
100- defer c .mutex .Unlock ()
101- if c .state == connStateReconnecting {
102- return
96+ if ! c .sentinel {
97+ c .mutex .Lock ()
98+ defer c .mutex .Unlock ()
99+ if c .state == connStateReconnecting {
100+ return
101+ }
102+ c .tcpConn .Close ()
103+ c .state = connStateReconnecting
104+ for {
105+ if err := c .connect (c .address , 0 ); err == nil {
106+ break
107+ }
108+ time .Sleep (time .Duration (Config .ReconnectTime ) * time .Second )
109+ }
103110 }
104- c .tcpConn .Close ()
105- c .state = connStateReconnecting
106- for {
107- if err := c .connect (c .address , 0 ); err == nil {
108- break
109- }
110- time .Sleep (2 * time .Second )
111- }
112111}
0 commit comments