Merge pull request #60 from divinerapier/master
fix: time.After will cause memory leaks
This commit is contained in:
commit
e162b22637
|
@ -1,6 +1,7 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -123,17 +124,20 @@ func (client *Client) Send(req Request) (*Response, error) {
|
||||||
client.catchersStore.Store(req.Extra, catcher)
|
client.catchersStore.Store(req.Extra, catcher)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
close(catcher)
|
|
||||||
client.catchersStore.Delete(req.Extra)
|
client.catchersStore.Delete(req.Extra)
|
||||||
|
close(catcher)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
client.jsonClient.Send(req)
|
client.jsonClient.Send(req)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), client.catchTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case response := <-catcher:
|
case response := <-catcher:
|
||||||
return response, nil
|
return response, nil
|
||||||
|
|
||||||
case <-time.After(client.catchTimeout):
|
case <-ctx.Done():
|
||||||
return nil, errors.New("response catching timeout")
|
return nil, errors.New("response catching timeout")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue