@@ -107,6 +107,8 @@ type Client struct {
107107 log * logging.Logger
108108 requestTimeout time.Duration
109109 keyTTL int64
110+ active bool
111+ leaseID clientv3.LeaseID
110112}
111113
112114// Host represents the state of a host.
@@ -172,6 +174,7 @@ func NewEtcdClient(config *Config, log *logging.Logger) (*Client, error) {
172174 log : log ,
173175 requestTimeout : 2 * time .Second ,
174176 keyTTL : keyTTL ,
177+ active : true ,
175178 }, nil
176179}
177180
@@ -205,35 +208,50 @@ func (c *Client) GetDestination(key string) (string, error) {
205208}
206209
207210// SetDestination set current destination in etcd.
208- func (c * Client ) SetDestination (rootctx context.Context , key , sshdHostport string , dst string ) (<- chan * clientv3.LeaseKeepAliveResponse , string , clientv3. LeaseID , error ) {
211+ func (c * Client ) SetDestination (rootctx context.Context , key , sshdHostport string , dst string ) (<- chan * clientv3.LeaseKeepAliveResponse , string , error ) {
209212 path := fmt .Sprintf ("%s/%s/%s/%s" , toConnectionKey (key ), dst , sshdHostport , time .Now ().Format (time .RFC3339Nano ))
210213 ctx , cancel := context .WithTimeout (context .Background (), c .requestTimeout )
211214 resp , err := c .cli .Grant (ctx , c .keyTTL )
212215 cancel ()
213216 if err != nil {
214- return nil , "" , 0 , err
217+ return nil , "" , err
215218 }
216219
217220 bytes , err := json .Marshal (& Bandwidth {
218221 In : 0 ,
219222 Out : 0 ,
220223 })
221224 if err != nil {
222- return nil , "" , 0 , err
225+ return nil , "" , err
223226 }
224227 ctx , cancel = context .WithTimeout (context .Background (), c .requestTimeout )
225228 _ , err = c .cli .Put (ctx , path , string (bytes ), clientv3 .WithLease (resp .ID ))
226229 cancel ()
227230 if err != nil {
228- return nil , "" , 0 , err
231+ return nil , "" , err
229232 }
230233
231234 k , e := c .cli .KeepAlive (rootctx , resp .ID )
232- return k , path , resp .ID , e
235+ c .leaseID = resp .ID
236+ return k , path , e
237+ }
238+
239+ // NewLease creates a new lease in etcd.
240+ func (c * Client ) NewLease (rootctx context.Context ) (<- chan * clientv3.LeaseKeepAliveResponse , error ) {
241+ ctx , cancel := context .WithTimeout (context .Background (), c .requestTimeout )
242+ resp , err := c .cli .Grant (ctx , c .keyTTL )
243+ cancel ()
244+ if err != nil {
245+ return nil , err
246+ }
247+
248+ k , e := c .cli .KeepAlive (rootctx , resp .ID )
249+ c .leaseID = resp .ID
250+ return k , e
233251}
234252
235253// UpdateStats updates the stats (bandwidth in and out in kB/s) of a connection.
236- func (c * Client ) UpdateStats (etcdPath string , stats map [int ]uint64 , leaseID clientv3. LeaseID ) error {
254+ func (c * Client ) UpdateStats (etcdPath string , stats map [int ]uint64 ) error {
237255 bytes , err := json .Marshal (& Bandwidth {
238256 In : int (stats [0 ] / 1024 ),
239257 Out : int ((stats [1 ] + stats [2 ]) / 1024 ),
@@ -243,7 +261,7 @@ func (c *Client) UpdateStats(etcdPath string, stats map[int]uint64, leaseID clie
243261 }
244262
245263 ctx , cancel := context .WithTimeout (context .Background (), c .requestTimeout )
246- _ , err = c .cli .Put (ctx , etcdPath , string (bytes ), clientv3 .WithLease (leaseID ))
264+ _ , err = c .cli .Put (ctx , etcdPath , string (bytes ), clientv3 .WithLease (c . leaseID ))
247265 cancel ()
248266 if err != nil {
249267 return err
@@ -567,5 +585,15 @@ func (c *Client) GetAllGroups(allFlag bool) (map[string]*FlatGroup, error) {
567585
568586// IsAlive checks if etcd client is still usable.
569587func (c * Client ) IsAlive () bool {
570- return c .cli != nil
588+ return c .cli != nil && c .active == true
589+ }
590+
591+ // Enable enables the etcd client.
592+ func (c * Client ) Enable () {
593+ c .active = true
594+ }
595+
596+ // Disable disables the etcd client.
597+ func (c * Client ) Disable () {
598+ c .active = false
571599}
0 commit comments