Skip to content

Commit fb8e551

Browse files
authored
Merge pull request #12 from DoodleScheduling/fix-wait
fix: don't await responses
2 parents 940673e + 6ca9007 commit fb8e551

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

config/samples/requestclone.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ apiVersion: proxy.infra.doodle.com/v1beta1
22
kind: RequestClone
33
metadata:
44
name: webhook-receiver
5-
namespace: apps
65
spec:
76
host: webhook-receiver.example.com
87
backend:
98
serviceName: webhook-receiver-app-1
10-
servicePort: http
9+
servicePort: http

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"os"
2424
"strings"
25+
"sync"
2526
"time"
2627

2728
"github.com/spf13/pflag"
@@ -193,10 +194,18 @@ func main() {
193194
MaxHeaderBytes: 1 << 20,
194195
}
195196

197+
var wg sync.WaitGroup
198+
wg.Add(1)
199+
196200
go func() {
201+
defer wg.Done()
197202
if err := s.ListenAndServe(); err != nil {
198203
setupLog.Error(err, "HTTP server error")
199204
}
205+
206+
s.RegisterOnShutdown(func() {
207+
proxy.Close()
208+
})
200209
}()
201210

202211
pReconciler := &controllers.RequestCloneReconciler{
@@ -218,4 +227,7 @@ func main() {
218227
setupLog.Error(err, "problem running manager")
219228
os.Exit(1)
220229
}
230+
231+
s.Shutdown(context.TODO())
232+
wg.Wait()
221233
}

proxy/http.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type HttpProxy struct {
3434
mutex sync.Mutex
3535
log logr.Logger
3636
bodySizeLimit int64
37+
wg sync.WaitGroup
3738
}
3839

3940
type Options struct {
@@ -92,7 +93,6 @@ func (h *HttpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9293
found bool
9394
b []byte
9495
err error
95-
wg sync.WaitGroup
9696
)
9797

9898
if h.bodySizeLimit == 0 {
@@ -111,10 +111,10 @@ func (h *HttpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
111111
if dst.Host == r.Host {
112112
found = true
113113
h.log.Info("found matching http backend for request", "request", r.RequestURI, "host", dst.Host, "service", dst.Service, "port", dst.Port)
114-
wg.Add(1)
114+
h.wg.Add(1)
115115

116116
go func(dst RequestClone, r *http.Request, b []byte) {
117-
defer wg.Done()
117+
defer h.wg.Done()
118118
clone := r.Clone(context.TODO())
119119

120120
clone.URL.Scheme = "http"
@@ -139,6 +139,8 @@ func (h *HttpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
139139
// We don't have any matching RequestClone resources matching the host
140140
w.WriteHeader(http.StatusServiceUnavailable)
141141
}
142+
}
142143

143-
wg.Wait()
144+
func (h *HttpProxy) Close() {
145+
h.wg.Wait()
144146
}

proxy/http_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ func TestServeHTTP(t *testing.T) {
184184

185185
w := httptest.NewRecorder()
186186
proxy.ServeHTTP(w, test.request())
187+
proxy.Close()
188+
187189
g.Expect(test.expectHTTPCode).To(Equal(w.Code))
188190
_ = w.Result()
189191

0 commit comments

Comments
 (0)