Skip to content

Commit

Permalink
code health: simplify context checks
Browse files Browse the repository at this point in the history
We don't need to over-optimize the work with requests with contexts
that already done. It doesn't look like a work method. It is better
to simplify the code.

Part of #244
  • Loading branch information
oleg-jukovec committed Dec 27, 2022
1 parent 3d37409 commit 091b938
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,22 +1062,18 @@ func (conn *Connection) newFuture(ctx context.Context) (fut *Future) {
}

// This method removes a future from the internal queue if the context
// is "done" before the response is come. Such select logic is inspired
// from this thread: https://groups.google.com/g/golang-dev/c/jX4oQEls3uk
// is "done" before the response is come.
func (conn *Connection) contextWatchdog(fut *Future, ctx context.Context) {
select {
case <-fut.done:
case <-ctx.Done():
}

select {
case <-fut.done:
return
default:
select {
case <-ctx.Done():
conn.cancelFuture(fut, fmt.Errorf("context is done"))
default:
select {
case <-fut.done:
case <-ctx.Done():
conn.cancelFuture(fut, fmt.Errorf("context is done"))
}
}
conn.cancelFuture(fut, fmt.Errorf("context is done"))
}
}

Expand All @@ -1093,11 +1089,9 @@ func (conn *Connection) send(req Request, streamId uint64) *Future {
return fut
default:
}
}
conn.putFuture(fut, req, streamId)
if req.Ctx() != nil {
go conn.contextWatchdog(fut, req.Ctx())
}
conn.putFuture(fut, req, streamId)
return fut
}

Expand Down Expand Up @@ -1310,15 +1304,6 @@ func (conn *Connection) Do(req Request) *Future {
return fut
}
}
if req.Ctx() != nil {
select {
case <-req.Ctx().Done():
fut := NewFuture()
fut.SetError(fmt.Errorf("context is done"))
return fut
default:
}
}
return conn.send(req, ignoreStreamId)
}

Expand Down

0 comments on commit 091b938

Please sign in to comment.