Skip to content

Commit f96feaa

Browse files
committed
short circuit for context.Background()
1 parent 1fdad70 commit f96feaa

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

connection.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ type mysqlConn struct {
4343
sequence uint8
4444
parseTime bool
4545
strict bool
46-
watcher chan<- mysqlContext
47-
closech chan struct{}
48-
finished chan<- struct{}
46+
47+
// for context support (From Go 1.8)
48+
watching bool
49+
watcher chan<- mysqlContext
50+
closech chan struct{}
51+
finished chan<- struct{}
4952

5053
// set non-zero when conn is closed, before closech is closed.
5154
// accessed atomically.
@@ -444,11 +447,12 @@ func (mc *mysqlConn) canceled() error {
444447

445448
// finish is called when the query has succeeded.
446449
func (mc *mysqlConn) finish() {
447-
if mc.finished == nil {
450+
if !mc.watching || mc.finished == nil {
448451
return
449452
}
450453
select {
451454
case mc.finished <- struct{}{}:
455+
mc.watching = false
452456
case <-mc.closech:
453457
}
454458
}

connection_go18.go

+11
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ func (stmt *mysqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue
166166
}
167167

168168
func (mc *mysqlConn) watchCancel(ctx context.Context) error {
169+
if mc.watching {
170+
err := errors.New("mysql: illegal watching state")
171+
errLog.Print(err)
172+
mc.cleanup()
173+
return err
174+
}
175+
if ctx.Done() == nil {
176+
return nil
177+
}
178+
179+
mc.watching = true
169180
select {
170181
default:
171182
case <-ctx.Done():

0 commit comments

Comments
 (0)