Skip to content

Commit

Permalink
v2 clean up subscriptions (#77)
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Ding <[email protected]>
  • Loading branch information
jzding authored Aug 16, 2024
1 parent 44062f4 commit d6f8679
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.14.0
github.com/redhat-cne/sdk-go v1.0.1-0.20240809152305-d59009827df0
github.com/redhat-cne/sdk-go v1.0.1-0.20240816174822-51aca34ab94e
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/redhat-cne/sdk-go v1.0.1-0.20240809152305-d59009827df0 h1:1dHxOlK41SQ4OCpMk+D0vM141zLVNplLooCTLk4F/wc=
github.com/redhat-cne/sdk-go v1.0.1-0.20240809152305-d59009827df0/go.mod h1:q9LxxPbK1tGpDbQm/KIPujqdP0bK1hhuHrIXV3vuUrM=
github.com/redhat-cne/sdk-go v1.0.1-0.20240816174822-51aca34ab94e h1:Viv1sLn0gru6kj98DwEoWtw3Kx/NF0LtmNCq0zR0VxU=
github.com/redhat-cne/sdk-go v1.0.1-0.20240816174822-51aca34ab94e/go.mod h1:q9LxxPbK1tGpDbQm/KIPujqdP0bK1hhuHrIXV3vuUrM=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
Expand Down
49 changes: 26 additions & 23 deletions v2/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,6 @@ func (s *Server) sendOut(eType channel.Type, sub *pubsub.PubSub) {
}
}

func (s *Server) sendOutToDelete(eType channel.Type, sub *pubsub.PubSub) {
// go ahead and create QDR to this address
s.dataOut <- &channel.DataChan{
ID: sub.GetID(),
Address: sub.GetResource(),
Data: &ce.Event{},
Type: eType,
Status: channel.DELETE,
}
}

func (s *Server) getSubscriptionByID(w http.ResponseWriter, r *http.Request) {
queries := mux.Vars(r)
subscriptionID, ok := queries["subscriptionid"]
Expand Down Expand Up @@ -340,9 +329,10 @@ func (s *Server) deleteSubscription(w http.ResponseWriter, r *http.Request) {
for _, subs := range s.subscriberAPI.SubscriberStore.Store {
cevent, _ := subs.CreateCloudEvents()
out := channel.DataChan{
Data: cevent,
Status: channel.SUCCESS,
Type: channel.SUBSCRIBER,
ClientID: subs.GetClientID(),
Data: cevent,
Status: channel.SUCCESS,
Type: channel.SUBSCRIBER,
}
s.dataOut <- &out
}
Expand All @@ -352,18 +342,31 @@ func (s *Server) deleteSubscription(w http.ResponseWriter, r *http.Request) {
}

func (s *Server) deleteAllSubscriptions(w http.ResponseWriter, _ *http.Request) {
size := len(s.pubSubAPI.GetSubscriptions())
if err := s.pubSubAPI.DeleteAllSubscriptions(); err != nil {
// update configMap
for _, subs := range s.subscriberAPI.SubscriberStore.Store {
cevent, _ := subs.CreateCloudEvents()
out := channel.DataChan{
ClientID: subs.GetClientID(),
Data: cevent,
Status: channel.DELETE,
Type: channel.SUBSCRIBER,
}
s.dataOut <- &out
}

numSubDeleted, err := s.subscriberAPI.DeleteAllSubscriptions()

// Subscriptions could be partially deleted when there were errors
if numSubDeleted > 0 {
localmetrics.UpdateSubscriptionCount(localmetrics.ACTIVE, -(numSubDeleted))
}

if err != nil {
respondWithError(w, err.Error())
return
}
//update metrics
if size > 0 {
localmetrics.UpdateSubscriptionCount(localmetrics.ACTIVE, -(size))
}
// go ahead and create QDR to this address
s.sendOutToDelete(channel.SUBSCRIBER, &pubsub.PubSub{ID: "", Resource: "delete-all-subscriptions"})
respondWithMessage(w, http.StatusOK, "deleted all subscriptions")

respondWithStatusCode(w, http.StatusNoContent, "")
}

func (s *Server) deleteAllPublishers(w http.ResponseWriter, _ *http.Request) {
Expand Down
33 changes: 23 additions & 10 deletions vendor/github.com/redhat-cne/sdk-go/v1/subscriber/subscriber.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ github.com/prometheus/common/model
github.com/prometheus/procfs
github.com/prometheus/procfs/internal/fs
github.com/prometheus/procfs/internal/util
# github.com/redhat-cne/sdk-go v1.0.1-0.20240809152305-d59009827df0
# github.com/redhat-cne/sdk-go v1.0.1-0.20240816174822-51aca34ab94e
## explicit; go 1.22
github.com/redhat-cne/sdk-go/pkg/channel
github.com/redhat-cne/sdk-go/pkg/common
Expand Down

0 comments on commit d6f8679

Please sign in to comment.