generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use separate bind for http ingress server (#1615)
Fixes #1609 @alecthomas I took a stab at this one. Lemme know if there's a better approach here. :)
- Loading branch information
1 parent
3351d23
commit f445e18
Showing
6 changed files
with
69 additions
and
11 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
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,45 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/TBD54566975/ftl/internal/log" | ||
) | ||
|
||
const ShutdownGracePeriod = 5 * time.Second | ||
|
||
func Serve(ctx context.Context, listen *url.URL, handler http.Handler) error { | ||
httpServer := &http.Server{ | ||
Addr: listen.Host, | ||
Handler: handler, | ||
ReadHeaderTimeout: 30 * time.Second, | ||
BaseContext: func(_ net.Listener) context.Context { | ||
return ctx | ||
}, | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
shutdownCtx, cancel := context.WithTimeout(context.Background(), ShutdownGracePeriod) | ||
defer cancel() | ||
err := httpServer.Shutdown(shutdownCtx) | ||
if err != nil { | ||
if errors.Is(err, context.Canceled) { | ||
_ = httpServer.Close() | ||
return | ||
} | ||
log.FromContext(ctx).Errorf(err, "server shutdown error") | ||
} | ||
}() | ||
|
||
err := httpServer.ListenAndServe() | ||
if errors.Is(err, http.ErrServerClosed) { | ||
return nil | ||
} | ||
return err | ||
} |