Lock locks resources on sync This call blocks until you can get lock
(path string, block bool)
| 125 | // Lock locks resources on sync |
| 126 | // This call blocks until you can get lock |
| 127 | func (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 | |
| 163 | func (s *Sync) abortLock(path string) { |
| 164 | s.locks.Set(path, false) |
nothing calls this directly
no test coverage detected