-
Notifications
You must be signed in to change notification settings - Fork 0
/
ql.go
82 lines (73 loc) · 1.8 KB
/
ql.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ql
import (
"context"
"encoding/json"
"fmt"
"github.com/libp2p/go-libp2p-gorpc"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
func QlCall(
h host.Host,
ctx context.Context,
input interface{},
peerAddr string,
serviceName string,
serviceMethod string,
method string,
span trace.Span,
) (
BridgeReply,
error,
) {
body, err := json.Marshal(input)
if err != nil {
return BridgeReply{}, fmt.Errorf("QlCall, json marshal input: %w", err)
}
ma, err := multiaddr.NewMultiaddr(peerAddr)
if err != nil {
return BridgeReply{}, err
}
addrs, err := madns.Resolve(context.Background(), ma)
if err != nil {
return BridgeReply{}, fmt.Errorf("QlCall, DNS resolve: %w", err)
}
if len(addrs) != 1 {
return BridgeReply{}, fmt.Errorf("QlCall, DNS address count: %d", len(addrs))
}
peerInfo, err := peer.AddrInfoFromP2pAddr(addrs[0])
if err != nil {
return BridgeReply{}, err
}
err = h.Connect(ctx, *peerInfo)
if err != nil {
return BridgeReply{}, fmt.Errorf("QlCall, host connection: \n%w", err)
}
rpcClient := rpc.NewClient(h, "/p2p/1.0.0")
bCall := BridgeCall{Method: method, Body: body}
if span != nil {
carrier := propagation.MapCarrier{}
propagation.TraceContext{}.Inject(ctx, carrier)
bCall.Carrier = carrier
}
args, err := json.Marshal(bCall)
if err != nil {
return BridgeReply{}, fmt.Errorf("QlCall, json marshal BridgeCall: %w", err)
}
var reply BridgeReply
err = rpcClient.Call(
peerInfo.ID,
serviceName,
serviceMethod,
BridgeArgs{Data: args},
&reply,
)
if err != nil {
return BridgeReply{}, fmt.Errorf("QlCall, rpcClient call: %w", err)
}
return reply, nil
}