-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialer.go
31 lines (25 loc) · 810 Bytes
/
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
package libp2pgrpc
import (
"context"
"net"
gostream "github.com/libp2p/go-libp2p-gostream"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/grpc"
)
func (c *Client) GetDialOption(_ context.Context) grpc.DialOption {
return grpc.WithContextDialer(func(ctx context.Context, peerIdStr string) (net.Conn, error) {
peerID, err := peer.Decode(peerIdStr)
if err != nil {
return nil, err
}
conn, err := gostream.Dial(ctx, c.host, peerID, c.protocol)
if err != nil {
return nil, err
}
return conn, nil
})
}
func (c *Client) Dial(ctx context.Context, peerID peer.ID, dialOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialOpsPrepended := append([]grpc.DialOption{c.GetDialOption(ctx)}, dialOpts...)
return grpc.DialContext(ctx, peerID.String(), dialOpsPrepended...)
}