Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit c305b9d

Browse files
committed
Improve dropped event calculation
1 parent ed23f64 commit c305b9d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

internal/sync/sync.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path"
1111
"path/filepath"
1212
"sync"
13+
"sync/atomic"
1314
"time"
1415

1516
"github.com/gorilla/websocket"
@@ -84,7 +85,7 @@ func (s Sync) initSync() error {
8485

8586
start := time.Now()
8687
// Delete old files on initial sync (e.g git checkout).
87-
err := s.syncPaths(true, s.LocalDir + "/.", s.RemoteDir)
88+
err := s.syncPaths(true, s.LocalDir+"/.", s.RemoteDir)
8889
if err == nil {
8990
flog.Info("finished initial sync (%v)", time.Since(start).Truncate(time.Millisecond))
9091
}
@@ -167,7 +168,6 @@ func (s Sync) work(ev timedEvent) {
167168
}
168169
}
169170

170-
171171
var ErrRestartSync = errors.New("the sync exited because it was overloaded, restart it")
172172

173173
// workEventGroup converges a group of events to prevent duplicate work.
@@ -239,14 +239,21 @@ func (s Sync) Run() error {
239239

240240
flog.Info("watching %s for changes", s.LocalDir)
241241

242+
var droppedEvents uint64
242243
// Timed events lets us track how long each individual file takes to update.
243244
timedEvents := make(chan timedEvent, cap(events))
244245
go func() {
245246
defer close(timedEvents)
246247
for event := range events {
247-
timedEvents <- timedEvent{
248+
select {
249+
case timedEvents <- timedEvent{
248250
CreatedAt: time.Now(),
249251
EventInfo: event,
252+
}:
253+
default:
254+
if atomic.AddUint64(&droppedEvents, 1) == 1 {
255+
flog.Info("dropped event, sync should restart soon")
256+
}
250257
}
251258
}
252259
}()
@@ -262,7 +269,7 @@ func (s Sync) Run() error {
262269

263270
select {
264271
case ev := <-timedEvents:
265-
if len(events) > maxInflightInotify {
272+
if atomic.LoadUint64(&droppedEvents) > 0 {
266273
return ErrRestartSync
267274
}
268275

0 commit comments

Comments
 (0)