@@ -5,7 +5,7 @@ of being pushed.
55It solves the problem, that you want to publish data to many goroutines. The
66standard way in go uses channels to push values to the readers. But channels
77have the problems, that either the goroutine sending the data has to wait for
8- the reader or has to discard messages,if the reader is too slow. A buffered
8+ the reader or has to discard messages, if the reader is too slow. A buffered
99channel can help to delay the problem, but eventually the buffer could be full.
1010
1111The idea of pulling updates is inspired by Kafka or Redis-Streams. A subscriber
@@ -44,30 +44,30 @@ ignored.
4444
4545# Receive messages
4646
47- Messages can be received with the Receive ()-method:
47+ Messages can be received with the ReceiveAll()- or ReceiveSince ()-method:
4848
49- id, values, err := top.Receive(context.Background(), 0)
49+ id, values := topic.ReceiveAll()
50+ id, values, err := top.ReceiveSince(context.Background(), 42)
5051
51- The first returned value is the id created by the last Publish()-call. The
52- second value is a slice of all messages that were published before.
52+ The returned id is the number of values in the topic. It can only increase.
5353
54- To receive newer values, Receive() can be called again with the id from the last
54+ The returned values are a slice of the published messages.
55+
56+ To receive newer values, ReceiveSince() can be called again with the id from the last
5557call:
5658
57- id, values, err := top.Receive(context.Background(), 0 )
59+ id, values, err := top.ReceiveAll( )
5860 ...
59- id, values, err = top.Receive (context.Background(), id)
61+ id, values, err = top.ReceiveSince (context.Background(), id)
6062
61- If the given id is zero, then all messages are returned. If the id is greater
62- than zero, then only messages are returned that were published by the topic
63- after the id was created.
63+ Only messages, that were published after the given id are returned.
6464
65- When there are no new values in the topic, then the Receive ()-call blocks until
65+ When there are no new values in the topic, then the ReceiveSince ()-call blocks until
6666there are new values. To add a timeout to the call, the context can be used:
6767
6868 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
6969 defer cancel()
70- id, values, err = top.Receive (ctx, id)
70+ id, values, err = top.ReceiveSince (ctx, id)
7171
7272If there are no new values before the context is canceled, the topic returns the error
7373of the context. For example `context.DeadlineExceeded` or `context.Canceled`.
@@ -81,7 +81,7 @@ The usual pattern to subscribe to a topic is:
8181 var values []string
8282 var err error
8383 for {
84- id, values, err = top.Receive (ctx, id)
84+ id, values, err = top.ReceiveSince (ctx, id)
8585 if err != nil {
8686 if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
8787 // Timeout
@@ -92,7 +92,7 @@ The usual pattern to subscribe to a topic is:
9292 // Process values
9393 }
9494
95- The loop will process all values published by the topic for one minute.
95+ The loop will process all values published to the topic for one minute.
9696
9797# Get Last ID
9898
@@ -101,18 +101,19 @@ should be processed that were published after the loop starts, the method
101101LastID() can be used:
102102
103103 id := top.LastID()
104- id, values, err = top.Receive (context.Background(), id)
104+ id, values, err = top.ReceiveSince (context.Background(), id)
105105
106- The return value of LastID() is the highest id in the topic. So a Receive() call
107- on top.LastID() will only return data that was published after the call.
106+ The return value of LastID() is the highest id in the topic. So
107+ a ReceiveSince() call on top.LastID() will only return data that
108+ was published after the call.
108109
109110A pattern to receive only new data is:
110111
111112 id := top.LastID()
112113 var values []string
113114 var err error
114115 for {
115- id, values, err = top.Receive (context.Background(), id)
116+ id, values, err = top.ReceiveSince (context.Background(), id)
116117 if err != nil {
117118 // Handle error
118119 }
0 commit comments