File: client.go

package info (click to toggle)
golang-k8s-sigs-apiserver-network-proxy 0.33.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: makefile: 220; sh: 118
file content (638 lines) | stat: -rw-r--r-- 20,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package agent

import (
	"context"
	"fmt"
	"io"
	"net"
	"os"
	runpprof "runtime/pprof"
	"strconv"
	"sync"
	"sync/atomic"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/connectivity"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"
	"k8s.io/klog/v2"

	commonmetrics "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/common/metrics"
	"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
	"sigs.k8s.io/apiserver-network-proxy/pkg/agent/metrics"
	"sigs.k8s.io/apiserver-network-proxy/proto/agent"
	"sigs.k8s.io/apiserver-network-proxy/proto/header"
)

const dialTimeout = 5 * time.Second

// endpointConn tracks a connection from agent to node network.
type endpointConn struct {
	conn      net.Conn
	connID    int64
	cleanFunc func()
	dataCh    chan []byte
	cleanOnce sync.Once
	warnChLim bool
	dialDone  chan struct{}
}

func (e *endpointConn) cleanup() {
	e.cleanOnce.Do(e.cleanFunc)
}

func (e *endpointConn) send(msg []byte) {
	// TODO (cheftako@): Get perf test working and compare this solution with a lock based solution.
	defer func() {
		// Handles the race condition where we write to a closed channel
		if err := recover(); err != nil {
			klog.InfoS("Recovered from attempt to write to closed channel")
		}
	}()
	if e.warnChLim && len(e.dataCh) >= cap(e.dataCh) {
		klog.V(2).InfoS("Data channel on agent is full", "connectionID", e.connID)
	}

	e.dataCh <- msg
}

type connectionManager struct {
	mu          sync.RWMutex
	connections map[int64]*endpointConn
}

func (cm *connectionManager) Add(connID int64, eConn *endpointConn) {
	cm.mu.Lock()
	defer cm.mu.Unlock()
	metrics.Metrics.EndpointConnectionInc()
	cm.connections[connID] = eConn
}

func (cm *connectionManager) Get(connID int64) (*endpointConn, bool) {
	cm.mu.RLock()
	defer cm.mu.RUnlock()
	eConn, ok := cm.connections[connID]
	return eConn, ok
}

func (cm *connectionManager) Delete(connID int64) {
	cm.mu.Lock()
	defer cm.mu.Unlock()
	// Delete for a connID is called from cleanFunc, which is
	// protected by cleanOnce.
	metrics.Metrics.EndpointConnectionDec()
	delete(cm.connections, connID)
}

func (cm *connectionManager) List() []*endpointConn {
	cm.mu.RLock()
	defer cm.mu.RUnlock()
	endpointConns := make([]*endpointConn, 0, len(cm.connections))
	for _, eConn := range cm.connections {
		endpointConns = append(endpointConns, eConn)
	}
	return endpointConns
}

func newConnectionManager() *connectionManager {
	return &connectionManager{
		connections: make(map[int64]*endpointConn),
	}
}

// Client runs on the node network side. It connects to proxy server and establishes
// a stream connection from which it sends and receives network traffic.
type Client struct {
	nextConnID int64

	connManager *connectionManager

	cs *ClientSet // the clientset that includes this AgentClient.

	stream           agent.AgentService_ConnectClient
	agentID          string
	agentIdentifiers string
	serverID         string // the id of the proxy server this client connects to.

	// connect opts
	address string
	opts    []grpc.DialOption
	conn    *grpc.ClientConn

	drainCh   <-chan struct{}
	drainOnce sync.Once
	stopCh    chan struct{}

	// locks
	sendLock      sync.Mutex
	recvLock      sync.Mutex
	probeInterval time.Duration // interval between probe pings

	// file path contains service account token.
	// token's value is auto-rotated by kubernetes, based on projected volume configuration.
	serviceAccountTokenPath string

	warnOnChannelLimit bool
}

func newAgentClient(address, agentID, agentIdentifiers string, cs *ClientSet, opts ...grpc.DialOption) (*Client, int, error) {
	a := &Client{
		cs:                      cs,
		address:                 address,
		agentID:                 agentID,
		agentIdentifiers:        agentIdentifiers,
		opts:                    opts,
		probeInterval:           cs.probeInterval,
		drainCh:                 cs.drainCh,
		stopCh:                  make(chan struct{}),
		serviceAccountTokenPath: cs.serviceAccountTokenPath,
		connManager:             newConnectionManager(),
		warnOnChannelLimit:      cs.warnOnChannelLimit,
	}
	serverCount, err := a.Connect()
	if err != nil {
		return nil, 0, err
	}
	return a, serverCount, nil
}

// Connect makes the grpc dial to the proxy server. It returns the count
// of the number of servers this agent should connected to.
func (a *Client) Connect() (int, error) {
	conn, err := grpc.Dial(a.address, a.opts...)
	if err != nil {
		return 0, err
	}
	ctx := metadata.AppendToOutgoingContext(context.Background(),
		header.AgentID, a.agentID,
		header.AgentIdentifiers, a.agentIdentifiers)
	if a.serviceAccountTokenPath != "" {
		if ctx, err = a.initializeAuthContext(ctx); err != nil {
			err := conn.Close()
			if err != nil {
				klog.ErrorS(err, "failed to close gRPC connection", "agentID", a.agentID)
			}
			return 0, err
		}
	}
	stream, err := agent.NewAgentServiceClient(conn).Connect(ctx)
	if err != nil {
		conn.Close() /* #nosec G104 */
		return 0, err
	}
	serverID, err := serverID(stream)
	if err != nil {
		conn.Close() /* #nosec G104 */
		return 0, err
	}
	serverCount, err := serverCount(stream)
	if err != nil {
		conn.Close() /* #nosec G104 */
		return 0, err
	}
	a.conn = conn
	a.stream = stream
	a.serverID = serverID
	klog.V(2).InfoS("Connect to server", "serverID", serverID)
	return serverCount, nil
}

// Close closes the Connect gRPC connection.
func (a *Client) Close() {
	if a.conn == nil {
		klog.Errorln("Unexpected empty AgentClient.conn")
	}
	err := a.conn.Close()
	if err != nil {
		klog.ErrorS(err, "failed to close gRPC connection", "serverID", a.serverID, "agentID", a.agentID)
	}
	close(a.stopCh)
}

func (a *Client) Send(pkt *client.Packet) error {
	a.sendLock.Lock()
	defer a.sendLock.Unlock()

	const segment = commonmetrics.SegmentFromAgent
	metrics.Metrics.ObservePacket(segment, pkt.Type)
	err := a.stream.Send(pkt)
	if err != nil && err != io.EOF {
		metrics.Metrics.ObserveServerFailureDeprecated(metrics.DirectionToServer)
		metrics.Metrics.ObserveStreamError(segment, err, pkt.Type)
		a.cs.RemoveClient(a.serverID)
	}
	return err
}

func (a *Client) Recv() (*client.Packet, error) {
	a.recvLock.Lock()
	defer a.recvLock.Unlock()

	const segment = commonmetrics.SegmentToAgent
	pkt, err := a.stream.Recv()
	if err != nil {
		if err != io.EOF {
			metrics.Metrics.ObserveServerFailureDeprecated(metrics.DirectionFromServer)
			metrics.Metrics.ObserveStreamErrorNoPacket(segment, err)
		}
		return nil, err
	}
	metrics.Metrics.ObservePacket(segment, pkt.Type)
	return pkt, nil
}

func serverCount(stream agent.AgentService_ConnectClient) (int, error) {
	md, err := stream.Header()
	if err != nil {
		return 0, err
	}
	scounts := md.Get(header.ServerCount)
	if len(scounts) != 1 {
		return 0, fmt.Errorf("expected one server count, got %d", len(scounts))
	}
	scount := scounts[0]
	return strconv.Atoi(scount)
}

func serverID(stream agent.AgentService_ConnectClient) (string, error) {
	// TODO: this is a blocking call. Add a timeout?
	md, err := stream.Header()
	if err != nil {
		return "", err
	}
	sids := md.Get(header.ServerID)
	if len(sids) != 1 {
		return "", fmt.Errorf("expected one server ID in the context, got %v", sids)
	}
	return sids[0], nil
}

func (a *Client) initializeAuthContext(ctx context.Context) (context.Context, error) {
	var err error
	var b []byte

	// load current service account's token value
	if b, err = os.ReadFile(a.serviceAccountTokenPath); err != nil {
		klog.ErrorS(err, "Failed to read token", "path", a.serviceAccountTokenPath)
		return nil, err
	}
	ctx = metadata.AppendToOutgoingContext(ctx, header.AuthenticationTokenContextKey, header.AuthenticationTokenContextSchemePrefix+string(b))

	return ctx, nil
}

// Connect connects to proxy server to establish a gRPC stream,
// on which the proxied traffic is multiplexed through the stream
// and piped to the local connection. It register itself as a
// backend from proxy server, so proxy server will route traffic
// to this agent.
//
// The caller needs to call Serve to start serving proxy requests
// coming from proxy server.

// Serve starts to serve proxied requests from proxy server over the
// gRPC stream. Successful Connect is required before Serve. The
// The requests include things like opening a connection to a server,
// streaming data and close the connection.
func (a *Client) Serve() {
	defer a.cs.RemoveClient(a.serverID)
	defer func() {
		// close all of conns with remote when Client exits
		for _, eConn := range a.connManager.List() {
			eConn.cleanup()
		}
		klog.V(2).InfoS("cleanup all of conn contexts when client exits")
	}()

	klog.V(2).InfoS("Start serving", "serverID", a.serverID, "agentID", a.agentID)
	go a.probe()
	for {
		select {
		case <-a.stopCh:
			klog.V(2).InfoS("stop agent client.")
			return
		case <-a.drainCh:
			a.drainOnce.Do(func() {
				klog.V(2).InfoS("drain agent client", "serverID", a.serverID, "agentID", a.agentID)
				drainPkt := &client.Packet{
					Type: client.PacketType_DRAIN,
					Payload: &client.Packet_Drain{
						Drain: &client.Drain{},
					},
				}
				if err := a.Send(drainPkt); err != nil {
					klog.ErrorS(err, "drain failure", "")
				}
			})
		default:
		}

		pkt, err := a.Recv()
		if err != nil {
			if err == io.EOF {
				klog.V(2).InfoS("received EOF, exit", "serverID", a.serverID, "agentID", a.agentID)
				return
			}
			if status.Code(err) == codes.Canceled {
				klog.V(2).InfoS("stream canceled", "serverID", a.serverID, "agentID", a.agentID)
			} else {
				klog.ErrorS(err, "could not read stream", "serverID", a.serverID, "agentID", a.agentID)
			}
			return
		}

		if pkt == nil {
			klog.V(3).InfoS("empty packet received")
			continue
		}

		klog.V(5).InfoS("[tracing] recv packet", "type", pkt.Type)
		switch pkt.Type {
		case client.PacketType_DIAL_REQ:
			dialReq := pkt.GetDialRequest()
			klog.V(3).InfoS("Received DIAL_REQ", "serverID", a.serverID, "agentID", a.agentID, "dialID", dialReq.Random, "dialAddress", dialReq.Address)
			dialResp := &client.Packet{
				Type:    client.PacketType_DIAL_RSP,
				Payload: &client.Packet_DialResponse{DialResponse: &client.DialResponse{}},
			}
			dialResp.GetDialResponse().Random = dialReq.Random

			connID := atomic.AddInt64(&a.nextConnID, 1)
			dataCh := make(chan []byte, a.cs.xfrChannelSize)
			dialDone := make(chan struct{})
			eConn := &endpointConn{
				dataCh:    dataCh,
				dialDone:  dialDone,
				warnChLim: a.warnOnChannelLimit,
			}
			eConn.cleanFunc = func() {
				// block on purpose
				<-dialDone
				if eConn.conn == nil {
					// TODO: move this guard lower
					klog.ErrorS(fmt.Errorf("remote connection is nil"), "could not send CLOSE_RESP to nil connection")
					return
				}
				klog.V(4).InfoS("close connection", "dialID", dialReq.Random, "connectionID", connID, "dialAddress", dialReq.Address)
				var closePkt *client.Packet
				if connID == 0 {
					closePkt = &client.Packet{
						Type:    client.PacketType_DIAL_CLS,
						Payload: &client.Packet_CloseDial{CloseDial: &client.CloseDial{}},
					}
					closePkt.GetCloseDial().Random = dialReq.Random
				} else {
					closePkt = &client.Packet{
						Type:    client.PacketType_CLOSE_RSP,
						Payload: &client.Packet_CloseResponse{CloseResponse: &client.CloseResponse{}},
					}
					closePkt.GetCloseResponse().ConnectID = connID
				}
				if err := a.Send(closePkt); err != nil {
					klog.ErrorS(err, "close response failure", "")
				}
				close(dataCh)
				a.connManager.Delete(connID)
				if err := eConn.conn.Close(); err != nil {
					klog.ErrorS(err, "failed to close connection to remote", "dialID", dialReq.Random, "connectionID", connID)
				}
			}
			labels := runpprof.Labels(
				"agentID", a.agentID,
				"agentIdentifiers", a.agentIdentifiers,
				"serverAddress", a.address,
				"serverID", a.serverID,
				"dialID", strconv.FormatInt(dialReq.Random, 10),
				"dialAddress", dialReq.Address,
			)
			go runpprof.Do(context.Background(), labels, func(context.Context) {
				defer close(dialDone)
				start := time.Now()
				conn, err := net.DialTimeout(dialReq.Protocol, dialReq.Address, dialTimeout)
				if err != nil {
					reason := metrics.DialFailureUnknown
					if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
						reason = metrics.DialFailureTimeout
					}
					metrics.Metrics.ObserveDialFailure(reason)
					// Do not log agent errors for remote unavailable.
					klog.V(1).InfoS("error dialing backend", "error", err, "dialID", dialReq.Random, "connectionID", connID, "dialAddress", dialReq.Address)
					dialResp.GetDialResponse().Error = err.Error()
					if err := a.Send(dialResp); err != nil {
						klog.ErrorS(err, "could not send DIAL_RSP with error", "dialID", dialReq.Random, "connectionID", connID, "dialAddress", dialReq.Address)
					}
					// Cannot invoke clean up as we have no conn yet.
					return
				}
				metrics.Metrics.ObserveDialLatency(time.Since(start))
				klog.V(3).InfoS("Endpoint connection established", "dialID", dialReq.Random, "connectionID", connID, "dialAddress", dialReq.Address)
				eConn.conn = conn
				a.connManager.Add(connID, eConn)
				dialResp.GetDialResponse().ConnectID = connID
				labels := runpprof.Labels(
					"agentID", a.agentID,
					"agentIdentifiers", a.agentIdentifiers,
					"serverAddress", a.address,
					"serverID", a.serverID,
					"connectionID", strconv.FormatInt(connID, 10),
					"dialAddress", dialReq.Address,
				)
				if err := a.Send(dialResp); err != nil {
					klog.ErrorS(err, "could not send DIAL_RSP", "dialID", dialReq.Random, "connectionID", connID, "dialAddress", dialReq.Address)
					// clean-up is normally called from remoteToProxy which we will never invoke.
					// So we are invoking it here to force the clean-up to occur.
					// However, cleanup will block until dialDone is closed.
					// So placing cleanup on its own goroutine to wait for the deferred close(dialDone) to kick in.
					go runpprof.Do(context.Background(), labels, func(context.Context) { eConn.cleanup() })
					return
				}
				go runpprof.Do(context.Background(), labels, func(context.Context) { a.remoteToProxy(connID, eConn) })
				go runpprof.Do(context.Background(), labels, func(context.Context) { a.proxyToRemote(connID, eConn) })
			})

		case client.PacketType_DATA:
			data := pkt.GetData()
			klog.V(4).InfoS("received DATA", "connectionID", data.ConnectID)
			if data.ConnectID == 0 {
				klog.ErrorS(nil, "Received packet missing ConnectID from frontend", "packetType", "DATA")
				continue
			}

			eConn, ok := a.connManager.Get(data.ConnectID)
			if ok {
				eConn.send(data.Data)
			} else {
				klog.V(2).InfoS("received DATA for unrecognized connection", "connectionID", data.ConnectID)
				a.Send(&client.Packet{
					Type: client.PacketType_CLOSE_RSP,
					Payload: &client.Packet_CloseResponse{
						CloseResponse: &client.CloseResponse{
							ConnectID: data.ConnectID,
							Error:     "unrecognized connectID",
						},
					},
				})
				continue
			}

		case client.PacketType_CLOSE_REQ:
			closeReq := pkt.GetCloseRequest()
			connID := closeReq.ConnectID

			klog.V(4).InfoS("received CLOSE_REQ", "connectionID", connID)

			eConn, ok := a.connManager.Get(connID)
			if ok {
				eConn.cleanup()
			} else {
				klog.V(4).InfoS("Failed to find connection context for close", "connectionID", connID)
				resp := &client.Packet{
					Type:    client.PacketType_CLOSE_RSP,
					Payload: &client.Packet_CloseResponse{CloseResponse: &client.CloseResponse{}},
				}
				resp.GetCloseResponse().ConnectID = connID
				resp.GetCloseResponse().Error = "Unknown connectID"
				if err := a.Send(resp); err != nil {
					klog.ErrorS(err, "could not send CLOSE_RSP", err, "connectionID", connID)
					continue
				}
			}

		default:
			klog.V(5).InfoS("unrecognized packet", "type", pkt)
		}
	}
}

func (a *Client) remoteToProxy(connID int64, eConn *endpointConn) {
	defer func() {
		if panicInfo := recover(); panicInfo != nil {
			klog.V(2).InfoS("Exiting remoteToProxy with recovery", "panicInfo", panicInfo, "connectionID", connID)
		} else {
			klog.V(4).InfoS("Exiting remoteToProxy", "connectionID", connID)
		}
	}()
	defer eConn.cleanup()

	var buf [1 << 12]byte
	resp := &client.Packet{
		Type: client.PacketType_DATA,
	}

	for {
		n, err := eConn.conn.Read(buf[:])
		klog.V(5).InfoS("received data from remote", "bytes", n, "connectionID", connID)

		if err == io.EOF {
			klog.V(2).InfoS("remote connection EOF", "connectionID", connID)
			return
		} else if err != nil {
			// "use of closed network connection" errors are expected upon receiving CLOSE_REQ
			// If connID doesn't exist in connManager, we assume the connection was meant to be closed.
			if _, ok := a.connManager.Get(connID); !ok {
				klog.V(5).InfoS("reading from a closed connection", "connectionID", connID, "err", err)
			} else {
				klog.ErrorS(err, "connection read failure", "connectionID", connID)
			}
			return
		}

		resp.Payload = &client.Packet_Data{Data: &client.Data{
			Data:      buf[:n],
			ConnectID: connID,
		}}
		if err := a.Send(resp); err != nil {
			klog.ErrorS(err, "could not send DATA", "connectionID", connID)
		}
	}
}

func (a *Client) proxyToRemote(connID int64, eConn *endpointConn) {
	defer func() {
		if panicInfo := recover(); panicInfo != nil {
			klog.V(2).InfoS("Exiting proxyToRemote with recovery", "panicInfo", panicInfo, "connectionID", connID)
		} else {
			klog.V(4).InfoS("Exiting proxyToRemote", "connectionID", connID)
		}
	}()
	// Not safe to call cleanup here, as cleanup() closes the dataCh
	// and we are the receiver for the dataCh. Also we now have a later
	// defer which will block until dataCh is closed.
	defer func() {
		// As the read side of the dataCh channel, we cannot close it.
		// However serve() may be blocked writing to the channel,
		// so we need to consume the channel until it is closed.
		discardedPktCount := 0
		for range eConn.dataCh {
			// Ignore values as this indicates there was a problem
			// with the remote connection.
			discardedPktCount++
		}
		if discardedPktCount > 0 {
			klog.V(2).InfoS("Discard packets while exiting proxyToRemote", "pktCount", discardedPktCount, "connectionID", connID)
		}
	}()

	for d := range eConn.dataCh {
		pos := 0
		for {
			n, err := eConn.conn.Write(d[pos:])
			if err == nil {
				klog.V(4).InfoS("write to remote", "connectionID", connID, "lastData", n, "dataSize", len(d))
				break
			} else if n > 0 {
				// https://golang.org/pkg/io/#Writer specifies return non nil error if n < len(d)
				klog.ErrorS(err, "write to remote with failure", "connectionID", connID, "lastData", n)
				pos += n
			} else {
				// "use of closed network connection" errors are expected upon receiving CLOSE_REQ
				// If connID doesn't exist in connManager, we assume the connection was meant to be closed.
				if _, ok := a.connManager.Get(connID); !ok {
					klog.V(5).InfoS("writing to a closed connection", "connectionID", connID, "err", err)
				} else {
					klog.ErrorS(err, "conn write failure", "connectionID", connID)
				}

				return
			}
		}
	}
}

func (a *Client) probe() {
	for {
		select {
		case <-a.stopCh:
			return
		case <-time.After(a.probeInterval):
			if a.conn == nil {
				continue
			}
			// health check
			if a.conn.GetState() == connectivity.Ready {
				continue
			}
		}
		klog.V(1).InfoS("Removing client used for server connection", "state", a.conn.GetState(), "serverID", a.serverID)
		a.cs.RemoveClient(a.serverID)
		return
	}
}