-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc.go
35 lines (31 loc) · 1.04 KB
/
grpc.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
package netx
import (
"context"
"net"
"time"
"github.com/simia-tech/netx/value"
)
// NewGRPCDialer returns a dialer that can be passed to the grpc.Dial function.
func NewGRPCDialer(network string, options ...value.Option) func(string, time.Duration) (net.Conn, error) {
return func(address string, timeout time.Duration) (net.Conn, error) {
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
conn, err := Dial(ctx, network, address, options...)
cancel()
return conn, err
}
return Dial(context.Background(), network, address, options...)
}
}
// NewGRPCMultiDialer returns a dialer that can be passed to the grpc.Dial function.
func NewGRPCMultiDialer(md *MultiDialer) func(string, time.Duration) (net.Conn, error) {
return func(address string, timeout time.Duration) (net.Conn, error) {
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
conn, err := md.Dial(ctx, address)
cancel()
return conn, err
}
return md.Dial(context.Background(), address)
}
}