github.com/searKing/golang/go@v1.2.117/sync/leaderelection/resource_locker.go (about) 1 // Copyright 2021 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package leaderelection 6 7 import ( 8 "context" 9 "time" 10 ) 11 12 // Record is the record that is stored in the leader election annotation. 13 // This information should be used for observational purposes only and could be replaced 14 // with a random string (e.g. UUID) with only slight modification of this code. 15 type Record struct { 16 // HolderIdentity is the ID that owns the lease. If empty, no one owns this lease and 17 // all callers may acquire. 18 // This value is set to empty when a client voluntarily steps down. 19 HolderIdentity string 20 // LeaseDuration is the duration that non-leader candidates will 21 // wait to force acquire leadership. This is measured against time of 22 // last observed ack. 23 LeaseDuration time.Duration 24 AcquireTime time.Time // when the leader hold this record 25 RenewTime time.Time // when the locker is renewed recently 26 LeaderTransitions int // +1 if expired checked by followers, changed to trigger a new lock acquire or new 27 } 28 29 // ResourceLocker offers a common interface for locking on arbitrary 30 // resources used in leader election. The ResourceLocker is used 31 // to hide the details on specific implementations in order to allow 32 // them to change over time. This interface is strictly for use 33 // by the leaderelection code. 34 type ResourceLocker interface { 35 // Get returns the LeaderElectionRecord 36 // get locker's state 37 Get(ctx context.Context) (record *Record, rawRecord []byte, err error) 38 39 // Create attempts to create a LeaderElectionRecord 40 // return err if lock failed 41 // Lock 42 Create(ctx context.Context, ler Record) error 43 44 // Update will update and existing LeaderElectionRecord 45 // return err if lock failed 46 // Lock or UnLock 47 Update(ctx context.Context, ler Record) error 48 49 // RecordEvent is used to record events 50 RecordEvent(name, event string) 51 52 // Identity will return the locks Identity 53 Identity() string 54 55 // Describe is used to convert details on current resource lock 56 // into a string 57 Describe() string 58 }