mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
update to consolidated libp2p interface package (#21)
and fix parsing of connection latencies This commit was moved from ipfs/go-ipfs-http-client@fd5cce4cbc
This commit is contained in:
parent
2fabc8c0e2
commit
9345cfa7b8
@ -6,38 +6,37 @@ import (
|
||||
|
||||
caopts "github.com/ipfs/interface-go-ipfs-core/options"
|
||||
"github.com/ipfs/interface-go-ipfs-core/path"
|
||||
"github.com/libp2p/go-libp2p-peer"
|
||||
"github.com/libp2p/go-libp2p-peerstore"
|
||||
notif "github.com/libp2p/go-libp2p-routing/notifications"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-core/routing"
|
||||
)
|
||||
|
||||
type DhtAPI HttpApi
|
||||
|
||||
func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (peerstore.PeerInfo, error) {
|
||||
func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
|
||||
var out struct {
|
||||
Type notif.QueryEventType
|
||||
Responses []peerstore.PeerInfo
|
||||
Type routing.QueryEventType
|
||||
Responses []peer.AddrInfo
|
||||
}
|
||||
resp, err := api.core().Request("dht/findpeer", p.Pretty()).Send(ctx)
|
||||
if err != nil {
|
||||
return peerstore.PeerInfo{}, err
|
||||
return peer.AddrInfo{}, err
|
||||
}
|
||||
if resp.Error != nil {
|
||||
return peerstore.PeerInfo{}, resp.Error
|
||||
return peer.AddrInfo{}, resp.Error
|
||||
}
|
||||
defer resp.Close()
|
||||
dec := json.NewDecoder(resp.Output)
|
||||
for {
|
||||
if err := dec.Decode(&out); err != nil {
|
||||
return peerstore.PeerInfo{}, err
|
||||
return peer.AddrInfo{}, err
|
||||
}
|
||||
if out.Type == notif.FinalPeer {
|
||||
if out.Type == routing.FinalPeer {
|
||||
return out.Responses[0], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopts.DhtFindProvidersOption) (<-chan peerstore.PeerInfo, error) {
|
||||
func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopts.DhtFindProvidersOption) (<-chan peer.AddrInfo, error) {
|
||||
options, err := caopts.DhtFindProvidersOptions(opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -57,7 +56,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
|
||||
if resp.Error != nil {
|
||||
return nil, resp.Error
|
||||
}
|
||||
res := make(chan peerstore.PeerInfo)
|
||||
res := make(chan peer.AddrInfo)
|
||||
|
||||
go func() {
|
||||
defer resp.Close()
|
||||
@ -67,18 +66,18 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p path.Path, opts ...caopt
|
||||
for {
|
||||
var out struct {
|
||||
Extra string
|
||||
Type notif.QueryEventType
|
||||
Responses []peerstore.PeerInfo
|
||||
Type routing.QueryEventType
|
||||
Responses []peer.AddrInfo
|
||||
}
|
||||
|
||||
if err := dec.Decode(&out); err != nil {
|
||||
return // todo: handle this somehow
|
||||
}
|
||||
if out.Type == notif.QueryError {
|
||||
if out.Type == routing.QueryError {
|
||||
return // usually a 'not found' error
|
||||
// todo: handle other errors
|
||||
}
|
||||
if out.Type == notif.Provider {
|
||||
if out.Type == routing.Provider {
|
||||
for _, pi := range out.Responses {
|
||||
select {
|
||||
case res <- pi:
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"github.com/ipfs/interface-go-ipfs-core"
|
||||
caopts "github.com/ipfs/interface-go-ipfs-core/options"
|
||||
"github.com/ipfs/interface-go-ipfs-core/path"
|
||||
"github.com/libp2p/go-libp2p-peer"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
)
|
||||
|
||||
type KeyAPI HttpApi
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
|
||||
"github.com/ipfs/interface-go-ipfs-core"
|
||||
caopts "github.com/ipfs/interface-go-ipfs-core/options"
|
||||
"github.com/libp2p/go-libp2p-peer"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
)
|
||||
|
||||
type PubsubAPI HttpApi
|
||||
|
||||
@ -5,16 +5,15 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/interface-go-ipfs-core"
|
||||
inet "github.com/libp2p/go-libp2p-net"
|
||||
"github.com/libp2p/go-libp2p-peer"
|
||||
"github.com/libp2p/go-libp2p-peerstore"
|
||||
"github.com/libp2p/go-libp2p-protocol"
|
||||
"github.com/libp2p/go-libp2p-core/network"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-core/protocol"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
|
||||
type SwarmAPI HttpApi
|
||||
|
||||
func (api *SwarmAPI) Connect(ctx context.Context, pi peerstore.PeerInfo) error {
|
||||
func (api *SwarmAPI) Connect(ctx context.Context, pi peer.AddrInfo) error {
|
||||
pidma, err := multiaddr.NewComponent("p2p", pi.ID.Pretty())
|
||||
if err != nil {
|
||||
return err
|
||||
@ -37,7 +36,7 @@ type connInfo struct {
|
||||
peer peer.ID
|
||||
latency time.Duration
|
||||
muxer string
|
||||
direction inet.Direction
|
||||
direction network.Direction
|
||||
streams []protocol.ID
|
||||
}
|
||||
|
||||
@ -49,7 +48,7 @@ func (c *connInfo) Address() multiaddr.Multiaddr {
|
||||
return c.addr
|
||||
}
|
||||
|
||||
func (c *connInfo) Direction() inet.Direction {
|
||||
func (c *connInfo) Direction() network.Direction {
|
||||
return c.direction
|
||||
}
|
||||
|
||||
@ -66,9 +65,9 @@ func (api *SwarmAPI) Peers(ctx context.Context) ([]iface.ConnectionInfo, error)
|
||||
Peers []struct {
|
||||
Addr string
|
||||
Peer string
|
||||
Latency time.Duration
|
||||
Latency string
|
||||
Muxer string
|
||||
Direction inet.Direction
|
||||
Direction network.Direction
|
||||
Streams []struct {
|
||||
Protocol string
|
||||
}
|
||||
@ -85,8 +84,9 @@ func (api *SwarmAPI) Peers(ctx context.Context) ([]iface.ConnectionInfo, error)
|
||||
|
||||
res := make([]iface.ConnectionInfo, len(resp.Peers))
|
||||
for i, conn := range resp.Peers {
|
||||
latency, _ := time.ParseDuration(conn.Latency)
|
||||
out := &connInfo{
|
||||
latency: conn.Latency,
|
||||
latency: latency,
|
||||
muxer: conn.Muxer,
|
||||
direction: conn.Direction,
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user