File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ package grpcpool
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "time"
7+
8+ pb "github.com/PythonHacker24/linux-acl-management-backend/proto"
9+ "go.uber.org/zap"
10+ "google.golang.org/grpc"
11+ )
12+
13+ /* monitor gRPC connections */
14+ func (p * ClientPool ) MonitorHealth (addr string , conn * grpc.ClientConn , errCh chan <- error ) {
15+ /* TODO: make it configurable */
16+ ticker := time .NewTicker (10 * time .Second )
17+ defer ticker .Stop ()
18+
19+ for {
20+ select {
21+ case <- p .stopCh :
22+ return
23+ case <- ticker .C :
24+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
25+ pingClient := pb .NewPingServiceClient (conn )
26+ _ , err := pingClient .Ping (ctx , & pb.PingRequest {})
27+ cancel ()
28+
29+ if err != nil {
30+ errCh <- fmt .Errorf ("ping failed for daemon at %s: %w" , addr , err )
31+
32+ p .mu .Lock ()
33+ conn .Close ()
34+ delete (p .conns , addr )
35+ p .mu .Unlock ()
36+
37+ return
38+ } else {
39+ zap .L ().Info ("Ping success" ,
40+ zap .String ("Address" , addr ),
41+ )
42+ }
43+ }
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments