From 43d64cbc45b47c47832b217a90a1da5b948214e0 Mon Sep 17 00:00:00 2001 From: Aleksandr Zelenin Date: Tue, 9 Oct 2018 06:08:28 +0300 Subject: [PATCH] timeout configuration --- client/client.go | 28 +++++++++++++++++++++++++--- client/tdlib.go | 7 ++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/client/client.go b/client/client.go index 3022836..c98f39b 100644 --- a/client/client.go +++ b/client/client.go @@ -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") } } diff --git a/client/tdlib.go b/client/tdlib.go index ba4cd4a..0e54764 100644 --- a/client/tdlib.go +++ b/client/tdlib.go @@ -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))