/
subscriptions.go
122 lines (100 loc) · 2.48 KB
/
subscriptions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package esdb
import (
"context"
"fmt"
"log"
"sync"
api "github.com/EventStore/EventStore-Client-Go/protos/streams"
)
type request struct {
channel chan *SubscriptionEvent
}
type Subscription struct {
client *Client
id string
inner api.Streams_ReadClient
channel chan request
cancel context.CancelFunc
once *sync.Once
}
func NewSubscription(client *Client, cancel context.CancelFunc, inner api.Streams_ReadClient, id string) *Subscription {
channel := make(chan request)
once := new(sync.Once)
// It is not safe to consume a stream in different goroutines. This is why we only consume
// the stream in a dedicated goroutine.
//
// Current implementation doesn't terminate the goroutine. When a subscription is dropped,
// we keep user requests coming but will always send back a subscription dropped event.
// This implementation is simple to maintain while letting the user sharing their subscription
// among as many goroutines as they want.
go func() {
closed := false
for {
req := <-channel
if closed {
req.channel <- &SubscriptionEvent{
SubscriptionDropped: &SubscriptionDropped{
Error: fmt.Errorf("subscription has been dropped"),
},
}
continue
}
result, err := inner.Recv()
if err != nil {
log.Printf("[error] subscription has dropped. Reason: %v", err)
dropped := SubscriptionDropped{
Error: err,
}
req.channel <- &SubscriptionEvent{
SubscriptionDropped: &dropped,
}
closed = true
continue
}
switch result.Content.(type) {
case *api.ReadResp_Checkpoint_:
{
checkpoint := result.GetCheckpoint()
position := Position{
Commit: checkpoint.CommitPosition,
Prepare: checkpoint.PreparePosition,
}
req.channel <- &SubscriptionEvent{
CheckPointReached: &position,
}
}
case *api.ReadResp_Event:
{
resolvedEvent := getResolvedEventFromProto(result.GetEvent())
req.channel <- &SubscriptionEvent{
EventAppeared: &resolvedEvent,
}
}
}
}
}()
return &Subscription{
client: client,
id: id,
inner: inner,
channel: channel,
once: once,
cancel: cancel,
}
}
func (sub *Subscription) Id() string {
return sub.id
}
func (sub *Subscription) Close() error {
sub.once.Do(sub.cancel)
return nil
}
func (sub *Subscription) Recv() *SubscriptionEvent {
channel := make(chan *SubscriptionEvent)
req := request{
channel: channel,
}
sub.channel <- req
resp := <-channel
return resp
}