implement conn manager api
This commit is contained in:
parent
7cb1607b18
commit
35595dfca7
8
conn.go
8
conn.go
|
@ -114,6 +114,14 @@ func (d *Daemon) handleConn(c net.Conn) {
|
|||
return
|
||||
}
|
||||
|
||||
case pb.Request_CONNMANAGER:
|
||||
res := d.doConnManager(&req)
|
||||
err := w.WriteMsg(res)
|
||||
if err != nil {
|
||||
log.Debugf("Error writing response: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
log.Debugf("Unexpected request type: %d", *req.Type)
|
||||
return
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package p2pd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
pb "github.com/libp2p/go-libp2p-daemon/pb"
|
||||
|
||||
peer "github.com/libp2p/go-libp2p-peer"
|
||||
)
|
||||
|
||||
func (d *Daemon) doConnManager(req *pb.Request) *pb.Response {
|
||||
if req.ConnManager == nil {
|
||||
return errorResponseString("Malformed request; missing parameters")
|
||||
}
|
||||
|
||||
switch *req.ConnManager.Type {
|
||||
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: %d", *req.ConnManager.Type)
|
||||
return errorResponseString("Unexpected request")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue