Skip to content

Commit

Permalink
Merge pull request #19 from xssnick/reconnect
Browse files Browse the repository at this point in the history
Conneection refactoring & reconnect logic
  • Loading branch information
xssnick authored May 11, 2022
2 parents 7d297e6 + 7670d75 commit 59a0a62
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 159 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,19 @@ if err != nil {
println(val)
```

### Custom reconnect policy
By default, standard reconnect method will be used - `c.DefaultReconnect(3*time.Second, 3)` which will do 3 tries and wait 3 seconds before each.

But you can use your own reconnection logic, this library support callbacks, in this case OnDisconnect callback can be used, you can set it like this:
```golang
client.SetOnDisconnect(func(addr, serverKey string) {
// ... do something
})
```

### Features to implement
*~~Support cell and slice as arguments for run get method~~
* Reconnect
* ✅ Support cell and slice as arguments for run get method
* Reconnect on failure
* More tests
* Get account state method
* Send external query method
Expand Down
17 changes: 16 additions & 1 deletion liteclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"errors"
"io"
"sync"
"time"
)

type OnDisconnectCallback func(addr, key string)

type LiteResponse struct {
TypeID int32
Data []byte
Expand All @@ -29,15 +32,21 @@ type Client struct {
requester chan *LiteRequest

activeConnections int32
onDisconnect func(addr, key string)
}

var ErrNoActiveConnections = errors.New("no active connections")

func NewClient() *Client {
return &Client{
c := &Client{
activeReqs: map[string]*LiteRequest{},
requester: make(chan *LiteRequest),
}

// default reconnect policy
c.SetOnDisconnect(c.DefaultReconnect(3*time.Second, 3))

return c
}

func (c *Client) Do(ctx context.Context, typeID int32, payload []byte) (*LiteResponse, error) {
Expand Down Expand Up @@ -91,3 +100,9 @@ func (c *Client) Do(ctx context.Context, typeID int32, payload []byte) (*LiteRes
return nil, ctx.Err()
}
}

func (c *Client) SetOnDisconnect(cb OnDisconnectCallback) {
c.mx.Lock()
c.onDisconnect = cb
c.mx.Unlock()
}
Loading

0 comments on commit 59a0a62

Please sign in to comment.