forked from NVIDIA/aistore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotif.go
70 lines (58 loc) · 1.95 KB
/
notif.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package notifications provides interfaces for AIStore notifications
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
*/
package nl
import (
"time"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/atomic"
"github.com/NVIDIA/aistore/cmn/mono"
"github.com/NVIDIA/aistore/core"
)
type (
Base struct {
F func(n core.Notif, err error, aborted bool) // notification callback
P func(n core.Notif) // on progress notification callback
Dsts []string // node IDs to notify
When core.Upon // see the enum below
Interval time.Duration // interval at which progress needs to be updated
lastNotified atomic.Int64 // time when last notified
}
)
//////////
// Base //
//////////
func (base *Base) OnFinishedCB() func(core.Notif, error, bool /*aborted*/) { return base.F }
func (base *Base) OnProgressCB() func(core.Notif) { return base.P }
func (base *Base) Upon(u core.Upon) bool { return base != nil && base.When&u != 0 }
func (base *Base) Subscribers() []string { return base.Dsts }
func (base *Base) LastNotifTime() int64 { return base.lastNotified.Load() }
func (base *Base) SetLastNotified(now int64) { base.lastNotified.Store(now) }
func (base *Base) NotifyInterval() time.Duration {
if base.Interval == 0 {
return cmn.GCO.Get().Periodic.NotifTime.D()
}
return base.Interval
}
//
// common callbacks
//
func shouldNotify(n core.Notif) bool {
lastTime := n.LastNotifTime()
return lastTime == 0 || mono.Since(lastTime) > n.NotifyInterval()
}
func OnProgress(n core.Notif) {
if n == nil {
return
}
if cb := n.OnProgressCB(); cb != nil && shouldNotify(n) {
n.SetLastNotified(mono.NanoTime())
cb(n)
}
}
func OnFinished(n core.Notif, err error, aborted bool) {
if cb := n.OnFinishedCB(); cb != nil {
cb(n, err, aborted)
}
}