MonitorHealth monitors the health of the connection via Echo. This function blocks forever.
()
| 290 | |
| 291 | // MonitorHealth monitors the health of the connection via Echo. This function blocks forever. |
| 292 | func (p *Pool) MonitorHealth() { |
| 293 | defer p.closer.Done() |
| 294 | |
| 295 | // We might have lost connection to the destination. In that case, re-dial |
| 296 | // the connection. |
| 297 | // Returns true, if reconnection was successful |
| 298 | reconnect := func() bool { |
| 299 | reconnectionTicker := time.Tick(time.Second) |
| 300 | for { |
| 301 | select { |
| 302 | case <-p.closer.HasBeenClosed(): |
| 303 | glog.Infof("CONN: Returning from MonitorHealth for %s", p.Addr) |
| 304 | return false |
| 305 | case <-reconnectionTicker: |
| 306 | } |
| 307 | |
| 308 | if err := p.closer.Ctx().Err(); err != nil { |
| 309 | return false |
| 310 | } |
| 311 | ctx, cancel := context.WithTimeout(p.closer.Ctx(), 10*time.Second) |
| 312 | conn, err := grpc.NewClient(p.Addr, p.dialOpts...) |
| 313 | if err == nil { |
| 314 | // Make a dummy request to test out the connection. |
| 315 | client := pb.NewRaftClient(conn) |
| 316 | _, err = client.IsPeer(ctx, &pb.RaftContext{}) |
| 317 | } |
| 318 | cancel() |
| 319 | if err == nil { |
| 320 | p.Lock() |
| 321 | if err := p.conn.Close(); err != nil { |
| 322 | glog.Warningf("error while closing connection: %v", err) |
| 323 | } |
| 324 | p.conn = conn |
| 325 | p.Unlock() |
| 326 | return true |
| 327 | } |
| 328 | glog.Errorf("CONN: Unable to connect with %s : %s\n", p.Addr, err) |
| 329 | if conn != nil { |
| 330 | if err := conn.Close(); err != nil { |
| 331 | glog.Warningf("error while closing connection: %v", err) |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | ticker := time.Tick(time.Second) |
| 338 | for { |
| 339 | select { |
| 340 | case <-p.closer.HasBeenClosed(): |
| 341 | glog.Infof("CONN: Returning from MonitorHealth for %s", p.Addr) |
| 342 | return |
| 343 | case <-ticker: |
| 344 | } |
| 345 | |
| 346 | err := p.listenToHeartbeat() |
| 347 | if err != nil { |
| 348 | if reconnect() { |
| 349 | glog.Infof("CONN: Re-established connection with %s.\n", p.Addr) |