forked from libp2p/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdaemon.go
273 lines (223 loc) · 6.13 KB
/
daemon.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package p2pd
import (
"context"
"fmt"
"time"
"os"
"sync"
"github.com/learning-at-home/go-libp2p-daemon/config"
"github.com/learning-at-home/go-libp2p-daemon/internal/utils"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
multierror "github.com/hashicorp/go-multierror"
logging "github.com/ipfs/go-log"
dht "github.com/libp2p/go-libp2p-kad-dht"
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
ps "github.com/libp2p/go-libp2p-pubsub"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
var log = logging.Logger("p2pd")
type Daemon struct {
ctx context.Context
host host.Host
listener manet.Listener
dht *dht.IpfsDHT
pubsub *ps.PubSub
peerSourceChan chan peer.AddrInfo // potential relay peers go through this channel; nil means no relay disovery
cancelRelayDiscovery context.CancelFunc
mx sync.Mutex
// stream handlers: map of protocol.ID to multi-addresses, balanced by round robin
handlers map[protocol.ID]*utils.RoundRobin
// closed is set when the daemon is shutting down
closed bool
// unary protocols handlers: map of protocol.ID to wirte ends of pipe, balanced by round robin
registeredUnaryProtocols map[protocol.ID]*utils.RoundRobin
// callID (int64) to chan *pb.PersistentConnectionResponse
// used to return responses to goroutines awating them
responseWaiters sync.Map
// callID (int64) to chan context.CancelFunc
// used to cancel request handlers
cancelUnary sync.Map
// this sync.Once ensures the goroutine awaiting deamon termination is
// only run once
terminateOnce sync.Once
terminateWG sync.WaitGroup
cancelTerminateTimer context.CancelFunc
persistentConnMsgMaxSize int
}
func NewDaemon(
ctx context.Context,
maddr ma.Multiaddr,
dhtMode string,
relayDiscovery bool,
trustedRelays []string,
persistentConnMsgMaxSize int,
opts ...libp2p.Option,
) (*Daemon, error) {
d := &Daemon{
ctx: ctx,
handlers: make(map[protocol.ID]*utils.RoundRobin),
registeredUnaryProtocols: make(map[protocol.ID]*utils.RoundRobin),
persistentConnMsgMaxSize: persistentConnMsgMaxSize,
}
// setup resource usage limits; see https://github.com/libp2p/go-libp2p/tree/master/p2p/host/resource-manager
rm, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(rcmgr.InfiniteLimits))
if err != nil {
panic(err)
}
opts = append(opts, libp2p.ResourceManager(rm))
opts, d.peerSourceChan = MaybeConfigureAutoRelay(opts, relayDiscovery, trustedRelays)
if dhtMode != "" {
var dhtOpts []dhtopts.Option
if dhtMode == config.DHTClientMode {
dhtOpts = append(dhtOpts, dht.Mode(dht.ModeClient))
} else if dhtMode == config.DHTServerMode {
dhtOpts = append(dhtOpts, dht.Mode(dht.ModeServer))
}
opts = append(opts, libp2p.Routing(d.DHTRoutingFactory(dhtOpts)))
}
h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
d.host = h
l, err := manet.Listen(maddr)
if err != nil {
h.Close()
return nil, err
}
d.listener = l
if d.peerSourceChan != nil {
d.cancelRelayDiscovery = BeginRelayDiscovery(d.host, d.dht, trustedRelays, d.peerSourceChan)
}
go d.trapSignals()
return d, nil
}
func (d *Daemon) Listener() manet.Listener {
return d.listener
}
func (d *Daemon) DHTRoutingFactory(opts []dhtopts.Option) func(host.Host) (routing.PeerRouting, error) {
makeRouting := func(h host.Host) (routing.PeerRouting, error) {
dhtInst, err := dht.New(d.ctx, h, opts...)
if err != nil {
return nil, err
}
d.dht = dhtInst
return dhtInst, nil
}
return makeRouting
}
func (d *Daemon) EnableRelayV2() error {
_, err := relay.New(d.host)
return err
}
func (d *Daemon) EnablePubsub(router string, sign, strict bool) error {
var opts []ps.Option
if !sign {
opts = append(opts, ps.WithMessageSigning(false))
} else if !strict {
opts = append(opts, ps.WithStrictSignatureVerification(false))
} else {
opts = append(opts, ps.WithMessageSignaturePolicy(ps.StrictSign))
}
switch router {
case "floodsub":
pubsub, err := ps.NewFloodSub(d.ctx, d.host, opts...)
if err != nil {
return err
}
d.pubsub = pubsub
return nil
case "gossipsub":
pubsub, err := ps.NewGossipSub(d.ctx, d.host, opts...)
if err != nil {
return err
}
d.pubsub = pubsub
return nil
default:
return fmt.Errorf("unknown pubsub router: %s", router)
}
}
func (d *Daemon) ID() peer.ID {
return d.host.ID()
}
func (d *Daemon) Addrs() []ma.Multiaddr {
return d.host.Addrs()
}
func (d *Daemon) Serve() error {
for {
if d.isClosed() {
return nil
}
c, err := d.listener.Accept()
if err != nil {
log.Errorw("error accepting connection", "error", err)
continue
}
log.Debug("incoming connection")
go d.handleConn(c)
}
}
func (d *Daemon) isClosed() bool {
d.mx.Lock()
defer d.mx.Unlock()
return d.closed
}
func clearUnixSockets(path ma.Multiaddr) error {
c, _ := ma.SplitFirst(path)
if c.Protocol().Code != ma.P_UNIX {
return nil
}
if err := os.Remove(c.Value()); err != nil {
return err
}
return nil
}
func (d *Daemon) Close() error {
d.mx.Lock()
d.closed = true
d.mx.Unlock()
var merr *multierror.Error
if err := d.host.Close(); err != nil {
merr = multierror.Append(err)
}
listenAddr := d.listener.Multiaddr()
if err := d.listener.Close(); err != nil {
merr = multierror.Append(merr, err)
}
if err := clearUnixSockets(listenAddr); err != nil {
merr = multierror.Append(merr, err)
}
if d.cancelRelayDiscovery != nil {
d.cancelRelayDiscovery()
d.cancelRelayDiscovery = nil
}
if d.peerSourceChan != nil {
close(d.peerSourceChan)
d.peerSourceChan = nil
}
return merr.ErrorOrNil()
}
func (d *Daemon) awaitTermination() {
d.terminateWG.Wait()
d.Close()
}
func (d *Daemon) KillOnTimeout(timeout time.Duration) {
var ctx context.Context
ctx, d.cancelTerminateTimer = context.WithCancel(d.ctx)
go func() {
select {
case <-ctx.Done():
return
case <-time.NewTimer(timeout).C:
d.Close()
}
}()
}