-
Notifications
You must be signed in to change notification settings - Fork 1
/
response_factory.go
216 lines (195 loc) · 4.65 KB
/
response_factory.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
package asyncp
import (
"context"
"fmt"
"regexp"
"sync"
)
// ResponseWriterFactory interface to generate new response object
type ResponseWriterFactory interface {
// Borrow response writer by event and task object
Borrow(ctx context.Context, promise Promise, event Event) ResponseWriter
// Release response writer object
Release(w ResponseWriter)
}
type streamResponseFactory struct {
pool sync.Pool
publisher Publisher
mux *TaskMux
}
// NewStreamResponseFactory returns
func NewStreamResponseFactory(publisher Publisher) ResponseWriterFactory {
return &streamResponseFactory{
publisher: publisher,
pool: sync.Pool{
New: func() any { return &responseStreamWriter{} },
},
}
}
func (s *streamResponseFactory) SetMux(mux *TaskMux) {
s.mux = mux
}
func (s *streamResponseFactory) Borrow(ctx context.Context, promise Promise, event Event) ResponseWriter {
wr := s.pool.Get().(*responseStreamWriter)
wr.ctx = ctx
wr.event = event
wr.promise = promise
wr.wstream = s.publisher
wr.mux = s.mux
wr.pool = s
return wr
}
func (s *streamResponseFactory) Release(w ResponseWriter) {
if w == nil {
return
}
if wr := w.(*responseStreamWriter); wr.event != nil {
wr.ctx = nil
wr.event = nil
wr.wstream = nil
wr.pool = nil
wr.mux = nil
s.pool.Put(wr)
}
}
type proxyResponseFactory struct {
mux *TaskMux
pool sync.Pool
}
// NewProxyResponseFactory returns
func NewProxyResponseFactory() ResponseWriterFactory {
return &proxyResponseFactory{
pool: sync.Pool{
New: func() any { return &responseProxyWriter{} },
},
}
}
func (s *proxyResponseFactory) SetMux(mux *TaskMux) {
s.mux = mux
}
func (s *proxyResponseFactory) Borrow(ctx context.Context, promise Promise, event Event) ResponseWriter {
wr := s.pool.Get().(*responseProxyWriter)
wr.event = event
wr.promise = promise
wr.pool = s
wr.mux = s.mux
return wr
}
func (s *proxyResponseFactory) Release(w ResponseWriter) {
if w == nil {
return
}
if wr := w.(*responseProxyWriter); wr.event != nil {
wr.promise = nil
wr.event = nil
wr.pool = nil
wr.mux = nil
s.pool.Put(wr)
}
}
type multistreamItem struct {
tfn []func(string) bool
patterns []*regexp.Regexp
publisher Publisher
}
func (it *multistreamItem) test(eventName string) bool {
if len(it.patterns) == 0 && len(it.tfn) == 0 {
return true
}
for _, f := range it.tfn {
if f(eventName) {
return true
}
}
for _, ptr := range it.patterns {
if ptr.MatchString(eventName) {
return true
}
}
return false
}
type mutistreamResponseFactory struct {
pool sync.Pool
publishers []*multistreamItem
mux *TaskMux
}
// NewMultistreamResponseFactory returns implementation with multipublisher support
func NewMultistreamResponseFactory(streams ...any) ResponseWriterFactory {
var (
publishers []*multistreamItem
commonItem *multistreamItem
item = new(multistreamItem)
)
for _, v := range streams {
switch val := v.(type) {
case []func(string) bool:
item.tfn = append(item.tfn, val...)
case func(string) bool:
item.tfn = append(item.tfn, val)
case []string:
for _, ptr := range val {
item.patterns = append(item.patterns, regexp.MustCompile(ptr))
}
case string:
item.patterns = append(item.patterns, regexp.MustCompile(val))
case *regexp.Regexp:
item.patterns = append(item.patterns, val)
case []*regexp.Regexp:
item.patterns = append(item.patterns, val...)
case Publisher:
item.publisher = val
if len(item.patterns) == 0 {
if commonItem != nil {
panic("only one default publisher supports")
}
commonItem = item
} else {
publishers = append(publishers, item)
}
item = new(multistreamItem)
default:
panic(fmt.Sprintf("invalid parameter %T", v))
}
}
if commonItem != nil {
publishers = append(publishers, commonItem)
}
return &mutistreamResponseFactory{
publishers: publishers,
pool: sync.Pool{
New: func() any { return &responseStreamWriter{} },
},
}
}
func (s *mutistreamResponseFactory) SetMux(mux *TaskMux) {
s.mux = mux
}
func (s *mutistreamResponseFactory) Borrow(ctx context.Context, promise Promise, event Event) ResponseWriter {
wr := s.pool.Get().(*responseStreamWriter)
wr.event = event
wr.promise = promise
wr.wstream = s.publisher(event)
wr.mux = s.mux
wr.pool = s
return wr
}
func (s *mutistreamResponseFactory) Release(w ResponseWriter) {
if w == nil {
return
}
if wr := w.(*responseStreamWriter); wr.event != nil {
wr.event = nil
wr.wstream = nil
wr.pool = nil
wr.mux = nil
s.pool.Put(wr)
}
}
func (s *mutistreamResponseFactory) publisher(event Event) Publisher {
for _, item := range s.publishers {
if item.test(event.Name()) {
return item.publisher
}
}
return nil
}