Skip to content

Commit

Permalink
Use UUID URN by default for event IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed May 13, 2020
1 parent aa03077 commit 1263271
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions hub/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestPublishGenerateUUID(t *testing.T) {
u := <-s.Receive()
require.NotNil(t, u)

_, err := uuid.FromString(u.ID)
_, err := uuid.FromString(strings.TrimPrefix(u.ID, "urn:uuid:"))
assert.Nil(t, err)
}()

Expand All @@ -244,7 +244,7 @@ func TestPublishGenerateUUID(t *testing.T) {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
body := string(bodyBytes)

_, err := uuid.FromString(body)
_, err := uuid.FromString(strings.TrimPrefix(body, "urn:uuid:"))
assert.Nil(t, err)

wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion hub/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (h *Hub) dispatchSubscriptionUpdate(s *Subscriber, active bool) {

for k, topic := range s.Topics {
connection := &subscription{
ID: "https://mercure.rocks/subscriptions/" + s.EscapedTopics[k] + "/" + s.ID,
ID: "https://mercure.rocks/subscriptions/" + s.EscapedTopics[k] + "/" + s.EscapedID,
Type: "https://mercure.rocks/Subscription",
Topic: topic,
Active: active,
Expand Down
1 change: 0 additions & 1 deletion hub/subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ func TestSubscriptionEvents(t *testing.T) {
ready := len(s.subscribers) == 2
s.RUnlock()

log.Info("Waiting for subscriber...")
if ready {
break
}
Expand Down
6 changes: 5 additions & 1 deletion hub/subscriber.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package hub

import (
"net/url"

"github.com/gofrs/uuid"
log "github.com/sirupsen/logrus"
"github.com/yosida95/uritemplate"
Expand All @@ -14,6 +16,7 @@ type updateSource struct {
// Subscriber represents a client subscribed to a list of topics.
type Subscriber struct {
ID string
EscapedID string
Claims *claims
Targets map[string]struct{}
Topics []string
Expand All @@ -35,9 +38,10 @@ type Subscriber struct {
}

func newSubscriber(lastEventID string) *Subscriber {
id := uuid.Must(uuid.NewV4()).String()
id := "urn:uuid:" + uuid.Must(uuid.NewV4()).String()
s := &Subscriber{
ID: id,
EscapedID: url.QueryEscape(id),
LastEventID: lastEventID,
LogFields: log.Fields{
"subscriber_id": id,
Expand Down
2 changes: 1 addition & 1 deletion hub/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newUpdate(event Event, topics []string, targets map[string]struct{}) *Updat
Targets: targets,
}
if u.ID == "" {
u.ID = uuid.Must(uuid.NewV4()).String()
u.ID = "urn:uuid:" + uuid.Must(uuid.NewV4()).String()
}

return u
Expand Down

0 comments on commit 1263271

Please sign in to comment.