timeout configuration

This commit is contained in:
Aleksandr Zelenin 2018-10-09 06:08:28 +03:00
parent d74e676cd7
commit 43d64cbc45
2 changed files with 29 additions and 6 deletions

View file

@ -12,6 +12,8 @@ type Client struct {
catcher chan *Response
listenerStore *listenerStore
catchersStore *sync.Map
updatesTimeout time.Duration
catchTimeout time.Duration
}
type Option func(*Client)
@ -22,6 +24,18 @@ func WithExtraGenerator(extraGenerator ExtraGenerator) Option {
}
}
func WithCatchTimeout(timeout time.Duration) Option {
return func(client *Client) {
client.catchTimeout = timeout
}
}
func WithUpdatesTimeout(timeout time.Duration) Option {
return func(client *Client) {
client.updatesTimeout = timeout
}
}
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
catchersListener := make(chan *Response, 1000)
@ -40,6 +54,14 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
client.extraGenerator = UuidV4Generator()
}
if client.catchTimeout == 0 {
client.catchTimeout = 60 * time.Second
}
if client.updatesTimeout == 0 {
client.updatesTimeout = 60 * time.Second
}
go client.receive()
go client.catch(catchersListener)
@ -53,7 +75,7 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
func (client *Client) receive() {
for {
resp, err := client.jsonClient.Receive(10)
resp, err := client.jsonClient.Receive(client.updatesTimeout)
if err != nil {
continue
}
@ -107,8 +129,8 @@ func (client *Client) Send(req Request) (*Response, error) {
case response := <-catcher:
return response, nil
case <-time.After(10 * time.Second):
return nil, errors.New("timeout")
case <-time.After(client.catchTimeout):
return nil, errors.New("response catching timeout")
}
}

View file

@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"strconv"
"time"
"unsafe"
)
@ -39,10 +40,10 @@ func (jsonClient *JsonClient) Send(req Request) {
// shouldn't be called simultaneously from two different threads.
// Returned pointer will be deallocated by TDLib during next call to td_json_client_receive or td_json_client_execute
// in the same thread, so it can't be used after that.
func (jsonClient *JsonClient) Receive(timeout float64) (*Response, error) {
result := C.td_json_client_receive(jsonClient.jsonClient, C.double(timeout))
func (jsonClient *JsonClient) Receive(timeout time.Duration) (*Response, error) {
result := C.td_json_client_receive(jsonClient.jsonClient, C.double(float64(timeout)/float64(time.Second)))
if result == nil {
return nil, errors.New("timeout")
return nil, errors.New("update receiving timeout")
}
data := []byte(C.GoString(result))