-
Notifications
You must be signed in to change notification settings - Fork 4
/
hostset.go
220 lines (196 loc) · 5.32 KB
/
hostset.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package renterutil
import (
"errors"
"fmt"
"strings"
"sync"
"time"
"go.sia.tech/siad/types"
"lukechampine.com/us/hostdb"
"lukechampine.com/us/renter"
"lukechampine.com/us/renter/proto"
)
var (
errNoHost = errors.New("no record of that host")
errHostAcquired = errors.New("host is currently acquired")
)
// A HostError associates an error with a given host.
type HostError struct {
HostKey hostdb.HostPublicKey
Err error
}
// Error implements error.
func (he HostError) Error() string {
return he.HostKey.ShortKey() + ": " + he.Err.Error()
}
// Unwrap returns the underlying error.
func (he HostError) Unwrap() error {
return he.Err
}
// A HostErrorSet is a collection of errors from various hosts.
type HostErrorSet []*HostError
// Error implements error.
func (hes HostErrorSet) Error() string {
strs := make([]string, len(hes))
for i := range strs {
strs[i] = hes[i].Error()
}
// include a leading newline so that the first error isn't printed on the
// same line as the error context
return "\n" + strings.Join(strs, "\n")
}
type tryLock struct {
c chan struct{}
once sync.Once
}
func (mu *tryLock) init() {
mu.c = make(chan struct{}, 1)
mu.c <- struct{}{}
}
func (mu *tryLock) Lock() {
mu.once.Do(mu.init)
<-mu.c
}
func (mu *tryLock) TryLock() bool {
mu.once.Do(mu.init)
select {
case <-mu.c:
return true
default:
return false
}
}
func (mu *tryLock) Unlock() {
mu.c <- struct{}{}
}
type lockedHost struct {
reconnect func() error
s *proto.Session
mu tryLock
}
// A HostSet is a collection of renter-host protocol sessions.
type HostSet struct {
sessions map[hostdb.HostPublicKey]*lockedHost
hkr renter.HostKeyResolver
currentHeight types.BlockHeight
lockTimeout time.Duration
onConnect func(s *proto.Session)
}
// HasHost returns true if the specified host is in the set.
func (set *HostSet) HasHost(hostKey hostdb.HostPublicKey) bool {
_, ok := set.sessions[hostKey]
return ok
}
// Close closes all of the sessions in the set.
func (set *HostSet) Close() error {
for hostKey, lh := range set.sessions {
lh.mu.Lock()
if lh.s != nil {
lh.s.Close()
lh.s = nil
}
delete(set.sessions, hostKey)
}
return nil
}
func (set *HostSet) acquire(host hostdb.HostPublicKey) (*proto.Session, error) {
ls, ok := set.sessions[host]
if !ok {
return nil, errNoHost
}
ls.mu.Lock()
if err := ls.reconnect(); err != nil {
ls.mu.Unlock()
return nil, err
}
return ls.s, nil
}
func (set *HostSet) tryAcquire(host hostdb.HostPublicKey) (*proto.Session, error) {
ls, ok := set.sessions[host]
if !ok {
return nil, errNoHost
}
if !ls.mu.TryLock() {
return nil, errHostAcquired
}
if err := ls.reconnect(); err != nil {
ls.mu.Unlock()
return nil, err
}
return ls.s, nil
}
func (set *HostSet) release(host hostdb.HostPublicKey) {
lh := set.sessions[host]
if lh.s.IsClosed() {
lh.s = nil // force a reconnect
}
lh.mu.Unlock()
}
// SetLockTimeout sets the timeout used for all Lock RPCs in Sessions initiated
// by the HostSet.
func (set *HostSet) SetLockTimeout(timeout time.Duration) { set.lockTimeout = timeout }
// SetOnConnect sets the function called on all newly-connected Sessions.
func (set *HostSet) SetOnConnect(fn func(*proto.Session)) { set.onConnect = fn }
// AddHost adds a host to the set for later use.
func (set *HostSet) AddHost(c renter.Contract) {
lh := new(lockedHost)
// lazy connection function
var lastSeen time.Time
lh.reconnect = func() error {
if lh.s != nil && !lh.s.IsClosed() {
// if it hasn't been long since the last reconnect, assume the
// connection is still open
if time.Since(lastSeen) < 2*time.Minute {
lastSeen = time.Now()
return nil
}
// otherwise, the connection *might* still be open; test by sending
// a "ping" RPC
//
// NOTE: this is somewhat inefficient; it means we might incur an
// extra roundtrip when we don't need to. Better would be for the
// caller to handle the reconnection logic after calling whatever
// RPC it wants to call; that way, we only do extra work if the host
// has actually disconnected. But that feels too burdensome.
if _, err := lh.s.Settings(); err == nil {
lastSeen = time.Now()
return nil
}
// connection timed out, or some other error occurred; close our
// end (just in case) and fallthrough to the reconnection logic
lh.s.Close()
}
hostIP, err := set.hkr.ResolveHostKey(c.HostKey)
if err != nil {
return fmt.Errorf("could not resolve host key: %w", err)
}
// create and lock the session manually so that we can use our custom
// lock timeout
lh.s, err = proto.NewUnlockedSession(hostIP, c.HostKey, set.currentHeight)
if err != nil {
return err
}
if err := lh.s.Lock(c.ID, c.RenterKey, set.lockTimeout); err != nil {
lh.s.Close()
return err
} else if _, err := lh.s.Settings(); err != nil {
lh.s.Close()
return err
}
set.onConnect(lh.s)
lastSeen = time.Now()
return nil
}
set.sessions[c.HostKey] = lh
}
// NewHostSet creates an empty HostSet using the provided resolver and current
// height.
func NewHostSet(hkr renter.HostKeyResolver, currentHeight types.BlockHeight) *HostSet {
return &HostSet{
hkr: hkr,
currentHeight: currentHeight,
sessions: make(map[hostdb.HostPublicKey]*lockedHost),
lockTimeout: 10 * time.Second,
onConnect: func(*proto.Session) {},
}
}