forked from Silverfish/proton-bridge
GODT-1657: Stable sync (still needs more tests)
This commit is contained in:
55
internal/user/sync_reporter.go
Normal file
55
internal/user/sync_reporter.go
Normal file
@ -0,0 +1,55 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/ProtonMail/gluon/queue"
|
||||
"github.com/ProtonMail/proton-bridge/v2/internal/events"
|
||||
)
|
||||
|
||||
type reporter struct {
|
||||
userID string
|
||||
eventCh *queue.QueuedChannel[events.Event]
|
||||
|
||||
start time.Time
|
||||
total int
|
||||
count int
|
||||
|
||||
last time.Time
|
||||
freq time.Duration
|
||||
}
|
||||
|
||||
func newReporter(userID string, eventCh *queue.QueuedChannel[events.Event], total int, freq time.Duration) *reporter {
|
||||
return &reporter{
|
||||
userID: userID,
|
||||
eventCh: eventCh,
|
||||
|
||||
start: time.Now(),
|
||||
total: total,
|
||||
freq: freq,
|
||||
}
|
||||
}
|
||||
|
||||
func (rep *reporter) add(delta int) {
|
||||
rep.count += delta
|
||||
|
||||
if time.Since(rep.last) > rep.freq {
|
||||
rep.eventCh.Enqueue(events.SyncProgress{
|
||||
UserID: rep.userID,
|
||||
Progress: float64(rep.count) / float64(rep.total),
|
||||
Elapsed: time.Since(rep.start),
|
||||
Remaining: time.Since(rep.start) * time.Duration(rep.total-(rep.count+1)) / time.Duration(rep.count+1),
|
||||
})
|
||||
|
||||
rep.last = time.Now()
|
||||
}
|
||||
}
|
||||
|
||||
func (rep *reporter) done() {
|
||||
rep.eventCh.Enqueue(events.SyncProgress{
|
||||
UserID: rep.userID,
|
||||
Progress: 1,
|
||||
Elapsed: time.Since(rep.start),
|
||||
Remaining: 0,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user