-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
57 lines (47 loc) · 1.27 KB
/
conn.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
package netx
import (
"context"
"crypto/tls"
"net"
"github.com/simia-tech/netx/value"
)
var dialFuncs = map[string]DialFunc{}
// DialFunc defines the signature of the Dial function.
type DialFunc func(context.Context, string, *value.Options) (net.Conn, error)
// RegisterDial registers the provided Dial method under the provided network name.
func RegisterDial(network string, dialFunc DialFunc) {
dialFuncs[network] = dialFunc
}
// RegisteredDialNetworks returns the available networks for the Dial function.
func RegisteredDialNetworks() []string {
networks := []string{}
for network := range dialFuncs {
networks = append(networks, network)
}
return networks
}
// Dial establishs a connection on the provided network to the provided address.
func Dial(ctx context.Context, network, address string, options ...value.Option) (net.Conn, error) {
o := &value.Options{}
for _, option := range options {
if option == nil {
continue
}
if err := option(o); err != nil {
return nil, err
}
}
dialFunc, ok := dialFuncs[network]
if ok {
return dialFunc(ctx, address, o)
}
var d net.Dialer
conn, err := d.DialContext(ctx, network, address)
if err != nil {
return nil, err
}
if o.TLSConfig != nil {
conn = tls.Client(conn, o.TLSConfig)
}
return conn, nil
}