Skip to content

Commit b11ee1e

Browse files
[MM-141]: Made access to KV store atomically safe when saving/removing subscriptions (#840)
1 parent e95f21a commit b11ee1e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

server/plugin/plugin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type kvStore interface {
6868
ListKeys(page int, count int, options ...pluginapi.ListKeysOption) ([]string, error)
6969
Get(key string, o any) error
7070
Delete(key string) error
71+
SetAtomicWithRetries(key string, valueFunc func(oldValue []byte) (newValue interface{}, err error)) error
7172
}
7273

7374
type Plugin struct {
@@ -169,7 +170,7 @@ func (p *Plugin) GetGitHubClient(ctx context.Context, userID string) (*github.Cl
169170
return p.githubConnectUser(ctx, userInfo), nil
170171
}
171172

172-
func (p *Plugin) githubConnectUser(ctx context.Context, info *GitHubUserInfo) *github.Client {
173+
func (p *Plugin) githubConnectUser(_ context.Context, info *GitHubUserInfo) *github.Client {
173174
tok := *info.Token
174175
return p.githubConnectToken(tok)
175176
}

server/plugin/subscriptions.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plugin
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"sort"
78
"strconv"
@@ -311,11 +312,14 @@ func (p *Plugin) GetSubscriptions() (*Subscriptions, error) {
311312
}
312313

313314
func (p *Plugin) StoreSubscriptions(s *Subscriptions) error {
314-
if _, err := p.store.Set(SubscriptionsKey, s); err != nil {
315-
return errors.Wrap(err, "could not store subscriptions in KV store")
316-
}
315+
return p.store.SetAtomicWithRetries(SubscriptionsKey, func(_ []byte) (interface{}, error) {
316+
modifiedBytes, err := json.Marshal(s)
317+
if err != nil {
318+
return nil, errors.Wrap(err, "could not store subscriptions in KV store")
319+
}
317320

318-
return nil
321+
return modifiedBytes, nil
322+
})
319323
}
320324

321325
func (p *Plugin) GetSubscribedChannelsForRepository(repo *github.Repository) []*Subscription {

0 commit comments

Comments
 (0)