~sircmpwn/core-go

3902e4bd25662f47e356df8ff29eab1393d413be — Simon Ser 6 months ago 86ad9b5
database: don't panic on context.DeadlineExceeded

Checking context.Cancelled is not enough, I also see the same issue
with context.DeadlineExceeded:

    panic: Transaction error: driver: bad connection
    Closure error: context deadline exceeded

    goroutine 66664 [running]:
    git.sr.ht/~sircmpwn/core-go/database.WithTx({0x5578e1847a58, 0xc0002c44e0}, 0x50?, 0xc000285f38)
            e7b8e02696a1">git.sr.ht/~sircmpwn/core-go@v0.0.0-20231129165057-e7b8e02696a1/database/middleware.go:74 +0x2ce
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.fetchUsersByID.func1({0xc0002cf190, 0x1, 0x1})
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/middleware.go:34 +0x118
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).end(...)
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:222
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).startTimer(0xc00044a360, 0xc00042d380)
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:218 +0xe7
    created by git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).keyIndex
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:191 +0x119
1 files changed, 2 insertions(+), 1 deletions(-)

M database/middleware.go
M database/middleware.go => database/middleware.go +2 -1
@@ 63,7 63,8 @@ func WithTx(ctx context.Context, opts *sql.TxOptions, fn func(tx *sql.Tx) error)
		txErr = tx.Commit()
	}

	if errors.Is(err, context.Canceled) && errors.Is(txErr, sqldriver.ErrBadConn) {
	if (errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)) &&
		errors.Is(txErr, sqldriver.ErrBadConn) {
		// When a query fails because the context has been canceled, pq will
		// return "driver: bad connection" from tx.Rollback. Do not panic in
		// this case. See https://github.com/lib/pq/issues/1137