Skip to content

Commit 4acf3f8

Browse files
committed
sleep 5 seconds when the socket is nil
1 parent b06436a commit 4acf3f8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

client/client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/hyperf/roc/serializer"
1616
)
1717

18+
var SocketNil = errors.New("the socket is nil")
19+
1820
type Client struct {
1921
Packer roc.PackerInterface
2022
IdGenerator roc.IdGeneratorInterface
@@ -69,7 +71,7 @@ func NewTcpClient(address string) (*Client, error) {
6971

7072
func (c *Client) SendPacket(p *roc.Packet) (uint32, error) {
7173
if c.Socket == nil {
72-
return 0, errors.New("the socket is nil")
74+
return 0, SocketNil
7375
}
7476

7577
bt := c.Packer.Pack(p)
@@ -155,6 +157,10 @@ func (c *Client) Loop() {
155157
for {
156158
buf, err := c.readAll(4)
157159
if err != nil {
160+
if errors.Is(err, SocketNil) {
161+
time.Sleep(5 * time.Second)
162+
}
163+
158164
if err != io.EOF {
159165
log.Printf("Error reading %s", err)
160166
}
@@ -188,7 +194,7 @@ func (c *Client) Loop() {
188194

189195
func (c *Client) readAll(length int) ([]byte, error) {
190196
if c.Socket == nil {
191-
return nil, errors.New("the socket is nil")
197+
return nil, SocketIsNil{}
192198
}
193199

194200
ret := make([]byte, 0, length)

0 commit comments

Comments
 (0)