@@ -46,7 +46,7 @@ type connection struct {
4646 client * TarsClient
4747
4848 conn net.Conn
49- connLock * sync.Mutex
49+ connLock sync.Mutex
5050
5151 isClosed bool
5252 idleTime time.Time
@@ -66,7 +66,7 @@ func NewTarsClient(address string, protocol ClientProtocol, config *TarsClientCo
6666 sendQueue : make (chan sendMsg , config .QueueLen ),
6767 sendFailQueue : make (chan sendMsg , 1 ),
6868 }
69- client .conn = & connection {client : client , isClosed : true , connLock : & sync. Mutex {}, dialTimeout : config .DialTimeout }
69+ client .conn = & connection {client : client , isClosed : true , dialTimeout : config .DialTimeout }
7070 return client
7171}
7272
@@ -122,6 +122,35 @@ func (tc *TarsClient) GraceClose(ctx context.Context) {
122122 }
123123}
124124
125+ func (c * connection ) ReConnect () (err error ) {
126+ c .connLock .Lock ()
127+ defer c .connLock .Unlock ()
128+ if c .isClosed {
129+ TLOG .Debug ("Connect:" , c .client .address , "Proto:" , c .client .config .Proto )
130+ if c .client .config .Proto == "ssl" {
131+ dialer := & net.Dialer {Timeout : c .dialTimeout }
132+ c .conn , err = tls .DialWithDialer (dialer , "tcp" , c .client .address , c .client .config .TlsConfig )
133+ } else {
134+ c .conn , err = net .DialTimeout (c .client .config .Proto , c .client .address , c .dialTimeout )
135+ }
136+
137+ if err != nil {
138+ return err
139+ }
140+ if c .client .config .Proto == "tcp" {
141+ if c .conn != nil {
142+ _ = c .conn .(* net.TCPConn ).SetKeepAlive (true )
143+ }
144+ }
145+ c .idleTime = time .Now ()
146+ c .isClosed = false
147+ connDone := make (chan bool , 1 )
148+ go c .recv (c .conn , connDone )
149+ go c .send (c .conn , connDone )
150+ }
151+ return nil
152+ }
153+
125154func (c * connection ) send (conn net.Conn , connDone chan bool ) {
126155 var m sendMsg
127156 t := time .NewTicker (time .Second )
@@ -164,6 +193,14 @@ func (c *connection) send(conn net.Conn, connDone chan bool) {
164193 TLOG .Errorf ("send request retry: %d, error: %v" , m .retry , err )
165194 c .client .sendFailQueue <- m
166195 c .close (conn )
196+ if err != net .ErrClosed {
197+ return
198+ }
199+
200+ // connection closed, try to reconnect once
201+ if err = c .ReConnect (); err != nil {
202+ TLOG .Errorf ("send request reconnect error: %v" , err )
203+ }
167204 return
168205 }
169206 }
@@ -226,35 +263,6 @@ func (c *connection) recv(conn net.Conn, connDone chan bool) {
226263 }
227264}
228265
229- func (c * connection ) ReConnect () (err error ) {
230- c .connLock .Lock ()
231- defer c .connLock .Unlock ()
232- if c .isClosed {
233- TLOG .Debug ("Connect:" , c .client .address , "Proto:" , c .client .config .Proto )
234- if c .client .config .Proto == "ssl" {
235- dialer := & net.Dialer {Timeout : c .dialTimeout }
236- c .conn , err = tls .DialWithDialer (dialer , "tcp" , c .client .address , c .client .config .TlsConfig )
237- } else {
238- c .conn , err = net .DialTimeout (c .client .config .Proto , c .client .address , c .dialTimeout )
239- }
240-
241- if err != nil {
242- return err
243- }
244- if c .client .config .Proto == "tcp" {
245- if c .conn != nil {
246- _ = c .conn .(* net.TCPConn ).SetKeepAlive (true )
247- }
248- }
249- c .idleTime = time .Now ()
250- c .isClosed = false
251- connDone := make (chan bool , 1 )
252- go c .recv (c .conn , connDone )
253- go c .send (c .conn , connDone )
254- }
255- return nil
256- }
257-
258266func (c * connection ) close (conn net.Conn ) {
259267 c .connLock .Lock ()
260268 defer c .connLock .Unlock ()
0 commit comments