go-libp2p-daemon/connmgr.go

59 lines
1.4 KiB
Go
Raw Permalink Normal View History

2018-10-28 12:21:30 +00:00
package p2pd
import (
"context"
"time"
2019-05-26 22:58:53 +00:00
"github.com/libp2p/go-libp2p-core/peer"
2018-10-28 12:21:30 +00:00
2019-05-26 22:58:53 +00:00
pb "github.com/libp2p/go-libp2p-daemon/pb"
2018-10-28 12:21:30 +00:00
)
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")
}
}