Skip to content

Commit

Permalink
User "earliest" instead of "-1" as reversed value to retrieve all events
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed May 20, 2020
1 parent a82ebfd commit ba6656c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions hub/bolt_transport.go
Expand Up @@ -170,8 +170,8 @@ func (t *BoltTransport) dispatchHistory(s *Subscriber, toSeq uint64) {
}

c := b.Cursor()
afterFromID := s.LastEventID == "-1"
previousID := "-1"
afterFromID := s.LastEventID == EarliestLastEventID
previousID := EarliestLastEventID
for k, v := c.First(); k != nil; k, v = c.Next() {
if !afterFromID {
if string(k[8:]) == s.LastEventID {
Expand Down
2 changes: 1 addition & 1 deletion hub/bolt_transport_test.go
Expand Up @@ -59,7 +59,7 @@ func TestBoltTransportRetrieveAllHistory(t *testing.T) {
})
}

s := newSubscriber("-1", newTopicSelectorStore())
s := newSubscriber(EarliestLastEventID, newTopicSelectorStore())
s.Topics = topics
go s.start()

Expand Down
8 changes: 4 additions & 4 deletions hub/subscribe_test.go
Expand Up @@ -560,7 +560,7 @@ func TestSendAllEvents(t *testing.T) {
defer wg.Done()

ctx, cancel := context.WithCancel(context.Background())
req := httptest.NewRequest("GET", defaultHubURL+"?topic=http://example.com/foos/{id}&Last-Event-ID=-1", nil).WithContext(ctx)
req := httptest.NewRequest("GET", defaultHubURL+"?topic=http://example.com/foos/{id}&Last-Event-ID="+EarliestLastEventID, nil).WithContext(ctx)

w := &responseTester{
header: http.Header{},
Expand All @@ -571,15 +571,15 @@ func TestSendAllEvents(t *testing.T) {
}

hub.SubscribeHandler(w, req)
assert.Equal(t, "-1", w.Header().Get("Last-Event-ID"))
assert.Equal(t, EarliestLastEventID, w.Header().Get("Last-Event-ID"))
}()

go func() {
defer wg.Done()

ctx, cancel := context.WithCancel(context.Background())
req := httptest.NewRequest("GET", defaultHubURL+"?topic=http://example.com/foos/{id}", nil).WithContext(ctx)
req.Header.Add("Last-Event-ID", "-1")
req.Header.Add("Last-Event-ID", EarliestLastEventID)

w := &responseTester{
header: http.Header{},
Expand All @@ -590,7 +590,7 @@ func TestSendAllEvents(t *testing.T) {
}

hub.SubscribeHandler(w, req)
assert.Equal(t, "-1", w.Header().Get("Last-Event-ID"))
assert.Equal(t, EarliestLastEventID, w.Header().Get("Last-Event-ID"))
}()

wg.Wait()
Expand Down
3 changes: 3 additions & 0 deletions hub/transport.go
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/spf13/viper"
)

// EarliestLastEventID is the reserved value representing the earliest available event id.
const EarliestLastEventID = "earliest"

// Transport provides methods to dispatch and persist updates.
type Transport interface {
// Dispatch dispatches an update to all subscribers.
Expand Down
4 changes: 2 additions & 2 deletions spec/mercure.md
Expand Up @@ -425,13 +425,13 @@ If both the `Last-Event-ID` HTTP header and the query parameter are present, the
If the `Last-Event-ID` HTTP header or query parameter exists, the hub **SHOULD** send all events
published following the one bearing this identifier to the subscriber.

The reserved value `-1` can be used to hint the hub to send all updates it has for the subscribed
The reserved value `earliest` can be used to hint the hub to send all updates it has for the subscribed
topics. According to its own policy, the hub **MAY** or **MAY NOT** fulfil this request.

The hub **MAY** discard some events for operational reasons. If the hub is not able to send all
requested events, it **MUST** set a `Last-Event-ID` header on the HTTP response containing the id of
event preceding the first sent to the subscriber. If such event doesn't exist, the hub **MUST** set
the `Last-Event-ID` header it sends to the reserved value `-1`. This value indicates that all events
the `Last-Event-ID` header it sends to the reserved value `earliest`. This value indicates that all events
stored for the subscribed topics have been sent to the subscriber.

The subscriber **MUST NOT** assume that no events will be lost (it may happen, for example after
Expand Down

0 comments on commit ba6656c

Please sign in to comment.