Skip to content

Commit

Permalink
v2 clean up subscriptions (redhat-cne#102)
Browse files Browse the repository at this point in the history
Add support for deleting all subscriptions

Signed-off-by: Jack Ding <[email protected]>
  • Loading branch information
jzding committed Aug 28, 2024
1 parent 16ff1b5 commit 1d6d0e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
33 changes: 23 additions & 10 deletions v1/subscriber/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,32 @@ func (p *API) DeleteSubscription(clientID uuid.UUID, subscriptionID string) erro
return nil
}

// DeleteAllSubscriptions delete all subscriptionOne information
func (p *API) DeleteAllSubscriptions(clientID uuid.UUID) error {
if subStore, ok := p.SubscriberStore.Get(clientID); ok {
if err := deleteAllFromFile(fmt.Sprintf("%s/%s", p.storeFilePath, fmt.Sprintf("%s.json", clientID))); err != nil {
return err
// DeleteAllSubscriptionsForClient delete all subscriptions for the client
func (p *API) DeleteAllSubscriptionsForClient(clientID uuid.UUID) (int, error) {
var err error
var numSubDeleted, numSubToDelete int
if sub, ok := p.SubscriberStore.Get(clientID); ok {
numSubToDelete = len(sub.SubStore.Store)
if err = p.DeleteClient(clientID); err != nil {
return 0, err
}
subStore.SubStore = &store.PubSubStore{
RWMutex: sync.RWMutex{},
Store: map[string]*pubsub.PubSub{},
numSubDeleted += numSubToDelete
}
return numSubDeleted, nil
}

// DeleteAllSubscriptions delete all subscriptions in store
func (p *API) DeleteAllSubscriptions() (int, error) {
var err error
var numSubDeleted, numSubToDelete int
for clientID, subs := range p.SubscriberStore.Store {
numSubToDelete = len(subs.SubStore.Store)
if err = p.DeleteClient(clientID); err != nil {
return numSubDeleted, err
}
p.SubscriberStore.Set(clientID, subStore)
numSubDeleted += numSubToDelete
}
return nil
return numSubDeleted, nil
}

// DeleteClient delete all subscriptionOne information
Expand Down
4 changes: 2 additions & 2 deletions v1/subscriber/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestAPI_DeleteAllSubscriptions(t *testing.T) {
assert.Nil(t, e)
assert.NotEmpty(t, s.ClientID)
assert.NotNil(t, s.SubStore.Store)
e = globalInstance.DeleteAllSubscriptions(clientID)
_, e = globalInstance.DeleteAllSubscriptionsForClient(clientID)
assert.Nil(t, e)
b, e := globalInstance.GetSubscriptionsFromFile(clientID)
assert.Nil(t, e)
Expand Down Expand Up @@ -217,7 +217,7 @@ func Test_Concurrency(t *testing.T) {
}

func clean() {
_ = globalInstance.DeleteAllSubscriptions(clientID)
globalInstance.DeleteAllSubscriptionsForClient(clientID) //nolint
}

func TestTeardown(*testing.T) {
Expand Down

0 comments on commit 1d6d0e7

Please sign in to comment.