Skip to content

Commit

Permalink
db: move some defer rows.Close() to after error check
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Apr 11, 2024
1 parent da0fb48 commit 9044aa9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type MediaRequestMethod string

const (
MediaRequestMethodImmediate MediaRequestMethod = "immediate"
MediaRequestMethodLocalTime = "local_time"
MediaRequestMethodLocalTime MediaRequestMethod = "local_time"
)

type BridgeConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion database/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ func (b *Backfill) Insert() {
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
RETURNING queue_id
`, b.UserID, b.BackfillType, b.Priority, b.Portal.JID, b.Portal.Receiver, b.TimeStart, b.MaxBatchEvents, b.MaxTotalEvents, b.BatchDelay, b.DispatchTime, b.CompletedAt)
defer rows.Close()
if err != nil || !rows.Next() {
b.log.Warnfln("Failed to insert %v/%s with priority %d: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
return
}
defer rows.Close()
err = rows.Scan(&b.QueueID)
if err != nil {
b.log.Warnfln("Failed to insert %s/%s with priority %s: %v", b.BackfillType, b.Portal.JID, b.Priority, err)
Expand Down
6 changes: 3 additions & 3 deletions database/historysync.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ func (hsq *HistorySyncQuery) GetNMostRecentConversations(userID id.UserID, n int
nPtr = nil
}
rows, err := hsq.db.Query(getNMostRecentConversations, userID, nPtr)
defer rows.Close()
if err != nil || rows == nil {
return nil
}
defer rows.Close()
for rows.Next() {
conversations = append(conversations, hsq.NewConversation().Scan(rows))
}
Expand All @@ -185,10 +185,10 @@ func (hsq *HistorySyncQuery) GetNMostRecentConversations(userID id.UserID, n int

func (hsq *HistorySyncQuery) GetConversation(userID id.UserID, portalKey *PortalKey) (conversation *HistorySyncConversation) {
rows, err := hsq.db.Query(getConversationByPortal, userID, portalKey.JID, portalKey.Receiver)
defer rows.Close()
if err != nil || rows == nil {
return nil
}
defer rows.Close()
if rows.Next() {
conversation = hsq.NewConversation().Scan(rows)
}
Expand Down Expand Up @@ -274,13 +274,13 @@ func (hsq *HistorySyncQuery) GetMessagesBetween(userID id.UserID, conversationID
}

rows, err := hsq.db.Query(fmt.Sprintf(getMessagesBetween, whereClauses, limitClause), args...)
defer rows.Close()
if err != nil || rows == nil {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
hsq.log.Warnfln("Failed to query messages between range: %v", err)
}
return nil
}
defer rows.Close()

var msgData []byte
for rows.Next() {
Expand Down
2 changes: 1 addition & 1 deletion database/mediabackfillrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ func (mbr *MediaBackfillRequest) Scan(row dbutil.Scannable) *MediaBackfillReques

func (mbrq *MediaBackfillRequestQuery) GetMediaBackfillRequestsForUser(userID id.UserID) (requests []*MediaBackfillRequest) {
rows, err := mbrq.db.Query(getMediaBackfillRequestsForUser, userID)
defer rows.Close()
if err != nil || rows == nil {
return nil
}
defer rows.Close()
for rows.Next() {
requests = append(requests, mbrq.newMediaBackfillRequest().Scan(rows))
}
Expand Down

0 comments on commit 9044aa9

Please sign in to comment.