-
Notifications
You must be signed in to change notification settings - Fork 153
/
monitor.go
124 lines (109 loc) · 3.53 KB
/
monitor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2020 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package pathhealth monitors paths to different ASes. Call Monitor.Register()
// to start monitoring paths to a remote AS using a chosen path policy. The call
// returns a registration object which can be used to obtain the best path.
package pathhealth
import (
"context"
"sync"
"time"
"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/log"
"github.com/scionproto/scion/pkg/private/ctrl/path_mgmt"
"github.com/scionproto/scion/pkg/snet"
)
const (
// defaultPathUpdateInterval specifies how often the paths are retrieved from the daemon.
defaultPathUpdateInterval = 10 * time.Second
)
type RemoteWatcher interface {
Run(context.Context)
PathWatchers() []PathWatcher
}
// RemoteWatcherFactory creates RemoteWatchers.
type RemoteWatcherFactory interface {
New(remote addr.IA) RemoteWatcher
}
// RevocationStore keeps track of revocations.
type RevocationStore interface {
// AddRevocation adds a revocation.
AddRevocation(ctx context.Context, rev *path_mgmt.RevInfo)
// IsRevoked returns true if there is at least one revoked interface on the path.
IsRevoked(path snet.Path) bool
// Cleanup removes all expired revocations.
Cleanup(ctx context.Context)
}
type Monitor struct {
// RemoteWatcherFactory creates a RemoteWatcher for the specified remote.
RemoteWatcherFactory RemoteWatcherFactory
mtx sync.Mutex
// remoteWatchers is a map of all monitored IAs.
remoteWatchers map[addr.IA]*remoteWatcherItem
}
// Register starts monitoring given AS under the specified selector.
func (m *Monitor) Register(remote addr.IA, selector PathSelector) *Registration {
m.mtx.Lock()
defer m.mtx.Unlock()
m.ensureInitializedLocked()
// If a monitor for the given AS does not exist, create it.
// Otherwise, increase its reference count.
item := m.remoteWatchers[remote]
if item == nil {
ctx, cancel := context.WithCancel(context.Background())
item = &remoteWatcherItem{
RemoteWatcher: m.RemoteWatcherFactory.New(remote),
refCount: 1,
remote: remote,
cancel: cancel,
}
m.remoteWatchers[remote] = item
go func() {
defer log.HandlePanic()
item.Run(ctx)
}()
} else {
item.refCount++
}
return &Registration{
monitor: m,
remoteWatcher: item,
pathSelector: selector,
}
}
func (m *Monitor) ensureInitializedLocked() {
if m.remoteWatchers != nil {
return
}
m.remoteWatchers = make(map[addr.IA]*remoteWatcherItem)
}
func (m *Monitor) unregister(remoteWatcher *remoteWatcherItem) {
m.mtx.Lock()
defer m.mtx.Unlock()
// If the monitor for the IA is not needed any more, remove it.
remoteWatcher.refCount--
if remoteWatcher.refCount == 0 {
remoteWatcher.cancel()
delete(m.remoteWatchers, remoteWatcher.remote)
}
}
// remoteWatcherItem is a helper structure that augments RemoteWatcher with
// Monitor specific metadata.
type remoteWatcherItem struct {
RemoteWatcher
remote addr.IA
// refCount keeps track of how many references to this object there are.
refCount int
cancel context.CancelFunc
}