Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions pkg/gotd/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,9 @@ retry:
case <-c.stuck.Ready():
c.log.Debug("Some connection dead, try to create new connection, cancel waiting")

c.freeReq.delete(key)
select {
default:
case conn, ok := <-ch:
if ok && conn != nil {
if !c.freeReq.delete(key) {
// transfer took the request, so a connection is on its way and must not be dropped.
if conn, ok := <-ch; ok && conn != nil {
return conn, nil
}
}
Expand All @@ -224,11 +222,10 @@ retry:
}

// Executed only if at least one of context is Done.
c.freeReq.delete(key)
select {
default:
case conn, ok := <-ch:
if ok && conn != nil {
if !c.freeReq.delete(key) {
// transfer took the request, so a connection is on its way and must be given back to the
// pool instead of being left in the channel.
if conn, ok := <-ch; ok && conn != nil {
c.release(conn)
}
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/gotd/pool/req_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ func (r *reqMap) transfer(c *poolConn) bool {
return true
}

func (r *reqMap) delete(key reqKey) {
// delete removes the pending request. It returns false if transfer already claimed the request,
// which means a connection is about to be sent to the request channel.
func (r *reqMap) delete(key reqKey) bool {
r.mux.Lock()
_, ok := r.m[key]
delete(r.m, key)
r.mux.Unlock()
return ok
}