@@ -13,6 +13,7 @@ type Proxy interface {
13
13
Connect (c gnet.Conn ) error
14
14
Disconnect (c gnet.Conn ) error
15
15
PassThrough (c gnet.Conn ) error
16
+ Reconnect (cl * Client ) * Client
16
17
Shutdown ()
17
18
Size () int
18
19
}
@@ -92,25 +93,23 @@ func (pr *ProxyImpl) Connect(c gnet.Conn) error {
92
93
93
94
func (pr * ProxyImpl ) Disconnect (c gnet.Conn ) error {
94
95
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 )
97
98
}
98
99
pr .connClients .Delete (c )
99
100
100
101
// TODO: The connection is unstable when I put the client back in the pool
101
102
// 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 != "" {
104
107
if err := pr .pool .Put (client ); err != nil {
105
108
return err
106
109
}
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))
111
110
}
112
111
} else {
113
- return errors . New ( "client is not connected (disconnect)" )
112
+ client . Close ( )
114
113
}
115
114
116
115
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 {
144
143
// Receive the response from the server
145
144
size , response , err := client .Receive ()
146
145
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
152
157
}
153
158
154
159
// Write the response to the incoming connection
@@ -157,6 +162,14 @@ func (pr *ProxyImpl) PassThrough(c gnet.Conn) error {
157
162
return nil
158
163
}
159
164
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
+
160
173
func (pr * ProxyImpl ) Shutdown () {
161
174
pr .pool .Shutdown ()
162
175
logrus .Info ("All busy client connections have been closed" )
0 commit comments