From f1622129e4ebce40bf901322d53dc7b6a28a3373 Mon Sep 17 00:00:00 2001 From: Deshi Xiao Date: Mon, 1 Nov 2021 14:20:16 +0800 Subject: [PATCH] refactor: Use plain channel send or receive fix issue #4369 should use a simple channel send/receive instead of select with a single case Signed-off-by: Deshi Xiao --- pkg/agent/tunnel/tunnel.go | 64 ++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/pkg/agent/tunnel/tunnel.go b/pkg/agent/tunnel/tunnel.go index 0503152eaa..d14f284cdb 100644 --- a/pkg/agent/tunnel/tunnel.go +++ b/pkg/agent/tunnel/tunnel.go @@ -92,42 +92,40 @@ func Setup(ctx context.Context, config *config.Node, proxy proxy.Proxy) error { } watching: for { - select { - case ev, ok := <-watch.ResultChan(): - if !ok || ev.Type == watchtypes.Error { - if ok { - logrus.Errorf("Tunnel endpoint watch channel closed: %v", ev) - } - watch.Stop() - continue connect + ev, ok := <-watch.ResultChan() + if !ok || ev.Type == watchtypes.Error { + if ok { + logrus.Errorf("Tunnel endpoint watch channel closed: %v", ev) } - endpoint, ok := ev.Object.(*v1.Endpoints) - if !ok { - logrus.Errorf("Tunnel could not convert event object to endpoint: %v", ev) - continue watching + watch.Stop() + continue connect + } + endpoint, ok := ev.Object.(*v1.Endpoints) + if !ok { + logrus.Errorf("Tunnel could not convert event object to endpoint: %v", ev) + continue watching + } + + newAddresses := util.GetAddresses(endpoint) + if reflect.DeepEqual(newAddresses, proxy.SupervisorAddresses()) { + continue watching + } + proxy.Update(newAddresses) + + validEndpoint := map[string]bool{} + + for _, address := range proxy.SupervisorAddresses() { + validEndpoint[address] = true + if _, ok := disconnect[address]; !ok { + disconnect[address] = connect(ctx, nil, address, tlsConfig) } + } - newAddresses := util.GetAddresses(endpoint) - if reflect.DeepEqual(newAddresses, proxy.SupervisorAddresses()) { - continue watching - } - proxy.Update(newAddresses) - - validEndpoint := map[string]bool{} - - for _, address := range proxy.SupervisorAddresses() { - validEndpoint[address] = true - if _, ok := disconnect[address]; !ok { - disconnect[address] = connect(ctx, nil, address, tlsConfig) - } - } - - for address, cancel := range disconnect { - if !validEndpoint[address] { - cancel() - delete(disconnect, address) - logrus.Infof("Stopped tunnel to %s", address) - } + for address, cancel := range disconnect { + if !validEndpoint[address] { + cancel() + delete(disconnect, address) + logrus.Infof("Stopped tunnel to %s", address) } } }