Skip to content

Commit

Permalink
fix connect race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
gardenerik committed Jul 28, 2024
1 parent 2e9e6d6 commit 6be41e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func ConnectSlack() {
return
}

for _, team := range teams {
for i, _ := range teams {
team := teams[i]
go team.Connect()
}
}
Expand Down
19 changes: 16 additions & 3 deletions slack_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"sync"

"github.com/charmbracelet/log"
"github.com/slack-go/slack"
Expand All @@ -17,7 +18,12 @@ type SlackTeamClient struct {
log *log.Logger
}

func ConnectTeam(team Team) error {
var connectLock sync.Mutex

func prepareTeam(team Team) (*socketmode.SocketmodeHandler, error) {
connectLock.Lock()
defer connectLock.Unlock()

logger := log.WithPrefix(fmt.Sprintf("[%s]", team.ID))
logger.Debug("Connecting...")

Expand All @@ -34,7 +40,7 @@ func ConnectTeam(team Team) error {

resp, err := api.AuthTest()
if err != nil {
return err
return nil, err
}
teamClient.BotUserID = resp.UserID

Expand All @@ -53,8 +59,15 @@ func ConnectTeam(team Team) error {
socketmodeHandler.Handle(socketmode.EventTypeHello, teamClient.handleHello)
socketmodeHandler.Handle(socketmode.EventTypeIncomingError, teamClient.handleError)
socketmodeHandler.Handle(socketmode.EventTypeConnectionError, teamClient.handleConnError)
return socketmodeHandler, nil
}

return socketmodeHandler.RunEventLoop()
func ConnectTeam(team Team) error {
handler, err := prepareTeam(team)
if err != nil {
return err
}
return handler.RunEventLoop()
}

func (st *SlackTeamClient) handleConnecting(evt *socketmode.Event, client *socketmode.Client) {
Expand Down

0 comments on commit 6be41e6

Please sign in to comment.