Skip to content

Commit d319141

Browse files
Monitor gRPC pool of connection to daemon
1 parent 407b071 commit d319141

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

internal/grpcpool/monitor.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)