This repository was archived by the owner on Apr 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhttpcoala.go
More file actions
213 lines (174 loc) · 4.34 KB
/
httpcoala.go
File metadata and controls
213 lines (174 loc) · 4.34 KB
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
package httpcoala
import (
"bytes"
"net/http"
"strings"
"sync"
"sync/atomic"
)
// Route middleware handler will coalesce multiple requests for the same URI
// (and routed methods) to be processed as a single request.
func Route(methods ...string) func(next http.Handler) http.Handler {
coalescer := newCoalescer(methods...)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bw, sw, ok := coalescer.Route(w, r)
if !ok {
next.ServeHTTP(w, r)
return
}
if sw != nil {
<-sw.Flushed()
return
}
defer coalescer.Flush(bw, r)
next.ServeHTTP(bw, r)
})
}
}
func newCoalescer(methods ...string) *coalescer {
methodMap := make(map[string]struct{})
for _, m := range methods {
if m == "*" {
break
}
methodMap[strings.ToUpper(m)] = struct{}{}
}
return &coalescer{
methodMap: methodMap,
requests: make(map[request]*batchWriter),
}
}
type coalescer struct {
mu sync.Mutex
methodMap map[string]struct{}
requests map[request]*batchWriter
}
func (c *coalescer) Route(w http.ResponseWriter, r *http.Request) (*batchWriter, *standbyWriter, bool) {
if _, ok := c.methodMap[r.Method]; !ok && len(c.methodMap) > 0 {
return nil, nil, false
}
var reqKey = request{Method: r.Method, Host: r.Host, URI: r.URL.RequestURI()}
var bw *batchWriter
var sw *standbyWriter
var found bool
c.mu.Lock()
defer c.mu.Unlock()
bw, found = c.requests[reqKey]
if found {
sw, found = bw.AddWriter(w)
}
if !found {
bw = newBatchWriter(w)
c.requests[reqKey] = bw
}
return bw, sw, true
}
func (c *coalescer) Flush(bw *batchWriter, r *http.Request) {
c.mu.Lock()
reqKey := request{Method: r.Method, Host: r.Host, URI: r.URL.RequestURI()}
delete(c.requests, reqKey)
c.mu.Unlock()
bw.Flush()
<-bw.Flushed()
}
type request struct {
Method string
Host string
URI string
}
type batchWriter struct {
writers []*standbyWriter
header http.Header
bufw *bytes.Buffer
wroteHeader uint32
flushed uint32
mu sync.Mutex
}
func newBatchWriter(w http.ResponseWriter) *batchWriter {
return &batchWriter{
writers: []*standbyWriter{newStandbyWriter(w)},
header: http.Header{},
bufw: &bytes.Buffer{},
}
}
func (bw *batchWriter) AddWriter(w http.ResponseWriter) (*standbyWriter, bool) {
// Synchronize writers and wroteHeader
bw.mu.Lock()
defer bw.mu.Unlock()
if atomic.LoadUint32(&bw.wroteHeader) > 0 {
return nil, false
}
sw := newStandbyWriter(w)
bw.writers = append(bw.writers, sw)
return sw, true
}
func (bw *batchWriter) Header() http.Header {
return bw.header
}
func (bw *batchWriter) Write(p []byte) (int, error) {
if atomic.CompareAndSwapUint32(&bw.wroteHeader, 0, 1) {
bw.writeHeader(http.StatusOK)
}
return bw.bufw.Write(p)
}
func (bw *batchWriter) WriteHeader(status int) {
if !atomic.CompareAndSwapUint32(&bw.wroteHeader, 0, 1) {
return
}
bw.writeHeader(status)
}
func (bw *batchWriter) writeHeader(status int) {
// Synchronize writers and wroteHeader
bw.mu.Lock()
defer bw.mu.Unlock()
// Broadcast WriteHeader to standby writers
for _, sw := range bw.writers {
go func(sw *standbyWriter, status int, header http.Header) {
h := map[string][]string(sw.Header())
for k, v := range header {
h[k] = v
}
// h["X-Coalesce"] = []string{"hit"}
sw.WriteHeader(status)
close(sw.wroteHeaderCh)
}(sw, status, bw.header)
}
}
func (bw *batchWriter) Flush() {
if !atomic.CompareAndSwapUint32(&bw.flushed, 0, 1) {
return
}
if atomic.CompareAndSwapUint32(&bw.wroteHeader, 0, 1) {
bw.writeHeader(http.StatusOK)
}
data := bw.bufw.Bytes()
for _, sw := range bw.writers {
go func(sw *standbyWriter, data []byte) {
// Block until the header has been written
<-sw.wroteHeaderCh
// Write the data to the writer and signal the
// flush to be finished by closing the channel.
sw.Write(data)
close(sw.flushedCh)
}(sw, data)
}
}
func (bw *batchWriter) Flushed() <-chan struct{} {
return bw.writers[0].flushedCh
}
type standbyWriter struct {
http.ResponseWriter
wroteHeaderCh chan struct{}
flushedCh chan struct{}
}
func newStandbyWriter(w http.ResponseWriter) *standbyWriter {
return &standbyWriter{
ResponseWriter: w,
wroteHeaderCh: make(chan struct{}, 0),
flushedCh: make(chan struct{}, 0),
}
}
func (sw *standbyWriter) Flushed() <-chan struct{} {
return sw.flushedCh
}