Merge pull request #530 from navytux/y/no-go-if-notneeded

Don't spawn interrupt goroutine if we know that context cannot be canceled
This commit is contained in:
mattn 2018-02-18 22:15:52 +09:00 committed by GitHub
commit 696e2e43cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 19 deletions

View File

@ -1124,18 +1124,20 @@ func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows,
done: make(chan struct{}), done: make(chan struct{}),
} }
go func(db *C.sqlite3) { if ctxdone := ctx.Done(); ctxdone != nil {
select { go func(db *C.sqlite3) {
case <-ctx.Done():
select { select {
case <-ctxdone:
select {
case <-rows.done:
default:
C.sqlite3_interrupt(db)
rows.Close()
}
case <-rows.done: case <-rows.done:
default:
C.sqlite3_interrupt(db)
rows.Close()
} }
case <-rows.done: }(s.c.db)
} }
}(s.c.db)
return rows, nil return rows, nil
} }
@ -1169,19 +1171,21 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
return nil, err return nil, err
} }
done := make(chan struct{}) if ctxdone := ctx.Done(); ctxdone != nil {
defer close(done) done := make(chan struct{})
go func(db *C.sqlite3) { defer close(done)
select { go func(db *C.sqlite3) {
case <-done:
case <-ctx.Done():
select { select {
case <-done: case <-done:
default: case <-ctxdone:
C.sqlite3_interrupt(db) select {
case <-done:
default:
C.sqlite3_interrupt(db)
}
} }
} }(s.c.db)
}(s.c.db) }
var rowid, changes C.longlong var rowid, changes C.longlong
rv := C._sqlite3_step(s.s, &rowid, &changes) rv := C._sqlite3_step(s.s, &rowid, &changes)