Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3d535f8

Browse files
committedFeb 10, 2020
Merge branch '100-race-on-creation' into 'master'
- fix the data race on user creation (#100) - update the app version See merge request postgres-ai/joe!81
2 parents 57db432 + 44ca340 commit 3d535f8

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed
 

‎cmd/joe/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var opts struct {
6262
}
6363

6464
// TODO (akartasov): Set the app version during build.
65-
const Version = "v0.4-rc1"
65+
const Version = "v0.4.0"
6666

6767
// TODO(anatoly): Refactor configs and envs.
6868

‎pkg/bot/bot.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net/http"
1717
"regexp"
1818
"strings"
19+
"sync"
1920
"time"
2021

2122
"github.com/dustin/go-humanize/english"
@@ -113,7 +114,7 @@ const MSG_HELP = "• `explain` — analyze your query (SELECT, INSERT, DELETE,
113114
"• `\\d`, `\\d+`, `\\dt`, `\\dt+`, `\\di`, `\\di+`, `\\l`, `\\l+`, `\\dv`, `\\dv+`, `\\dm`, `\\dm+` — psql meta information commands\n" +
114115
"• `help` — this message\n"
115116

116-
const MsgSessionStarting = "Starting new session...\n\n"
117+
const MsgSessionStarting = "Starting new session...\n"
117118

118119
const MsgSessionForewordTpl = "• Say 'help' to see the full list of commands.\n" +
119120
"• Sessions are fully independent. Feel free to do anything.\n" +
@@ -173,10 +174,11 @@ type DBLabInstance struct {
173174
}
174175

175176
type Bot struct {
176-
Config config.Bot
177-
Chat *chatapi.Chat
178-
DBLab *dblabapi.Client
179-
Users map[string]*User // Slack UID -> User.
177+
Config config.Bot
178+
Chat *chatapi.Chat
179+
DBLab *dblabapi.Client
180+
usersMutex sync.RWMutex
181+
Users map[string]*User // Slack UID -> User.
180182
}
181183

182184
type Audit struct {
@@ -400,21 +402,17 @@ func (b *Bot) processMessageEvent(ev *slackevents.MessageEvent) {
400402
message = strings.TrimRight(message, "`")
401403

402404
// Get user or create a new one.
403-
user, ok := b.Users[ev.User]
404-
if !ok {
405-
chatUser, err := b.Chat.GetUserInfo(ev.User)
406-
if err != nil {
407-
log.Err(err)
405+
user, err := b.createUser(ev.User)
406+
if err != nil {
407+
log.Err(errors.Wrap(err, "failed to get user"))
408408

409-
msg, _ := b.Chat.NewMessage(ch)
410-
msg.Publish(" ")
411-
msg.Fail(err.Error())
412-
return
413-
}
409+
msg, _ := b.Chat.NewMessage(ch)
410+
msg.Publish(" ")
411+
msg.Fail(err.Error())
414412

415-
user = NewUser(chatUser, b.Config)
416-
b.Users[ev.User] = user
413+
return
417414
}
415+
418416
user.Session.LastActionTs = time.Now()
419417
if !util.Contains(user.Session.ChannelIDs, ch) {
420418
user.Session.ChannelIDs = append(user.Session.ChannelIDs, ch)

‎pkg/bot/user.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package bot
2+
3+
import (
4+
"github.com/pkg/errors"
5+
)
6+
7+
func (b *Bot) createUser(userID string) (*User, error) {
8+
user, ok := b.findUser(userID)
9+
if ok {
10+
return user, nil
11+
}
12+
13+
chatUser, err := b.Chat.GetUserInfo(userID)
14+
if err != nil {
15+
return nil, errors.Wrap(err, "failed to get user info")
16+
}
17+
18+
user = NewUser(chatUser, b.Config)
19+
20+
if err := b.addUser(userID, user); err != nil {
21+
return nil, errors.Wrap(err, "failed to add user")
22+
}
23+
24+
return user, nil
25+
}
26+
27+
func (b *Bot) findUser(userID string) (*User, bool) {
28+
b.usersMutex.RLock()
29+
user, ok := b.Users[userID]
30+
b.usersMutex.RUnlock()
31+
32+
return user, ok
33+
}
34+
35+
func (b *Bot) addUser(userID string, user *User) error {
36+
b.usersMutex.Lock()
37+
defer b.usersMutex.Unlock()
38+
39+
if _, ok := b.Users[userID]; ok {
40+
return errors.Errorf("user %q already exists", userID)
41+
}
42+
43+
b.Users[userID] = user
44+
45+
return nil
46+
}

0 commit comments

Comments
 (0)
Please sign in to comment.