@@ -69,10 +69,22 @@ func (s *Store) getHealth(ctx context.Context, id uint64, name string, urls []st
69
69
func (s * Store ) GetClusterHealth (ctx context.Context , cluster clientv3.Cluster , etcdClientTLSConfig * tls.Config ) * corev2.HealthResponse {
70
70
healthResponse := & corev2.HealthResponse {}
71
71
72
+ var timeout time.Duration
73
+ if val := ctx .Value ("timeout" ); val != nil {
74
+ timeout , _ = val .(time.Duration )
75
+ }
76
+
72
77
// Do a get op against every cluster member. Collect the memberIDs and
73
78
// op errors into a response map, and return this map as etcd health
74
79
// information.
75
- mList , err := cluster .MemberList (ctx )
80
+ tctx := ctx
81
+ if timeout > 0 {
82
+ var cancel context.CancelFunc
83
+ tctx , cancel = context .WithTimeout (ctx , timeout )
84
+ defer cancel ()
85
+ }
86
+
87
+ mList , err := cluster .MemberList (tctx )
76
88
if err != nil {
77
89
logger .WithError (err ).Error ("could not get the cluster member list" )
78
90
healthResponse .ClusterHealth = []* corev2.ClusterHealth {& corev2.ClusterHealth {
@@ -96,10 +108,13 @@ func (s *Store) GetClusterHealth(ctx context.Context, cluster clientv3.Cluster,
96
108
for _ , member := range mList .Members {
97
109
go func (id uint64 , name string , urls []string ) {
98
110
defer wg .Done ()
99
- select {
100
- case healths <- s .getHealth (ctx , id , name , urls , etcdClientTLSConfig ):
101
- case <- ctx .Done ():
111
+ tctx := ctx
112
+ if timeout > 0 {
113
+ var cancel context.CancelFunc
114
+ tctx , cancel = context .WithTimeout (ctx , timeout )
115
+ defer cancel ()
102
116
}
117
+ healths <- s .getHealth (tctx , id , name , urls , etcdClientTLSConfig )
103
118
}(member .ID , member .Name , member .ClientURLs )
104
119
}
105
120
@@ -112,7 +127,13 @@ func (s *Store) GetClusterHealth(ctx context.Context, cluster clientv3.Cluster,
112
127
return healthResponse .ClusterHealth [i ].Name < healthResponse .ClusterHealth [j ].Name
113
128
})
114
129
115
- alarmResponse , err := s .client .Maintenance .AlarmList (ctx )
130
+ if timeout > 0 {
131
+ var cancel context.CancelFunc
132
+ tctx , cancel = context .WithTimeout (ctx , timeout )
133
+ defer cancel ()
134
+ }
135
+
136
+ alarmResponse , err := s .client .Maintenance .AlarmList (tctx )
116
137
if err != nil {
117
138
logger .WithError (err ).Error ("failed to fetch etcd alarm list" )
118
139
} else {
0 commit comments