-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
177 lines (144 loc) · 3.37 KB
/
client.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gipc
import (
"context"
"errors"
"fmt"
"io"
"time"
)
// StartClient - start the ipc client.
// ipcName = is the name of the unix socket or named pipe that the client will try and connect to.
func StartClient(config *ClientConfig) (*Client, error) {
if config.MultiClient {
return StartClientPool(config)
} else {
cc, err := NewClient(config.Name, config)
if err != nil {
return nil, err
}
cc.ClientId = 0
return start(cc)
}
}
func NewClient(name string, config *ClientConfig) (*Client, error) {
err := checkIpcName(name)
if err != nil {
return nil, err
}
if config == nil {
config = &ClientConfig{
Name: name,
Encryption: ENCRYPT_BY_DEFAULT,
}
}
cc := &Client{Actor: NewActor(&ActorConfig{
ClientConfig: config,
})}
cc.clientRef = cc
config.Name = name
if config.Timeout < 0 {
cc.timeout = 0
} else {
cc.timeout = config.Timeout
}
if config.RetryTimer <= 0 {
cc.retryTimer = 1 * time.Second
} else {
cc.retryTimer = config.RetryTimer
}
return cc, err
}
func start(c *Client) (*Client, error) {
c.dispatchStatus(Connecting)
err := c.dial()
if err != nil {
c.dispatchError(err)
return c, err
}
go c.read(c.ByteReader)
go c.write()
c.dispatchStatus(Connected)
return c, nil
}
// Client connect to the unix socket created by the server - for unix and linux
func (c *Client) dial() error {
errChan := make(chan error, 1)
go func() {
startTime := time.Now()
for {
if c.timeout != 0 {
if time.Since(startTime) > c.timeout {
return
}
}
conn, err := c.connect()
if err != nil {
c.logger.Debugf("Client.dial err: %s", err)
} else {
c.setConn(conn)
err = c.handshake()
if err != nil {
c.logger.Errorf("%s.dial handshake err: %s", c, err)
}
errChan <- err
return
}
time.Sleep(c.retryTimer)
}
}()
if c.timeout != 0 {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
select {
case <-ctx.Done():
return errors.New("timed out trying to connect")
case err := <-errChan:
return err
}
} else {
return <-errChan
}
}
func (c *Client) ByteReader(a *Actor, buff []byte) bool {
_, err := io.ReadFull(a.getConn(), buff)
if err != nil {
a.logger.Debugf("%s.readData err: %s", c, err)
if c.getStatus() == Closing {
a.dispatchStatusBlocking(Closed)
a.dispatchErrorStrBlocking("client has closed the connection")
return false
}
if err == io.EOF { // the connection has been closed by the client.
a.getConn().Close()
if a.getStatus() != Closing {
go reconnect(c)
}
return false
}
// other read error
return false
}
return true
}
func reconnect(c *Client) {
c.logger.Warn("Client.reconnect called")
c.dispatchStatus(ReConnecting)
// IMPORTANT removing this line will allow a dial before the new connection
// is ready resulting in a dial hang when a timeout is not specified
time.Sleep(c.retryTimer)
err := c.dial()
if err != nil {
c.logger.Errorf("Client.reconnect -> dial err: %s", err)
if err.Error() == "timed out trying to connect" {
c.dispatchStatusBlocking(Timeout)
c.dispatchErrorStrBlocking("timed out trying to re-connect")
}
return
}
c.dispatchStatus(Connected)
go c.read(c.ByteReader)
}
// getStatus - get the current status of the connection
func (c *Client) String() string {
return fmt.Sprintf("Client(%d)(%s)", c.ClientId, c.getStatus())
}