forked from andeya/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.go
150 lines (133 loc) · 3.67 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2015-2019 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package erpc
import (
"context"
"crypto/tls"
"net"
"time"
"github.com/henrylee2cn/erpc/v6/quic"
)
// Dialer dial-up connection
type Dialer struct {
network string
localAddr net.Addr
tlsConfig *tls.Config
dialTimeout time.Duration
redialInterval time.Duration
redialTimes int32
}
// NewDialer creates a dialer.
func NewDialer(localAddr net.Addr, tlsConfig *tls.Config,
dialTimeout, redialInterval time.Duration, redialTimes int32,
) *Dialer {
return &Dialer{
network: localAddr.Network(),
localAddr: localAddr,
tlsConfig: tlsConfig,
dialTimeout: dialTimeout,
redialInterval: redialInterval,
redialTimes: redialTimes,
}
}
// Network returns the network.
func (d *Dialer) Network() string {
return d.network
}
// LocalAddr returns the local address.
func (d *Dialer) LocalAddr() net.Addr {
return d.localAddr
}
// TLSConfig returns the TLS config.
func (d *Dialer) TLSConfig() *tls.Config {
return d.tlsConfig
}
// DialTimeout returns the dial timeout.
func (d *Dialer) DialTimeout() time.Duration {
return d.dialTimeout
}
// RedialInterval returns the redial interval.
func (d *Dialer) RedialInterval() time.Duration {
return d.redialInterval
}
// RedialTimes returns the redial times.
func (d *Dialer) RedialTimes() int32 {
return d.redialTimes
}
// Dial dials the connection, and try again if it fails.
func (d *Dialer) Dial(addr string) (net.Conn, error) {
return d.dialWithRetry(addr, "")
}
// dialWithRetry dials the connection, and try again if it fails.
// NOTE:
// sessID is not empty only when the disconnection is redialing
func (d *Dialer) dialWithRetry(addr, sessID string) (net.Conn, error) {
conn, err := d.dialOne(addr)
if err == nil {
return conn, nil
}
redialTimes := d.newRedialCounter()
for redialTimes.Next() {
time.Sleep(d.redialInterval)
if sessID == "" {
Debugf("trying to redial... (network:%s, addr:%s)", d.network, addr)
} else {
Debugf("trying to redial... (network:%s, addr:%s, id:%s)", d.network, addr, sessID)
}
conn, err = d.dialOne(addr)
if err == nil {
return conn, nil
}
}
return nil, err
}
// dialOne dials the connection once.
func (d *Dialer) dialOne(addr string) (net.Conn, error) {
if asQUIC(d.network) {
ctx := context.Background()
if d.dialTimeout > 0 {
ctx, _ = context.WithTimeout(ctx, d.dialTimeout)
}
if d.tlsConfig == nil {
return quic.DialAddrContext(ctx, addr, GenerateTLSConfigForClient(), nil)
}
return quic.DialAddrContext(ctx, addr, d.tlsConfig, nil)
}
dialer := &net.Dialer{
LocalAddr: d.localAddr,
Timeout: d.dialTimeout,
}
if d.tlsConfig != nil {
return tls.DialWithDialer(dialer, d.network, addr, d.tlsConfig)
}
return dialer.Dial(d.network, addr)
}
// newRedialCounter creates a new redial counter.
func (d *Dialer) newRedialCounter() *redialCounter {
r := redialCounter(d.redialTimes)
return &r
}
// redialCounter redial counter
type redialCounter int32
// Next returns whether there are still more redial times.
func (r *redialCounter) Next() bool {
t := *r
if t == 0 {
return false
}
if t > 0 {
*r--
}
return true
}