|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package agentclient |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/rand" |
| 22 | + "sync" |
| 23 | + "time" |
| 24 | + |
| 25 | + "google.golang.org/grpc" |
| 26 | + "k8s.io/klog" |
| 27 | +) |
| 28 | + |
| 29 | +// ClientSet consists of clients connected to each instance of an HA proxy server. |
| 30 | +type ClientSet struct { |
| 31 | + mu sync.Mutex //protects the following |
| 32 | + clients map[string]*AgentClient // map between serverID and the client |
| 33 | + // connects to this server. |
| 34 | + |
| 35 | + agentID string // ID of this agent |
| 36 | + address string // proxy server address. Assuming HA proxy server |
| 37 | + serverCount int // number of proxy server instances, should be 1 |
| 38 | + // unless it is an HA server. Initialized when the ClientSet creates |
| 39 | + // the first client. |
| 40 | + syncInterval time.Duration // The interval by which the agent |
| 41 | + // periodically checks that it has connections to all instances of the |
| 42 | + // proxy server. |
| 43 | + dialOption grpc.DialOption |
| 44 | +} |
| 45 | + |
| 46 | +func (cs *ClientSet) clientsCount() int { |
| 47 | + cs.mu.Lock() |
| 48 | + defer cs.mu.Unlock() |
| 49 | + return len(cs.clients) |
| 50 | +} |
| 51 | + |
| 52 | +func (cs *ClientSet) hasIDLocked(serverID string) bool { |
| 53 | + _, ok := cs.clients[serverID] |
| 54 | + return ok |
| 55 | +} |
| 56 | + |
| 57 | +func (cs *ClientSet) HasID(serverID string) bool { |
| 58 | + cs.mu.Lock() |
| 59 | + defer cs.mu.Unlock() |
| 60 | + return cs.hasIDLocked(serverID) |
| 61 | +} |
| 62 | + |
| 63 | +func (cs *ClientSet) addClientLocked(serverID string, c *AgentClient) error { |
| 64 | + if cs.hasIDLocked(serverID) { |
| 65 | + return fmt.Errorf("client for proxy server %s already exists", serverID) |
| 66 | + } |
| 67 | + cs.clients[serverID] = c |
| 68 | + return nil |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +func (cs *ClientSet) AddClient(serverID string, c *AgentClient) error { |
| 73 | + cs.mu.Lock() |
| 74 | + defer cs.mu.Unlock() |
| 75 | + return cs.addClientLocked(serverID, c) |
| 76 | +} |
| 77 | + |
| 78 | +func (cs *ClientSet) RemoveClient(serverID string) { |
| 79 | + cs.mu.Lock() |
| 80 | + defer cs.mu.Unlock() |
| 81 | + delete(cs.clients, serverID) |
| 82 | +} |
| 83 | + |
| 84 | +func NewAgentClientSet(address, agentID string, syncInterval time.Duration, dialOption grpc.DialOption) *ClientSet { |
| 85 | + return &ClientSet{ |
| 86 | + clients: make(map[string]*AgentClient), |
| 87 | + agentID: agentID, |
| 88 | + address: address, |
| 89 | + syncInterval: syncInterval, |
| 90 | + dialOption: dialOption, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (cs *ClientSet) newAgentClient() (*AgentClient, error) { |
| 95 | + return newAgentClient(cs.address, cs.agentID, cs, cs.dialOption) |
| 96 | +} |
| 97 | + |
| 98 | +// sync makes sure that #clients >= #proxy servers |
| 99 | +func (cs *ClientSet) sync() { |
| 100 | + jitter := float64(0.2) |
| 101 | + for { |
| 102 | + if cs.serverCount != 0 { |
| 103 | + sleep := cs.syncInterval + time.Duration(rand.Float64()*jitter*float64(cs.syncInterval)) |
| 104 | + time.Sleep(sleep) |
| 105 | + } |
| 106 | + if cs.serverCount == 0 || cs.clientsCount() < cs.serverCount { |
| 107 | + c, err := cs.newAgentClient() |
| 108 | + if err != nil { |
| 109 | + klog.Error(err) |
| 110 | + continue |
| 111 | + } |
| 112 | + cs.serverCount = c.serverCount |
| 113 | + if err := cs.AddClient(c.serverID, c); err != nil { |
| 114 | + klog.Infof("closing connection: %v", err) |
| 115 | + c.Close() |
| 116 | + continue |
| 117 | + } |
| 118 | + klog.Infof("sync added client connecting to proxy server %s", c.serverID) |
| 119 | + go c.Serve() |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func (cs *ClientSet) Serve() { |
| 125 | + go cs.sync() |
| 126 | +} |
0 commit comments