forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_status.go
79 lines (66 loc) · 1.97 KB
/
connection_status.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
71
72
73
74
75
76
77
78
79
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"sync"
)
// Service names used in ConnectionStatus.
const (
KeybaseServiceName = "keybase-service"
MDServiceName = "md-server"
LoginStatusUpdateName = "login"
LogoutStatusUpdateName = "logout"
)
type errDisconnected struct{}
func (errDisconnected) Error() string { return "Disconnected" }
type kbfsCurrentStatus struct {
lock sync.Mutex
failingServices map[string]error
invalidateChan chan StatusUpdate
}
// Init inits the kbfsCurrentStatus.
func (kcs *kbfsCurrentStatus) Init() {
kcs.failingServices = map[string]error{}
kcs.invalidateChan = make(chan StatusUpdate)
}
// CurrentStatus returns a copy of the current status.
func (kcs *kbfsCurrentStatus) CurrentStatus() (map[string]error, chan StatusUpdate) {
kcs.lock.Lock()
defer kcs.lock.Unlock()
res := map[string]error{}
for k, v := range kcs.failingServices {
res[k] = v
}
return res, kcs.invalidateChan
}
// PushConnectionStatusChange pushes a change to the connection status of one of the services.
func (kcs *kbfsCurrentStatus) PushConnectionStatusChange(service string, err error) {
kcs.lock.Lock()
defer kcs.lock.Unlock()
if err != nil {
// Exit early if the service is already failed, to avoid an
// invalidation.
_, errExisted := kcs.failingServices[service]
kcs.failingServices[service] = err
if errExisted {
return
}
} else {
// Potentially exit early if nothing changes.
_, exist := kcs.failingServices[service]
if !exist {
return
}
delete(kcs.failingServices, service)
}
close(kcs.invalidateChan)
kcs.invalidateChan = make(chan StatusUpdate)
}
// PushStatusChange forces a new status be fetched by status listeners.
func (kcs *kbfsCurrentStatus) PushStatusChange() {
kcs.lock.Lock()
defer kcs.lock.Unlock()
close(kcs.invalidateChan)
kcs.invalidateChan = make(chan StatusUpdate)
}