Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: slack app cache (#13571)

p0nyed2136b2a0fd
diff --git a/backend/plugin/webhook/slack/app.go b/backend/plugin/webhook/slack/app.go
index 13e34c68d9..ffacc5281b 100644
--- a/backend/plugin/webhook/slack/app.go
+++ b/backend/plugin/webhook/slack/app.go
@@ -8,6 +8,7 @@ import (
"net/url"
"strings"
+ lru "github.com/hashicorp/golang-lru/v2"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/webhook"
@@ -104,8 +105,21 @@ func (p *provider) authTest(ctx context.Context) error {
return nil
}
+var userIDCache = func() *lru.Cache[string, string] {
+ cache, err := lru.New[string, string](5000)
+ if err != nil {
+ panic(err)
+ }
+ return cache
+}()
+
// https://api.slack.com/methods/users.lookupByEmail
-func (p *provider) lookupByEmail(ctx context.Context, email string) (string, error) {
+// id="" indicates that the user is not found.
+func (p *provider) lookupByEmail(ctx context.Context, email string) (id string, err error) {
+ if id, ok := userIDCache.Get(email); ok {
+ return id, nil
+ }
+
q := url.Values{}
q.Set("email", email)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://slack.com/api/users.lookupByEmail", nil)
@@ -135,9 +149,14 @@ func (p *provider) lookupByEmail(ctx context.Context, email string) (string, err
return "", errors.Wrapf(err, "failed to unmarshal")
}
if !res.OK {
+ if strings.Contains(res.Error, "users_not_found") {
+ userIDCache.Add(email, "")
+ return "", nil
+ }
return "", errors.Errorf("failed to get user, error: %v", res.Error)
}
+ userIDCache.Add(email, res.User.ID)
return res.User.ID, nil
}
diff --git a/backend/plugin/webhook/slack/slack.go b/backend/plugin/webhook/slack/slack.go
index e26e27ceb3..b5782306d5 100644
--- a/backend/plugin/webhook/slack/slack.go
+++ b/backend/plugin/webhook/slack/slack.go
@@ -8,7 +8,6 @@ import (
"io"
"log/slog"
"net/http"
- "strings"
"github.com/pkg/errors"
"go.uber.org/multierr"
@@ -191,11 +190,11 @@ func postDirectMessage(webhookCtx webhook.Context) {
err := func() error {
userID, err := p.lookupByEmail(ctx, u.Email)
if err != nil {
- if strings.Contains(err.Error(), "users_not_found") {
- return nil
- }
return errors.Wrapf(err, "failed to lookup user")
}
+ if userID == "" {
+ return nil
+ }
channelID, err := p.openConversation(ctx, userID)
if err != nil {
return errors.Wrapf(err, "failed to open conversation")