-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathsyncer_status.go
More file actions
49 lines (37 loc) · 927 Bytes
/
syncer_status.go
File metadata and controls
49 lines (37 loc) · 927 Bytes
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
package sync
import "sync"
// SyncerStatus is used by header and block exchange service for keeping track
// of the status of the syncer in them.
type SyncerStatus struct {
mu sync.Mutex
started bool
}
func (syncerStatus *SyncerStatus) isStarted() bool {
syncerStatus.mu.Lock()
defer syncerStatus.mu.Unlock()
return syncerStatus.started
}
func (syncerStatus *SyncerStatus) startOnce(startFn func() error) (bool, error) {
syncerStatus.mu.Lock()
defer syncerStatus.mu.Unlock()
if syncerStatus.started {
return false, nil
}
if err := startFn(); err != nil {
return false, err
}
syncerStatus.started = true
return true, nil
}
func (syncerStatus *SyncerStatus) stopIfStarted(stopFn func() error) error {
syncerStatus.mu.Lock()
defer syncerStatus.mu.Unlock()
if !syncerStatus.started {
return nil
}
if err := stopFn(); err != nil {
return err
}
syncerStatus.started = false
return nil
}