Files

2.9 KiB

ready

import "github.com/effective-security/porto/restserver/ready"

Package ready provides an http.Handler wrapper that gates requests on a readiness check. While ServiceStatus.IsReady reports false every request is answered with a JSON 503 "not_ready" error instead of reaching the delegate handler.

h := ready.NewServiceStatusVerifier(server, next)

restserver uses it so that a server whose services are still initialising (or that has stopped serving) reports unavailable to load balancers.

Index

func NewServiceStatusVerifier

func NewServiceStatusVerifier(s ServiceStatus, delegate http.Handler) http.Handler

NewServiceStatusVerifier returns a ServiceReadyVerifier that answers requests with a JSON 503 not_ready error (via marshal.WriteJSON) carrying the request's correlation ID while s is not ready, and chains to delegate otherwise. The error is built per request; nothing is shared.

type ServiceReadyVerifier

ServiceReadyVerifier is a http.Handler that checks if the service is ready to serve, and if so chains to Delegate, otherwise calls NotReadyHandler. All fields must be set; NewServiceStatusVerifier provides the JSON 503 default for NotReadyHandler.

type ServiceReadyVerifier struct {
    // Status is the readiness probe.
    Status ServiceStatus
    // Delegate handles requests while Status is ready.
    Delegate http.Handler
    // NotReadyHandler handles requests while Status is not ready.
    NotReadyHandler http.Handler
}

func (*ServiceReadyVerifier) ServeHTTP

func (c *ServiceReadyVerifier) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface

type ServiceStatus

ServiceStatus is the readiness probe consulted on every request. restserver.Server satisfies it.

type ServiceStatus interface {
    // IsReady reports whether requests may be served right now. It is
    // called concurrently and must be cheap and safe for concurrent use.
    IsReady() bool
}

Generated by gomarkdoc