-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathbackend_keepalive_test.go
199 lines (163 loc) · 5.51 KB
/
backend_keepalive_test.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
package integration
import (
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"sync"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("KeepAlive (HTTP Persistent Connections) to backends", func() {
var (
testState *testState
testAppRoute string
testApp *StateTrackingTestApp
)
BeforeEach(func() {
testState = NewTestState()
testApp = NewUnstartedTestApp(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
_, err := io.ReadAll(r.Body)
Expect(err).NotTo(HaveOccurred())
w.WriteHeader(200)
}))
testAppRoute = "potato.potato"
})
AfterEach(func() {
if testState != nil {
testState.StopAndCleanup()
}
testApp.Close()
})
doRequest := func() {
assertRequestSucceeds(testState.client,
testState.newGetRequest(fmt.Sprintf("http://%s", testAppRoute)))
}
Context("when KeepAlives are disabled", func() {
BeforeEach(func() {
testState.cfg.DisableKeepAlives = true
testState.StartGorouterOrFail()
testApp.Start()
testState.register(testApp.Server, testAppRoute)
Expect(testApp.GetConnStates()).To(BeEmpty())
})
Specify("connections to backends are closed after each request", func() {
doRequest()
By("checking that the connection is closed after the first request")
connStates := testApp.GetConnStates()
Expect(connStates).To(HaveLen(3))
Expect(connStates[0].State).To(Equal("new"))
Expect(connStates[1].State).To(Equal("active"))
Expect(connStates[2].State).To(Equal("closed"))
By("doing a second request")
doRequest()
By("checking that the connection is not re-used")
connStates = testApp.GetConnStates()
Expect(connStates[0].State).To(Equal("new"))
Expect(connStates[1].State).To(Equal("active"))
Expect(connStates[2].State).To(Equal("closed"))
Expect(connStates[3].State).To(Equal("new"))
Expect(connStates[4].State).To(Equal("active"))
By("checking that different connections are used for each request")
Expect(connStates[0].ConnPtr).NotTo(Equal(connStates[3].ConnPtr))
})
})
Context("when KeepAlives are enabled", func() {
BeforeEach(func() {
testState.cfg.DisableKeepAlives = false
testState.StartGorouterOrFail()
})
Context("when connecting to a non-TLS backend", func() {
BeforeEach(func() {
testApp.Start()
testState.register(testApp.Server, testAppRoute)
})
Specify("connections to backends are persisted after requests finish", func() {
doRequest()
assertConnectionIsReused(testApp.GetConnStates(), "new", "active", "idle")
doRequest()
assertConnectionIsReused(testApp.GetConnStates(), "new", "active", "idle", "active", "idle")
By("re-registering the route")
testState.register(testApp.Server, testAppRoute)
By("doing a third request")
doRequest()
By("checking that the same connection is *still* being re-used on the backend")
assertConnectionIsReused(testApp.GetConnStates()[:6],
"new", "active", "idle", "active", "idle", "active")
})
})
Context("when connecting to a TLS-enabled backend", func() {
BeforeEach(func() {
testApp.TLS = testState.trustedBackendTLSConfig
testApp.StartTLS()
testState.registerAsTLS(testApp.Server, testAppRoute, testState.trustedBackendServerCertSAN)
})
Specify("connections to backends are persisted after requests finish", func() {
By("doing a couple requests")
doRequest()
doRequest()
By("checking that only one backend connection is used for both requests")
assertConnectionIsReused(testApp.GetConnStates()[:4], "new", "active", "idle", "active")
By("re-registering the route")
testState.registerAsTLS(testApp.Server, testAppRoute, testState.trustedBackendServerCertSAN)
By("doing a third request")
doRequest()
By("checking that the same connection is *still* being re-used on the backend")
// We don't need to assert on the last "idle" since we know the connection is being reused by not seeing a "new"
assertConnectionIsReused(testApp.GetConnStates()[:6],
"new", "active", "idle", "active", "idle", "active")
})
})
})
})
type ConnState struct {
ConnPtr string
RemoteAddr string
State string
}
type StateTrackingTestApp struct {
*httptest.Server
backendConnStates []ConnState
m sync.Mutex
}
func (s *StateTrackingTestApp) GetConnStates() []ConnState {
s.m.Lock()
defer s.m.Unlock()
ret := make([]ConnState, len(s.backendConnStates))
copy(ret, s.backendConnStates) // copy(dst, src)
return ret
}
func assertConnectionIsReused(actualStates []ConnState, expectedStates ...string) {
// get initial connection
p := actualStates[0].ConnPtr
a := actualStates[0].RemoteAddr
// check length
Expect(actualStates).To(HaveLen(len(expectedStates)))
// construct slice of expected connection state values
expectedConnStates := make([]ConnState, len(expectedStates))
for i := 0; i < len(expectedStates); i++ {
expectedConnStates[i] = ConnState{ConnPtr: p, RemoteAddr: a, State: expectedStates[i]}
}
// assert
Expect(actualStates).To(Equal(expectedConnStates))
}
func NewUnstartedTestApp(handler http.Handler) *StateTrackingTestApp {
a := &StateTrackingTestApp{
Server: httptest.NewUnstartedServer(handler),
}
a.Server.Config.ConnState = func(conn net.Conn, state http.ConnState) {
a.m.Lock()
defer a.m.Unlock()
a.backendConnStates = append(a.backendConnStates,
ConnState{
ConnPtr: fmt.Sprintf("%p", conn),
RemoteAddr: conn.RemoteAddr().String(),
State: state.String(),
})
}
a.Server.Config.IdleTimeout = 5 * time.Second
return a
}