Skip to content

Commit

Permalink
Merge pull request #530 from navytux/y/no-go-if-notneeded
Browse files Browse the repository at this point in the history
Don't spawn interrupt goroutine if we know that context cannot be canceled
  • Loading branch information
mattn authored Feb 18, 2018
2 parents 2a20b89 + 00a23ba commit 696e2e4
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,18 +1124,20 @@ func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows,
done: make(chan struct{}),
}

go func(db *C.sqlite3) {
select {
case <-ctx.Done():
if ctxdone := ctx.Done(); ctxdone != nil {
go func(db *C.sqlite3) {
select {
case <-ctxdone:
select {
case <-rows.done:
default:
C.sqlite3_interrupt(db)
rows.Close()
}
case <-rows.done:
default:
C.sqlite3_interrupt(db)
rows.Close()
}
case <-rows.done:
}
}(s.c.db)
}(s.c.db)
}

return rows, nil
}
Expand Down Expand Up @@ -1169,19 +1171,21 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
return nil, err
}

done := make(chan struct{})
defer close(done)
go func(db *C.sqlite3) {
select {
case <-done:
case <-ctx.Done():
if ctxdone := ctx.Done(); ctxdone != nil {
done := make(chan struct{})
defer close(done)
go func(db *C.sqlite3) {
select {
case <-done:
default:
C.sqlite3_interrupt(db)
case <-ctxdone:
select {
case <-done:
default:
C.sqlite3_interrupt(db)
}
}
}
}(s.c.db)
}(s.c.db)
}

var rowid, changes C.longlong
rv := C._sqlite3_step(s.s, &rowid, &changes)
Expand Down

0 comments on commit 696e2e4

Please sign in to comment.