Skip to content

Commit

Permalink
Fixed a potential issue with the user cache
Browse files Browse the repository at this point in the history
Accidentally ran 2 websocket instances, and found a situation where a
user may not be in the cache, but exist on the backend, leaving a period
of time between creating a new user and propagating the cache update where
the bot may try to create a duplicate user ID.

Fixed it
  • Loading branch information
arcward committed Sep 22, 2024
1 parent 9dd73c1 commit 20f5bb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions disconcierge/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ func (d *database) GetOrCreateUser(
log = slog.Default()
}

if user, cachedUser := d.userCache[u.ID]; cachedUser {
user, cachedUser := d.userCache[u.ID]

if cachedUser {
// FIXME This isn't particularly concurrency-safe, as the cached
// record may be read by another goroutine while we're updating it.
log.InfoContext(ctx, "found existing user", "user", user)
Expand Down Expand Up @@ -247,10 +249,22 @@ func (d *database) GetOrCreateUser(
log.Error("error updating user", "user", user, tint.Err(err))
}
return user, false, nil
} else {
var existingUser User
err := d.db.Where("id = ?", u.ID).First(&existingUser).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.Error("error getting user", "user_id", u.ID, tint.Err(err))
return nil, false, err
}
if existingUser.ID == u.ID {
log.Info("found existing user", "user", existingUser)
user = &existingUser
d.userCache[u.ID] = user
return user, false, nil
}
}

log.Info("creating new user", "user", u)
user, _ := NewUser(u)
user, _ = NewUser(u)
if dc != nil {
config := dc.RuntimeConfig()
user.UserChatCommandLimit6h = config.UserChatCommandLimit6h
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/gorilla/securecookie v1.1.2
github.com/gorilla/sessions v1.2.2
github.com/jackc/pgx/v5 v5.4.3
github.com/joho/godotenv v1.5.1
github.com/lmittmann/tint v1.0.4
github.com/mitchellh/mapstructure v1.5.0
github.com/sashabaranov/go-openai v0.0.0-00010101000000-000000000000
Expand All @@ -22,6 +23,7 @@ require (
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.26.0
golang.org/x/sync v0.8.0
golang.org/x/term v0.24.0
golang.org/x/time v0.5.0
gorm.io/driver/postgres v1.5.7
gorm.io/driver/sqlite v1.5.5
Expand Down Expand Up @@ -55,7 +57,6 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand Down Expand Up @@ -83,7 +84,6 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down

0 comments on commit 20f5bb5

Please sign in to comment.