processSyncWatch handles events on a path with a handler. Returns any error or context cancel. This method gets a lock on the sync backend and returns with an error when fails.
(ctx context.Context, path string)
| 245 | // Returns any error or context cancel. |
| 246 | // This method gets a lock on the sync backend and returns with an error when fails. |
| 247 | func (watcher *SyncWatcher) processSyncWatch(ctx context.Context, path string) error { |
| 248 | lockKey := lockPath + "/watch" + path |
| 249 | lost, err := watcher.sync.Lock(lockKey, false) |
| 250 | if err != nil { |
| 251 | return errLockFailed |
| 252 | } |
| 253 | defer watcher.sync.Unlock(lockKey) |
| 254 | |
| 255 | watchCtx, watchCancel := context.WithCancel(ctx) |
| 256 | defer watchCancel() |
| 257 | fromRevision := watcher.fetchStoredRevision(path) + 1 |
| 258 | respCh := watcher.sync.WatchContext(watchCtx, path, fromRevision) |
| 259 | watchErr := make(chan error, 1) |
| 260 | go func() { |
| 261 | watchErr <- func() error { |
| 262 | for response := range respCh { |
| 263 | if response.Err != nil { |
| 264 | return response.Err |
| 265 | } |
| 266 | watcher.watchExtensionHandler(response) |
| 267 | |
| 268 | err := watcher.storeRevision(path, response.Revision) |
| 269 | if err != nil { |
| 270 | return err |
| 271 | } |
| 272 | } |
| 273 | return nil |
| 274 | }() |
| 275 | }() |
| 276 | |
| 277 | select { |
| 278 | case <-ctx.Done(): |
| 279 | <-watchErr |
| 280 | return ctx.Err() |
| 281 | case <-lost: |
| 282 | watchCancel() |
| 283 | <-watchErr |
| 284 | return fmt.Errorf("lock for path `%s` is lost", path) |
| 285 | case err := <-watchErr: |
| 286 | return err |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | func (watcher *SyncWatcher) watchExtensionHandler(response *gohan_sync.Event) { |
| 291 | defer l.Panic(log) |
no test coverage detected