Close db iterators when context is cancelled
This commit is contained in:
parent
b41a556fcd
commit
94457aeb93
17
src/db/db.go
17
src/db/db.go
|
@ -234,11 +234,24 @@ func Query(ctx context.Context, conn ConnOrTx, destExample interface{}, query st
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &StructQueryIterator{
|
it := &StructQueryIterator{
|
||||||
fieldPaths: fieldPaths,
|
fieldPaths: fieldPaths,
|
||||||
rows: rows,
|
rows: rows,
|
||||||
destType: destType,
|
destType: destType,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
// Ensure that iterators are closed if context is cancelled. Otherwise, iterators can hold
|
||||||
|
// open connections even after a request is cancelled, causing the app to deadlock.
|
||||||
|
go func() {
|
||||||
|
done := ctx.Done()
|
||||||
|
if done == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
<-done
|
||||||
|
it.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return it, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getColumnNamesAndPaths(destType reflect.Type, pathSoFar []int, prefix string) (names []string, paths [][]int, err error) {
|
func getColumnNamesAndPaths(destType reflect.Type, pathSoFar []int, prefix string) (names []string, paths [][]int, err error) {
|
||||||
|
|
Loading…
Reference in New Issue