MCPcopy Create free account
hub / github.com/cloudwan/gohan / Lock

Method Lock

sync/etcd/etcd.go:127–161  ·  view source on GitHub ↗

Lock locks resources on sync This call blocks until you can get lock

(path string, block bool)

Source from the content-addressed store, hash-verified

125// Lock locks resources on sync
126// This call blocks until you can get lock
127func (s *Sync) Lock(path string, block bool) (chan struct{}, error) {
128 for {
129 _, err := s.etcdClient.Create(path, s.processID, masterTTL)
130 if err != nil {
131 log.Notice("failed to lock path %s: %s", path, err)
132 s.locks.Set(path, false)
133 if !block {
134 return nil, err
135 }
136 time.Sleep(masterTTL * time.Second)
137 continue
138 }
139 s.locks.Set(path, true)
140 log.Info("Locked %s", path)
141
142 //Refresh master token
143 lost := make(chan struct{})
144 go func() {
145 defer s.abortLock(path)
146 defer close(lost)
147
148 for s.HasLock(path) {
149 _, err := s.etcdClient.CompareAndSwap(
150 path, s.processID, masterTTL, s.processID, 0)
151 if err != nil {
152 log.Notice("failed to keepalive lock for %s %s", path, err)
153 s.locks.Set(path, false)
154 return
155 }
156 time.Sleep(masterTTL / 2 * time.Second)
157 }
158 }()
159 return lost, nil
160 }
161}
162
163func (s *Sync) abortLock(path string) {
164 s.locks.Set(path, false)

Callers

nothing calls this directly

Implementers 5

Syncsync/etcd/etcd.go
MockSyncsync/mocks/sync_mock.go
MockSyncMockRecordersync/mocks/sync_mock.go
Syncsync/noop/noop.go
Syncsync/etcdv3/etcd.go

Calls 6

abortLockMethod · 0.95
HasLockMethod · 0.95
CreateMethod · 0.65
NoticeMethod · 0.65
SetMethod · 0.65
InfoMethod · 0.65

Tested by

no test coverage detected