Skip to content

Commit 6c2f319

Browse files
committed
code health: simplify context checks
We don't need to over-optimize the work with requests with contexts that already done. It doesn't look like a use case. It is better to simplify the code. Part of #244
1 parent 3d37409 commit 6c2f319

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

connection.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,22 +1062,18 @@ func (conn *Connection) newFuture(ctx context.Context) (fut *Future) {
10621062
}
10631063

10641064
// This method removes a future from the internal queue if the context
1065-
// is "done" before the response is come. Such select logic is inspired
1066-
// from this thread: https://groups.google.com/g/golang-dev/c/jX4oQEls3uk
1065+
// is "done" before the response is come.
10671066
func (conn *Connection) contextWatchdog(fut *Future, ctx context.Context) {
10681067
select {
10691068
case <-fut.done:
1069+
case <-ctx.Done():
1070+
}
1071+
1072+
select {
1073+
case <-fut.done:
1074+
return
10701075
default:
1071-
select {
1072-
case <-ctx.Done():
1073-
conn.cancelFuture(fut, fmt.Errorf("context is done"))
1074-
default:
1075-
select {
1076-
case <-fut.done:
1077-
case <-ctx.Done():
1078-
conn.cancelFuture(fut, fmt.Errorf("context is done"))
1079-
}
1080-
}
1076+
conn.cancelFuture(fut, fmt.Errorf("context is done"))
10811077
}
10821078
}
10831079

@@ -1093,11 +1089,9 @@ func (conn *Connection) send(req Request, streamId uint64) *Future {
10931089
return fut
10941090
default:
10951091
}
1096-
}
1097-
conn.putFuture(fut, req, streamId)
1098-
if req.Ctx() != nil {
10991092
go conn.contextWatchdog(fut, req.Ctx())
11001093
}
1094+
conn.putFuture(fut, req, streamId)
11011095
return fut
11021096
}
11031097

@@ -1310,15 +1304,6 @@ func (conn *Connection) Do(req Request) *Future {
13101304
return fut
13111305
}
13121306
}
1313-
if req.Ctx() != nil {
1314-
select {
1315-
case <-req.Ctx().Done():
1316-
fut := NewFuture()
1317-
fut.SetError(fmt.Errorf("context is done"))
1318-
return fut
1319-
default:
1320-
}
1321-
}
13221307
return conn.send(req, ignoreStreamId)
13231308
}
13241309

0 commit comments

Comments
 (0)