-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdatasource_sse.go
130 lines (108 loc) · 2.59 KB
/
datasource_sse.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
123
124
125
126
127
128
129
130
package growthbook
import (
"context"
"fmt"
"log/slog"
"net/http"
"time"
"github.com/tmaxmax/go-sse"
)
type SseDataSource struct {
client *Client
cancel context.CancelFunc
ready bool
retry time.Duration
logger *slog.Logger
}
const minbufsize = 64 * 1024
const maxbufsize = 10 * 1024 * 1024
func WithSseDataSource() ClientOption {
return func(c *Client) error {
c.data.dataSource = newSseDataSource(c)
return nil
}
}
func newSseDataSource(client *Client) *SseDataSource {
return &SseDataSource{
client: client,
logger: client.logger.With("source", "Growthbook SSE datasource"),
}
}
func (ds *SseDataSource) Start(ctx context.Context) error {
ds.logger.Info("Starting")
ctx, cancel := context.WithCancel(ctx)
ds.cancel = cancel
err := ds.loadData(ctx)
if err != nil {
return err
}
ds.logger.Info("First load finished")
ds.ready = true
go ds.connect(ctx)
ds.logger.Info("Started")
return nil
}
func (ds *SseDataSource) Close() error {
if !ds.ready {
return fmt.Errorf("Datasource is not ready")
}
ds.logger.Info("Closing")
ds.cancel()
return nil
}
func (ds *SseDataSource) connect(ctx context.Context) error {
sseUrl := ds.client.data.getSseUrl()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, sseUrl, http.NoBody)
if err != nil {
return err
}
ds.setReqHeaders(req)
sseClient := &sse.Client{
HTTPClient: ds.client.data.httpClient,
OnRetry: ds.onRetry,
}
sseConn := sseClient.NewConnection(req)
buf := make([]byte, minbufsize)
sseConn.Buffer(buf, maxbufsize)
sseConn.SubscribeEvent("features", func(event sse.Event) {
ds.processEvent(event)
})
sseConn.Connect()
return nil
}
func (ds *SseDataSource) onRetry(err error, delay time.Duration) {
ds.logger.Info("Reconnect", "reason", err, "delay", delay)
}
func (ds *SseDataSource) processEvent(event sse.Event) {
if event.Data == "" {
return
}
ds.logger.Info("Updating features")
err := ds.client.UpdateFromApiResponseJSON(event.Data)
if err != nil {
ds.logger.Error("Error updating features", "error", err)
}
}
func (ds *SseDataSource) loadData(ctx context.Context) error {
resp, err := ds.client.CallFeatureApi(ctx, "")
if err != nil {
return err
}
if !resp.SseSupport {
return fmt.Errorf("Sse is not supported")
}
if resp.Features == nil {
return nil
}
err = ds.client.UpdateFromApiResponse(resp)
if err != nil {
return err
}
return nil
}
func (ds *SseDataSource) setReqHeaders(req *http.Request) {
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Cache", "no-cache")
}