timeout configuration
This commit is contained in:
parent
d74e676cd7
commit
43d64cbc45
|
@ -12,6 +12,8 @@ type Client struct {
|
||||||
catcher chan *Response
|
catcher chan *Response
|
||||||
listenerStore *listenerStore
|
listenerStore *listenerStore
|
||||||
catchersStore *sync.Map
|
catchersStore *sync.Map
|
||||||
|
updatesTimeout time.Duration
|
||||||
|
catchTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Client)
|
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) {
|
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
|
||||||
catchersListener := make(chan *Response, 1000)
|
catchersListener := make(chan *Response, 1000)
|
||||||
|
|
||||||
|
@ -40,6 +54,14 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
|
||||||
client.extraGenerator = UuidV4Generator()
|
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.receive()
|
||||||
go client.catch(catchersListener)
|
go client.catch(catchersListener)
|
||||||
|
|
||||||
|
@ -53,7 +75,7 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
|
||||||
|
|
||||||
func (client *Client) receive() {
|
func (client *Client) receive() {
|
||||||
for {
|
for {
|
||||||
resp, err := client.jsonClient.Receive(10)
|
resp, err := client.jsonClient.Receive(client.updatesTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -107,8 +129,8 @@ func (client *Client) Send(req Request) (*Response, error) {
|
||||||
case response := <-catcher:
|
case response := <-catcher:
|
||||||
return response, nil
|
return response, nil
|
||||||
|
|
||||||
case <-time.After(10 * time.Second):
|
case <-time.After(client.catchTimeout):
|
||||||
return nil, errors.New("timeout")
|
return nil, errors.New("response catching timeout")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -39,10 +40,10 @@ func (jsonClient *JsonClient) Send(req Request) {
|
||||||
// shouldn't be called simultaneously from two different threads.
|
// 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
|
// 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.
|
// in the same thread, so it can't be used after that.
|
||||||
func (jsonClient *JsonClient) Receive(timeout float64) (*Response, error) {
|
func (jsonClient *JsonClient) Receive(timeout time.Duration) (*Response, error) {
|
||||||
result := C.td_json_client_receive(jsonClient.jsonClient, C.double(timeout))
|
result := C.td_json_client_receive(jsonClient.jsonClient, C.double(float64(timeout)/float64(time.Second)))
|
||||||
if result == nil {
|
if result == nil {
|
||||||
return nil, errors.New("timeout")
|
return nil, errors.New("update receiving timeout")
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte(C.GoString(result))
|
data := []byte(C.GoString(result))
|
||||||
|
|
Loading…
Reference in a new issue