Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions network/handlers/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ var _ http.Handler = (*Drainer)(nil)

// ServeHTTP implements http.Handler
func (d *Drainer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
draining := d.draining()
// Respond to probes regardless of path.
if d.isHealthCheckRequest(r) {
if d.draining() {
if draining {
http.Error(w, "shutting down", http.StatusServiceUnavailable)
} else if d.HealthCheck != nil {
d.HealthCheck(w, r)
Expand All @@ -101,15 +102,22 @@ func (d *Drainer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if isKProbe(r) {
if d.draining() {
if draining {
http.Error(w, "shutting down", http.StatusServiceUnavailable)
} else {
serveKProbe(w, r)
}
return
}

d.resetTimer()
if draining {
d.resetTimer()
if r.ProtoMajor == 1 {
// Setting the connection close header will hint the server to
// close the connection after handling the request
w.Header().Set("Connection", "close")
}
}
d.Inner.ServeHTTP(w, r)
}

Expand Down Expand Up @@ -186,14 +194,6 @@ func (d *Drainer) Reset() {
}

func (d *Drainer) resetTimer() {
if func() bool {
d.RLock()
defer d.RUnlock()
return d.timer == nil
}() {
return
}

d.Lock()
defer d.Unlock()
if d.timer != nil && d.timer.Stop() {
Expand Down
40 changes: 37 additions & 3 deletions network/handlers/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (mt *mockTimer) tickChan() <-chan time.Time {

func TestDrainMechanics(t *testing.T) {
var (
w http.ResponseWriter
w = httptest.NewRecorder()
req = &http.Request{}
probe = &http.Request{
Header: http.Header{
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestDrainMechanics(t *testing.T) {

func TestDrainerKProbe(t *testing.T) {
var (
w http.ResponseWriter
w = httptest.NewRecorder()
req = &http.Request{}
kprobehash = "hash"
kprobe = &http.Request{
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestHealthCheckWithProbeType(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var (
w http.ResponseWriter
w = httptest.NewRecorder()
req = &http.Request{}
cnt = 0
inner = http.HandlerFunc(func(http.ResponseWriter, *http.Request) { cnt++ })
Expand Down Expand Up @@ -586,3 +586,37 @@ func TestResetWithActiveRequests(t *testing.T) {
// We need requests to be active for a bit
time.Sleep(time.Second)
}

func BenchmarkNoDrain(b *testing.B) {
r, _ := http.NewRequest(http.MethodGet, "knative.dev", nil)
w := httptest.NewRecorder()
handler := func(http.ResponseWriter, *http.Request) {
}

d := Drainer{
QuietPeriod: 10 * time.Second,
Inner: http.HandlerFunc(handler),
}

for b.Loop() {
d.ServeHTTP(w, r)
}
}

func BenchmarkDrain(b *testing.B) {
r, _ := http.NewRequest(http.MethodGet, "knative.dev", nil)
w := httptest.NewRecorder()
handler := func(http.ResponseWriter, *http.Request) {
}

d := Drainer{
QuietPeriod: 10 * time.Second,
Inner: http.HandlerFunc(handler),
}

go d.Drain()

for b.Loop() {
d.ServeHTTP(w, r)
}
}
Loading