Skip to content

Commit b0ef0e1

Browse files
committed
fix(client): deprecate WithResponseTimeout in favor of WithResponseTimeoutDuration
1 parent 60bbf98 commit b0ef0e1

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

api/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (c *Client) SendRequest(requestBody Params, responseBody Response) error {
103103

104104
var response *opcodes.RequestResponse
105105

106-
timer := time.NewTimer(c.ResponseTimeout * time.Millisecond)
106+
timer := time.NewTimer(c.ResponseTimeout)
107107
defer timer.Stop()
108108
select {
109109
case response = <-c.IncomingResponses:

client.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,24 @@ func WithRequestHeader(x http.Header) Option {
8484
// WithResponseTimeout sets the time we're willing to wait to receive a response
8585
// from the server for any request, before responding with an error. It's in
8686
// milliseconds. The default timeout is 10 seconds.
87+
//
88+
// Deprecated: WithResponseTimeout interprets its argument as milliseconds, even
89+
// though it is typed as [time.Duration]. Prefer WithResponseTimeoutDuration,
90+
// which takes a proper [time.Duration] value.
8791
func WithResponseTimeout(x time.Duration) Option {
88-
return func(o *Client) { o.client.ResponseTimeout = time.Duration(x) }
92+
return WithResponseTimeoutDuration(x * time.Millisecond)
93+
}
94+
95+
// WithResponseTimeoutDuration sets the time we're willing to wait to receive a
96+
// response from the server for any request, before responding with an error.
97+
func WithResponseTimeoutDuration(x time.Duration) Option {
98+
if x <= 0 {
99+
x = 10 * time.Second
100+
}
101+
102+
return func(o *Client) {
103+
o.client.ResponseTimeout = x
104+
}
89105
}
90106

91107
// WithScheme sets the protocol scheme to use when connecting to the server,
@@ -165,7 +181,7 @@ func New(host string, opts ...Option) (*Client, error) {
165181
Disconnected: make(chan struct{}),
166182
IncomingResponses: make(chan *opcodes.RequestResponse),
167183
Opcodes: make(chan opcodes.Opcode),
168-
ResponseTimeout: 10000,
184+
ResponseTimeout: 10 * time.Second,
169185
Log: log.New(
170186
&logutils.LevelFilter{
171187
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "ERROR", ""},
@@ -233,7 +249,7 @@ func (c *Client) connect() (err error) {
233249
close(c.client.IncomingResponses)
234250
}()
235251

236-
timer := time.NewTimer(c.client.ResponseTimeout * time.Millisecond)
252+
timer := time.NewTimer(c.client.ResponseTimeout)
237253
defer timer.Stop()
238254
select {
239255
case a := <-authComplete:

0 commit comments

Comments
 (0)