Skip to content

Commit 7fbfe9b

Browse files
committed
Some sessions improvements and tests
1 parent f515c0f commit 7fbfe9b

8 files changed

Lines changed: 326 additions & 53 deletions

File tree

api/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func ReadRequest(c context.Context, w http.ResponseWriter, r heligo.Request) (re
314314
// check preconditions
315315
var zero bool
316316
sc := database.GetSmoothContext(c)
317-
options := sc.QueryOptions
317+
options := &sc.QueryOptions
318318
if r.Method == "POST" {
319319
// [] as input cause no inserts
320320
if zero, status, err = writeIfZeroRecordsToInsert(w, records, options); zero {
@@ -338,7 +338,7 @@ func ReadRequest(c context.Context, w http.ResponseWriter, r heligo.Request) (re
338338
// in the response (eg 416 for RequestedRangeNotSatisfiable)
339339
func SetResponseHeaders(ctx context.Context, w http.ResponseWriter, r heligo.Request, count int64) int {
340340
sc := database.GetSmoothContext(ctx)
341-
options := sc.QueryOptions
341+
options := &sc.QueryOptions
342342
// @@ must check if the table has a pk
343343
w.Header().Set("Content-Location", r.RequestURI)
344344
// Content-Range

authn/middleware.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,27 @@ func (m middleware) acquireSession(ctx context.Context, r heligo.Request,
6666
} else {
6767
db = session.Db
6868
}
69-
if session.DbConn == nil {
69+
if session.DbConn == nil || session.DbConn.Conn().PgConn().IsClosed() {
70+
if session.DbConn != nil {
71+
// Connection went stale while held by the session — release it
72+
session.DbConn.Release()
73+
session.DbConn = nil
74+
}
7075
dbconn, err = database.AcquireConnection(ctx, db)
7176
if err != nil {
7277
return nil, nil, http.StatusInternalServerError, err
7378
}
7479
session.DbConn = dbconn
7580
newAcquire = true
76-
77-
claimsString = session.Claims.RawClaims
7881
} else {
7982
dbconn = session.DbConn
8083
}
84+
claimsString = session.Claims.RawClaims
8185
err = database.PrepareConnection(ctx, dbconn, session.Claims.Role, claimsString, newAcquire)
8286
if err != nil {
8387
return nil, nil, http.StatusInternalServerError, err
8488
}
8589
ctx = database.FillContext(ctx, r.Request, db, dbconn.Conn(), session.Claims.Role)
86-
if err != nil {
87-
return nil, nil, http.StatusInternalServerError, err
88-
}
8990
return ctx, session, http.StatusOK, nil
9091
}
9192

@@ -98,7 +99,7 @@ func (m middleware) releaseSession(ctx context.Context, status int, session *Ses
9899
// Otherwise it will be released in the sessionmanager after a cer
99100
if !m.SessionManager().enabled {
100101
err = database.ReleaseConnection(ctx, session.DbConn, httpErr, true)
101-
} else if database.HasTX(session.DbConn) {
102+
} else if session.DbConn != nil && database.HasTX(session.DbConn) {
102103
err = database.ReleaseConnection(ctx, session.DbConn, httpErr, false)
103104
session.DbConn = nil
104105
}

0 commit comments

Comments
 (0)