-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c42621c
commit 2e41dba
Showing
4 changed files
with
170 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Package httputil contains additional functionality for working with http. | ||
package httputil | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pomerium/pomerium/pkg/contextutil" | ||
) | ||
|
||
// ServeWithGracefulStop serves the HTTP listener until ctx.Done(), and then gracefully stops and waits for gracefulTimeout | ||
// before definitively stopping. | ||
func ServeWithGracefulStop(ctx context.Context, handler http.Handler, li net.Listener, gracefulTimeout time.Duration) error { | ||
// create a context that will be used for the http requests | ||
// it will only be cancelled when baseCancel is called but will | ||
// preserve the values from ctx | ||
baseCtx, baseCancel := context.WithCancelCause(contextutil.OnlyValues(ctx)) | ||
|
||
srv := http.Server{ | ||
Handler: handler, | ||
BaseContext: func(l net.Listener) context.Context { | ||
return baseCtx | ||
}, | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
|
||
// create a context that will cancel after the graceful timeout | ||
timeoutCtx, clearTimeout := context.WithTimeout(context.Background(), gracefulTimeout) | ||
defer clearTimeout() | ||
|
||
// shut the http server down | ||
_ = srv.Shutdown(timeoutCtx) | ||
|
||
// cancel the base context used for http requests | ||
baseCancel(ctx.Err()) | ||
}() | ||
|
||
err := srv.Serve(li) | ||
if errors.Is(err, http.ErrServerClosed) { | ||
err = nil | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package httputil_test | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/pomerium/pomerium/pkg/httputil" | ||
) | ||
|
||
func TestServeWithGracefulStop(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("immediate", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
li, err := net.Listen("tcp4", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
|
||
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
}) | ||
|
||
now := time.Now() | ||
err = httputil.ServeWithGracefulStop(ctx, h, li, time.Millisecond*100) | ||
elapsed := time.Since(now) | ||
assert.Nil(t, err) | ||
assert.Less(t, elapsed, time.Millisecond*100, "should complete immediately") | ||
}) | ||
t.Run("graceful", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
li, err := net.Listen("tcp4", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
|
||
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case "/": | ||
w.WriteHeader(http.StatusNoContent) | ||
case "/wait": | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("\n")) | ||
w.(http.Flusher).Flush() | ||
select { | ||
case <-r.Context().Done(): | ||
case <-make(chan struct{}): | ||
} | ||
default: | ||
http.NotFound(w, r) | ||
} | ||
}) | ||
|
||
now := time.Now() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
eg, ctx := errgroup.WithContext(ctx) | ||
eg.Go(func() error { | ||
return httputil.ServeWithGracefulStop(ctx, h, li, time.Millisecond*100) | ||
}) | ||
eg.Go(func() error { | ||
// poll until the server is ready | ||
for { | ||
res, err := http.Get("http://" + li.Addr().String() + "/") | ||
if err != nil { | ||
continue | ||
} | ||
res.Body.Close() | ||
|
||
break | ||
} | ||
|
||
// issue a stream request that will last indefinitely | ||
res, err := http.Get("http://" + li.Addr().String() + "/wait") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cancel() | ||
|
||
// wait until the request completes (should stop after the graceful timeout) | ||
io.ReadAll(res.Body) | ||
res.Body.Close() | ||
|
||
return nil | ||
}) | ||
eg.Wait() | ||
elapsed := time.Since(now) | ||
assert.Greater(t, elapsed, time.Millisecond*100, "should complete after 100ms") | ||
}) | ||
} |