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

Method Run

server/sync_watcher.go:89–132  ·  view source on GitHub ↗

Run starts the main loop of the watcher. This method blocks until the ctx is canceled by the caller

(ctx context.Context)

Source from the content-addressed store, hash-verified

87// Run starts the main loop of the watcher.
88// This method blocks until the ctx is canceled by the caller
89func (watcher *SyncWatcher) Run(ctx context.Context) error {
90 for {
91 err := func() error {
92 // register self process to the cluster
93 lockKey := processPathPrefix + "/" + watcher.sync.GetProcessID()
94 lost, err := watcher.sync.Lock(lockKey, true)
95 if err != nil {
96 return err
97 }
98 defer watcher.sync.Unlock(lockKey)
99
100 watchCtx, watchCancel := context.WithCancel(ctx)
101 defer watchCancel()
102 events := watcher.sync.WatchContext(watchCtx, processPathPrefix, int64(gohan_sync.RevisionCurrent))
103 watchErr := make(chan error, 1)
104 go func() {
105 watchErr <- watcher.processWatchLoop(events)
106 }()
107
108 select {
109 case err := <-watchErr:
110 return err
111 case <-ctx.Done():
112 <-watchErr
113 return nil
114 case <-lost:
115 watchCancel()
116 <-watchErr
117 return fmt.Errorf("lock is lost")
118 }
119 }()
120
121 if err != nil {
122 log.Error("process watch intrupted: %s", err)
123 }
124
125 select {
126 case <-time.After(watcher.backoff):
127 continue
128 case <-ctx.Done():
129 return ctx.Err()
130 }
131 }
132}
133
134// processWatchLoop handles events from the watch on the process list.
135// When this method detects a change, spawns new goroutines for sync event handling.

Callers 1

RunServerFunction · 0.95

Calls 7

processWatchLoopMethod · 0.95
GetProcessIDMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65
WatchContextMethod · 0.65
ErrorfMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected