/
stream.go
186 lines (168 loc) · 4.42 KB
/
stream.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package stream
import (
"github.com/configcat/configcat-proxy/diag/metrics"
"github.com/configcat/configcat-proxy/log"
"github.com/configcat/configcat-proxy/model"
"github.com/configcat/configcat-proxy/sdk"
"hash/maphash"
)
type Stream interface {
CanEval(key string) bool
IsInValidState() bool
CreateConnection(key string, user model.UserAttrs) *Connection
CloseConnection(conn *Connection, key string)
Close()
}
type connEstablished struct {
conn *Connection
user model.UserAttrs
key string
}
type connClosed struct {
conn *Connection
key string
}
type stream struct {
sdkClient sdk.Client
sdkConfigChanged <-chan struct{}
stop chan struct{}
log log.Logger
serverType string
sdkId string
metrics metrics.Reporter
channels map[string]map[uint64]channel
connEstablished chan *connEstablished
connClosed chan *connClosed
connCount int64
seed maphash.Seed
}
func NewStream(sdkId string, sdkClient sdk.Client, metrics metrics.Reporter, log log.Logger, serverType string) Stream {
s := &stream{
channels: make(map[string]map[uint64]channel),
connEstablished: make(chan *connEstablished),
connClosed: make(chan *connClosed),
stop: make(chan struct{}),
sdkConfigChanged: sdkClient.SubConfigChanged(serverType + sdkId),
sdkClient: sdkClient,
log: log.WithPrefix("stream-" + sdkId),
serverType: serverType,
sdkId: sdkId,
metrics: metrics,
seed: maphash.MakeSeed(),
}
go s.run()
return s
}
func (s *stream) run() {
for {
select {
case established := <-s.connEstablished:
s.addConnection(established)
s.connCount++
s.log.Debugf("#%s: connection established, all connections: %d", established.key, s.connCount)
if s.metrics != nil {
s.metrics.IncrementConnection(s.sdkId, s.serverType, established.key)
}
case closed := <-s.connClosed:
s.removeConnection(closed)
s.connCount--
s.log.Debugf("#%s: connection closed, all connections: %d", closed.key, s.connCount)
if s.metrics != nil {
s.metrics.DecrementConnection(s.sdkId, s.serverType, closed.key)
}
case <-s.sdkConfigChanged:
s.notifyConnections()
case <-s.stop:
return
}
}
}
func (s *stream) CanEval(key string) bool {
keys := s.sdkClient.Keys()
for _, k := range keys {
if k == key {
return true
}
}
return false
}
func (s *stream) IsInValidState() bool {
return s.sdkClient.IsInValidState()
}
func (s *stream) CreateConnection(key string, user model.UserAttrs) *Connection {
var discriminator uint64
if user != nil {
discriminator = user.Discriminator(s.seed)
}
conn := newConnection(discriminator)
select {
case <-s.stop:
return conn
default:
s.connEstablished <- &connEstablished{conn: conn, user: user, key: key}
return conn
}
}
func (s *stream) CloseConnection(conn *Connection, key string) {
select {
case <-s.stop:
return
default:
s.connClosed <- &connClosed{conn: conn, key: key}
}
}
func (s *stream) Close() {
close(s.stop)
s.sdkClient.UnsubConfigChanged(s.serverType + s.sdkId)
s.log.Reportf("shutdown complete")
}
func (s *stream) addConnection(established *connEstablished) {
bucket, ok := s.channels[established.key]
if !ok {
ch := createChannel(established, s.sdkClient)
bucket = map[uint64]channel{established.conn.discriminator: ch}
s.channels[established.key] = bucket
}
ch, ok := bucket[established.conn.discriminator]
if !ok {
ch = createChannel(established, s.sdkClient)
bucket[established.conn.discriminator] = ch
}
ch.AddConnection(established.conn)
established.conn.receive <- ch.LastPayload()
if s.metrics != nil {
s.metrics.AddSentMessageCount(1, s.sdkId, s.serverType, established.key)
}
}
func (s *stream) removeConnection(closed *connClosed) {
bucket, ok := s.channels[closed.key]
if !ok {
return
}
ch, ok := bucket[closed.conn.discriminator]
if !ok {
return
}
ch.RemoveConnection(closed.conn)
if ch.IsEmpty() {
delete(bucket, closed.conn.discriminator)
}
if len(bucket) == 0 {
delete(s.channels, closed.key)
}
}
func (s *stream) notifyConnections() {
sent := 0
for key, bucket := range s.channels {
for _, ch := range bucket {
count := ch.Notify(s.sdkClient, key)
sent += count
if s.metrics != nil {
s.metrics.AddSentMessageCount(count, s.sdkId, s.serverType, key)
}
}
}
if sent > 0 {
s.log.Debugf("payload sent to %d connection(s)", sent)
}
}