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

Method processSyncWatch

server/sync_watcher.go:247–288  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

245// Returns any error or context cancel.
246// This method gets a lock on the sync backend and returns with an error when fails.
247func (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
290func (watcher *SyncWatcher) watchExtensionHandler(response *gohan_sync.Event) {
291 defer l.Panic(log)

Callers 1

runSyncWatchesMethod · 0.95

Calls 7

fetchStoredRevisionMethod · 0.95
watchExtensionHandlerMethod · 0.95
storeRevisionMethod · 0.95
LockMethod · 0.65
UnlockMethod · 0.65
WatchContextMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected