forked from libp2p/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconnmgr.go
58 lines (45 loc) · 1.37 KB
/
connmgr.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
package p2pd
import (
"context"
"time"
"github.com/libp2p/go-libp2p/core/peer"
pb "github.com/learning-at-home/go-libp2p-daemon/pb"
)
func (d *Daemon) doConnManager(req *pb.Request) *pb.Response {
if req.ConnManager == nil {
return errorResponseString("Malformed request; missing parameters")
}
switch req.ConnManager.GetType() {
case pb.ConnManagerRequest_TAG_PEER:
p, err := peer.IDFromBytes(req.ConnManager.GetPeer())
if err != nil {
return errorResponse(err)
}
tag := req.ConnManager.GetTag()
if tag == "" {
return errorResponseString("Malformed request; missing tag parameter")
}
weight := req.ConnManager.GetWeight()
d.host.ConnManager().TagPeer(p, tag, int(weight))
return okResponse()
case pb.ConnManagerRequest_UNTAG_PEER:
p, err := peer.IDFromBytes(req.ConnManager.GetPeer())
if err != nil {
return errorResponse(err)
}
tag := req.ConnManager.GetTag()
if tag == "" {
return errorResponseString("Malformed request; missing tag parameter")
}
d.host.ConnManager().UntagPeer(p, tag)
return okResponse()
case pb.ConnManagerRequest_TRIM:
ctx, cancel := context.WithTimeout(d.ctx, 60*time.Second)
defer cancel()
d.host.ConnManager().TrimOpenConns(ctx)
return okResponse()
default:
log.Debugf("unexpected ConnManager request type", "type", req.ConnManager.GetType())
return errorResponseString("Unexpected request")
}
}