Skip to content

Commit

Permalink
Fixed unstable SQL statement causing memory leaks due to prepared stm…
Browse files Browse the repository at this point in the history
…t caching
  • Loading branch information
ppacher committed Aug 3, 2022
1 parent 0a46f54 commit 37951c2
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion netquery/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -325,7 +326,16 @@ func (db *Database) Save(ctx context.Context, conn Conn) error {
values := make(map[string]interface{}, len(connMap))
updateSets := make([]string, 0, len(connMap))

for key, value := range connMap {
// sort keys so we get a stable SQLite query that can be better cached.
keys := make([]string, 0, len(connMap))
for key := range connMap {
keys = append(keys, key)
}
sort.Sort(sort.StringSlice(keys))

for _, key := range keys {
value := connMap[key]

columns = append(columns, key)
placeholders = append(placeholders, ":"+key)
values[":"+key] = value
Expand Down

0 comments on commit 37951c2

Please sign in to comment.