@@ -13,6 +13,7 @@ type Proxy interface {
1313 Connect (c gnet.Conn ) error
1414 Disconnect (c gnet.Conn ) error
1515 PassThrough (c gnet.Conn ) error
16+ Reconnect (cl * Client ) * Client
1617 Shutdown ()
1718 Size () int
1819}
@@ -92,25 +93,23 @@ func (pr *ProxyImpl) Connect(c gnet.Conn) error {
9293
9394func (pr * ProxyImpl ) Disconnect (c gnet.Conn ) error {
9495 var client * Client
95- if c , ok := pr .connClients .Load (c ); ok {
96- client = c .(* Client )
96+ if cl , ok := pr .connClients .Load (c ); ok {
97+ client = cl .(* Client )
9798 }
9899 pr .connClients .Delete (c )
99100
100101 // TODO: The connection is unstable when I put the client back in the pool
101102 // If the client is not in the pool, put it back
102- if client != nil && client .ID != "" {
103- if pr .Elastic && pr .ReuseElasticClients {
103+
104+ if pr .Elastic && pr .ReuseElasticClients || ! pr .Elastic {
105+ client = pr .Reconnect (client )
106+ if client != nil && client .ID != "" {
104107 if err := pr .pool .Put (client ); err != nil {
105108 return err
106109 }
107- } else {
108- // FIXME: Close the client connection, as it is not reusable???
109- pr .pool .Put (client )
110- // pr.pool.Put(NewClient("tcp", "localhost:5432", 4096))
111110 }
112111 } else {
113- return errors . New ( "client is not connected (disconnect)" )
112+ client . Close ( )
114113 }
115114
116115 logrus .Infof ("[D] There are %d clients in the pool" , len (pr .pool .ClientIDs ()))
@@ -144,11 +143,17 @@ func (pr *ProxyImpl) PassThrough(c gnet.Conn) error {
144143 // Receive the response from the server
145144 size , response , err := client .Receive ()
146145 if err != nil {
147- return err
148- }
149-
150- if size == 0 {
151- return errors .New ("no response from the server" )
146+ // FIXME: Is this the right way to handle this error?
147+ if err .Error () == "EOF" {
148+ logrus .Error ("The client is not connected to the server anymore" )
149+ // Either the client is not connected to the server anymore or
150+ // server forceful closed the connection
151+ // Reconnect the client
152+ client = pr .Reconnect (client )
153+ // Store the client in the map, replacing the old one
154+ pr .connClients .Store (c , client )
155+ }
156+ // return err
152157 }
153158
154159 // Write the response to the incoming connection
@@ -157,6 +162,14 @@ func (pr *ProxyImpl) PassThrough(c gnet.Conn) error {
157162 return nil
158163}
159164
165+ func (pr * ProxyImpl ) Reconnect (cl * Client ) * Client {
166+ // Close the client
167+ if cl != nil && cl .ID != "" {
168+ cl .Close ()
169+ }
170+ return NewClient ("tcp" , "localhost:5432" , 4096 )
171+ }
172+
160173func (pr * ProxyImpl ) Shutdown () {
161174 pr .pool .Shutdown ()
162175 logrus .Info ("All busy client connections have been closed" )
0 commit comments