-
Notifications
You must be signed in to change notification settings - Fork 10
/
dialer.go
51 lines (43 loc) · 1.33 KB
/
dialer.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
package p2pgrpc
import (
"context"
"net"
"time"
peer "github.com/libp2p/go-libp2p-peer"
ps "github.com/libp2p/go-libp2p-peerstore"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
// GetDialOption returns the WithDialer option to dial via libp2p.
// note: ctx should be the root context.
func (p *GRPCProtocol) GetDialOption(ctx context.Context) grpc.DialOption {
return grpc.WithDialer(func(peerIdStr string, timeout time.Duration) (net.Conn, error) {
subCtx, subCtxCancel := context.WithTimeout(ctx, timeout)
defer subCtxCancel()
id, err := peer.IDB58Decode(peerIdStr)
if err != nil {
return nil, errors.WithMessage(err, "grpc tried to dial non peer-id")
}
err = p.host.Connect(subCtx, ps.PeerInfo{
ID: id,
})
if err != nil {
return nil, err
}
stream, err := p.host.NewStream(ctx, id, Protocol)
if err != nil {
return nil, err
}
return &streamConn{Stream: stream}, nil
})
}
// Dial attempts to open a GRPC connection over libp2p to a peer.
// Note that the context is used as the **stream context** not just the dial context.
func (p *GRPCProtocol) Dial(
ctx context.Context,
peerID peer.ID,
dialOpts ...grpc.DialOption,
) (*grpc.ClientConn, error) {
dialOpsPrepended := append([]grpc.DialOption{p.GetDialOption(ctx)}, dialOpts...)
return grpc.DialContext(ctx, peerID.Pretty(), dialOpsPrepended...)
}