Skip to content

Commit

Permalink
api: add explicit readiness check
Browse files Browse the repository at this point in the history
Now the readiness is governed by a function func() bool.
  • Loading branch information
mmatczuk committed Mar 7, 2023
1 parent c2c65f3 commit 12ccf9a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
12 changes: 4 additions & 8 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,20 @@ import (
"github.com/saucelabs/forwarder/internal/version"
)

type server interface {
Addr() string
}

// APIHandler serves API endpoints.
// It provides health and readiness endpoints prometheus metrics, and pprof debug endpoints.
type APIHandler struct {
mux *http.ServeMux
server server
ready func() bool
config string
script string
}

func NewAPIHandler(r prometheus.Gatherer, s server, config, pac string) *APIHandler {
func NewAPIHandler(r prometheus.Gatherer, ready func() bool, config, pac string) *APIHandler {
m := http.NewServeMux()
a := &APIHandler{
mux: m,
server: s,
ready: ready,
config: config,
script: pac,
}
Expand All @@ -60,7 +56,7 @@ func (h *APIHandler) healthz(w http.ResponseWriter, r *http.Request) {
}

func (h *APIHandler) readyz(w http.ResponseWriter, r *http.Request) {
if h.server.Addr() != "" {
if h.ready() {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("OK"))
Expand Down
2 changes: 1 addition & 1 deletion cmd/forwarder/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *command) RunE(cmd *cobra.Command, args []string) error {
}

r := prometheus.NewRegistry()
a, err := forwarder.NewHTTPServer(c.apiServerConfig, forwarder.NewAPIHandler(r, s, config, ""), logger.Named("api"))
a, err := forwarder.NewHTTPServer(c.apiServerConfig, forwarder.NewAPIHandler(r, s.Ready, config, ""), logger.Named("api"))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/forwarder/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *command) RunE(cmd *cobra.Command, args []string) error {
f = append(f, p.Run)

if c.apiServerConfig.Addr != "" {
h := forwarder.NewAPIHandler(c.promReg, p, config, script)
h := forwarder.NewAPIHandler(c.promReg, p.Ready, config, script)
a, err := forwarder.NewHTTPServer(c.apiServerConfig, h, logger.Named("api"))
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ func (hp *HTTPProxy) Addr() string {
return *addr
}

// Ready returns true if the server is running and ready to accept requests.
func (hp *HTTPProxy) Ready() bool {
return hp.Addr() != ""
}

// headerRemover removes headers that match given prefix.
type headerRemover struct {
prefix string
Expand Down
5 changes: 5 additions & 0 deletions http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,8 @@ func (hs *HTTPServer) Addr() string {
}
return *addr
}

// Ready returns true if the server is running and ready to accept requests.
func (hs *HTTPServer) Ready() bool {
return hs.Addr() != ""
}

0 comments on commit 12ccf9a

Please sign in to comment.