forked from taiyoh/graphqlws-subscription-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe_filter.go
218 lines (199 loc) · 5.25 KB
/
subscribe_filter.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package gss
import (
"sort"
"strconv"
"strings"
"sync"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
)
type SubscriptionIDByConnectionID map[string]string
type QueryArgsMap map[string]string
type SubscribeFilter interface {
RegisterConnectionIDFromDocument(connID string, subID string, doc *ast.Document, variables map[string]interface{})
RemoveSubscriptionIDFromConnectionID(connID, subID string)
RemoveConnectionIDFromChannels(connID string)
GetChannelRegisteredConnectionIDs(channel string) SubscriptionIDByConnectionID
}
type ChannelSerializer interface {
Serialize(field string, args QueryArgsMap) string
}
type channelSerializerFunc func(field string, args QueryArgsMap) string
func (f channelSerializerFunc) Serialize(field string, args QueryArgsMap) string {
return f(field, args)
}
func getNewChannelSerializerFunc() channelSerializerFunc {
return func(field string, args QueryArgsMap) string {
sargs := []string{}
for k := range args {
sargs = append(sargs, k)
}
sort.Slice(sargs, func(i, j int) bool {
return sargs[i] <= sargs[j]
})
strList := []string{field}
for i := range sargs {
strList = append(strList, args[sargs[i]])
}
return strings.Join(strList, ":")
}
}
type subscribeFilter struct {
SubscribeFilter
Serializer ChannelSerializer
ConnectionIDByChannel map[string]*sync.Map
ChannelByConnectionID map[string]*sync.Map
}
func NewSubscribeFilter() *subscribeFilter {
return &subscribeFilter{
Serializer: getNewChannelSerializerFunc(),
ConnectionIDByChannel: map[string]*sync.Map{},
ChannelByConnectionID: map[string]*sync.Map{},
}
}
func operationDefinitionsWithOperation(
doc *ast.Document,
op string,
) []*ast.OperationDefinition {
defs := []*ast.OperationDefinition{}
for _, node := range doc.Definitions {
if node.GetKind() == "OperationDefinition" {
if def, ok := node.(*ast.OperationDefinition); ok {
if def.Operation == op {
defs = append(defs, def)
}
}
}
}
return defs
}
func selectionSetsForOperationDefinitions(
defs []*ast.OperationDefinition,
) []*ast.SelectionSet {
sets := []*ast.SelectionSet{}
for _, def := range defs {
if set := def.GetSelectionSet(); set != nil {
sets = append(sets, set)
}
}
return sets
}
func ifToStr(d interface{}) string {
if v, ok := d.(string); ok {
return v
}
if v, ok := d.(int); ok {
return strconv.Itoa(v)
}
return ""
}
func getArgKeyValueFromAstValue(variables map[string]interface{}, arg *ast.Argument) (string, string, bool) {
var k, v string
val := arg.Value
if val.GetKind() == kinds.Variable {
n := val.GetValue().(*ast.Name)
k = n.Value
vv, ok := variables[n.Value]
if !ok {
return "", "", false
}
v = ifToStr(vv)
} else {
k = arg.Name.Value
vv := arg.Value.GetValue().(string)
v = ifToStr(vv)
}
if v != "" {
return k, v, true
}
return "", "", false
}
func channelsForSelectionSets(variables map[string]interface{}, sets []*ast.SelectionSet) map[string]map[string]string {
nameList := map[string]map[string]string{}
for _, set := range sets {
if len(set.Selections) < 1 {
continue
}
field := set.Selections[0].(*ast.Field)
args := map[string]string{}
for _, arg := range field.Arguments {
if k, v, ok := getArgKeyValueFromAstValue(variables, arg); ok {
args[k] = v
}
}
if len(args) > 0 {
nameList[field.Name.Value] = args
}
}
return nameList
}
func (f *subscribeFilter) RegisterConnectionIDFromDocument(connID string, subID string, doc *ast.Document, variables map[string]interface{}) {
defs := operationDefinitionsWithOperation(doc, "subscription")
sets := selectionSetsForOperationDefinitions(defs)
for field, args := range channelsForSelectionSets(variables, sets) {
ch := f.Serializer.Serialize(field, args)
if m, ok := f.ConnectionIDByChannel[ch]; ok {
m.Store(connID, subID)
} else {
m := &sync.Map{}
m.Store(connID, subID)
f.ConnectionIDByChannel[ch] = m
}
if m, ok := f.ChannelByConnectionID[connID]; ok {
m.Store(ch, subID)
} else {
m := &sync.Map{}
m.Store(ch, subID)
f.ChannelByConnectionID[connID] = m
}
}
}
func (f *subscribeFilter) RemoveConnectionIDFromChannels(connID string) {
channels := []string{}
if m1, ok := f.ChannelByConnectionID[connID]; ok {
m1.Range(func(k, v interface{}) bool {
channels = append(channels, k.(string))
return true
})
delete(f.ChannelByConnectionID, connID)
}
for _, ch := range channels {
if m, ok := f.ConnectionIDByChannel[ch]; ok {
m.Delete(connID)
}
}
}
func (f *subscribeFilter) RemoveSubscriptionIDFromConnectionID(connID, subID string) {
var ch string
m1, ok := f.ChannelByConnectionID[connID]
if !ok {
return
}
m1.Range(func(k, v interface{}) bool {
if v.(string) == subID {
ch = k.(string)
}
return ch == ""
})
if ch == "" {
return
}
m1.Delete(ch)
m2, ok := f.ConnectionIDByChannel[ch]
if !ok {
return
}
if s, ok := m2.Load(connID); ok && s.(string) == subID {
m2.Delete(connID)
}
}
func (f *subscribeFilter) GetChannelRegisteredConnectionIDs(channel string) SubscriptionIDByConnectionID {
founds := SubscriptionIDByConnectionID{}
if m, ok := f.ConnectionIDByChannel[channel]; ok {
m.Range(func(k, v interface{}) bool {
founds[k.(string)] = v.(string)
return true
})
}
return founds
}