service

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrServiceNotRunning = errors.New("service not running")
	ErrServiceShutdown   = errors.New("service shutdown")
)
View Source
var (
	DefaultHTTPHealthzName = "healthz"
	DefaultHTTPHealthzAddr = ":9400"
	DefaultHTTPHealthzPath = "/healthz"
)
View Source
var (
	ErrUnhandledHealthzProbe = errors.New("unhandled healthz probe")
	ErrProbeFailed           = errors.New("probe failed")
	ErrLivenessProbeFailed   = errors.New("liveness probe failed")
	ErrReadinessProbeFailed  = errors.New("readiness probe failed")
	ErrStartupProbeFailed    = errors.New("startup probe failed")
)
View Source
var (
	DefaultHTTPPProfName = "pprof"
	DefaultHTTPPProfAddr = "localhost:6060"
	DefaultHTTPPProfPath = "/debug/pprof"
)
View Source
var (
	DefaultHTTPPrometheusName = "prometheus"
	DefaultHTTPPrometheusAddr = ":9200"
	DefaultHTTPPrometheusPath = "/metrics"
)
View Source
var (
	DefaultHTTPReadmeName = "readme"
	DefaultHTTPReadmeAddr = "localhost:9001"
	DefaultHTTPReadmePath = "/readme"
)
View Source
var (
	DefaultHTTPViperName = "viper"
	DefaultHTTPViperAddr = "localhost:9300"
	DefaultHTTPViperPath = "/config"
)
View Source
var (
	DefaultHTTPZapName = "zap"
	DefaultHTTPZapAddr = "localhost:9100"
	DefaultHTTPZapPath = "/log"
)

Functions

This section is empty.

Types

type GoRoutine

type GoRoutine struct {
	// contains filtered or unexported fields
}

GoRoutine struct

func NewGoRoutine

func NewGoRoutine(l *zap.Logger, name string, handler GoRoutineFn, opts ...GoRoutineOption) *GoRoutine
Example
var once sync.Once

svr := keel.NewServer(
	keel.WithLogger(zap.NewExample()),
	keel.WithGracefulPeriod(3*time.Second),
)

svr.AddService(
	service.NewGoRoutine(svr.Logger(), "demo", func(ctx context.Context, l *zap.Logger) error {
		for {
			// handle graceful shutdowns
			if err := ctx.Err(); errors.Is(context.Cause(ctx), service.ErrServiceShutdown) {
				l.Info("context has been canceled du to graceful shutdow")
				return nil
			} else if err != nil {
				return errors.Wrap(err, "unexpected context error")
			}

			l.Info("ping")
			time.Sleep(700 * time.Millisecond)
			once.Do(shutdown)
		}
	}),
)

svr.Run()
Output:

{"level":"info","msg":"starting keel server"}
{"level":"info","msg":"starting keel service","keel_service_type":"goroutine","keel_service_name":"demo"}
{"level":"info","msg":"ping","keel_service_type":"goroutine","keel_service_name":"demo","keel_service_inst":0}
{"level":"info","msg":"ping","keel_service_type":"goroutine","keel_service_name":"demo","keel_service_inst":0}
{"level":"info","msg":"keel graceful shutdown","graceful_period":"3s"}
{"level":"info","msg":"keel graceful shutdown: closers"}
{"level":"info","msg":"stopping keel service","keel_service_type":"goroutine","keel_service_name":"demo"}
{"level":"info","msg":"context has been canceled du to graceful shutdow","keel_service_type":"goroutine","keel_service_name":"demo","keel_service_inst":0}
{"level":"debug","msg":"keel graceful shutdown: closer closed","name":"*service.GoRoutine"}
{"level":"debug","msg":"keel graceful shutdown: closer closed","name":"noop.TracerProvider"}
{"level":"debug","msg":"keel graceful shutdown: closer closed","name":"noop.MeterProvider"}
{"level":"info","msg":"keel graceful shutdown: complete"}
{"level":"info","msg":"keel server stopped"}

func (*GoRoutine) Close

func (s *GoRoutine) Close(ctx context.Context) error

func (*GoRoutine) Healthz

func (s *GoRoutine) Healthz() error

func (*GoRoutine) Name

func (s *GoRoutine) Name() string

func (*GoRoutine) Start

func (s *GoRoutine) Start(ctx context.Context) error

func (*GoRoutine) String

func (s *GoRoutine) String() string

type GoRoutineFn

type GoRoutineFn func(ctx context.Context, l *zap.Logger) error

GoRoutine struct

type GoRoutineOption

type GoRoutineOption func(*GoRoutine)

GoRoutine struct

func GoRoutineWithPralllel

func GoRoutineWithPralllel(v int) GoRoutineOption

type HTTP

type HTTP struct {
	// contains filtered or unexported fields
}

HTTP struct

func NewDefaultHTTPPProf

func NewDefaultHTTPPProf(l *zap.Logger) *HTTP

func NewDefaultHTTPProbes

func NewDefaultHTTPProbes(l *zap.Logger, probes map[healthz.Type][]interface{}) *HTTP

func NewDefaultHTTPPrometheus

func NewDefaultHTTPPrometheus(l *zap.Logger) *HTTP

func NewDefaultHTTPReadme

func NewDefaultHTTPReadme(l *zap.Logger, readmers func() []interfaces.Readmer) *HTTP

func NewDefaultHTTPViper

func NewDefaultHTTPViper(l *zap.Logger) *HTTP

func NewDefaultHTTPZap

func NewDefaultHTTPZap(l *zap.Logger) *HTTP

func NewHTTP

func NewHTTP(l *zap.Logger, name, addr string, handler http.Handler, middlewares ...middleware.Middleware) *HTTP
Example
svr := keel.NewServer(
	keel.WithLogger(zap.NewExample()),
	keel.WithGracefulPeriod(10*time.Second),
)

l := svr.Logger()

svr.AddService(
	service.NewHTTP(l, "demo", "localhost:8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte("OK"))
	})),
)

go func() {
	waitFor("localhost:8080")
	l.Info(httpGet("http://localhost:8080"))
	shutdown()
}()

svr.Run()
Output:

{"level":"info","msg":"starting keel server"}
{"level":"info","msg":"starting keel service","keel_service_type":"http","keel_service_name":"demo","net_host_ip":"localhost","net_host_port":"8080"}
{"level":"info","msg":"OK"}
{"level":"info","msg":"keel graceful shutdown","graceful_period":"10s"}
{"level":"info","msg":"keel graceful shutdown: closers"}
{"level":"info","msg":"stopping keel service","keel_service_type":"http","keel_service_name":"demo"}
{"level":"debug","msg":"keel graceful shutdown: closer closed","name":"*service.HTTP"}
{"level":"debug","msg":"keel graceful shutdown: closer closed","name":"noop.TracerProvider"}
{"level":"debug","msg":"keel graceful shutdown: closer closed","name":"noop.MeterProvider"}
{"level":"info","msg":"keel graceful shutdown: complete"}
{"level":"info","msg":"keel server stopped"}

func NewHTTPPProf

func NewHTTPPProf(l *zap.Logger, name, addr, path string) *HTTP

func NewHTTPPrometheus

func NewHTTPPrometheus(l *zap.Logger, name, addr, path string) *HTTP

func NewHTTPReadme

func NewHTTPReadme(l *zap.Logger, name, addr, path string, readmers func() []interfaces.Readmer) *HTTP

func NewHTTPViper

func NewHTTPViper(l *zap.Logger, c *viper.Viper, name, addr, path string) *HTTP

func NewHTTPZap

func NewHTTPZap(l *zap.Logger, name, addr, path string) *HTTP

func NewHealthz

func NewHealthz(l *zap.Logger, name, addr, path string, probes map[healthz.Type][]interface{}) *HTTP

func (*HTTP) Close

func (s *HTTP) Close(ctx context.Context) error

func (*HTTP) Healthz

func (s *HTTP) Healthz() error

func (*HTTP) Name

func (s *HTTP) Name() string

func (*HTTP) Server added in v0.18.0

func (s *HTTP) Server() *http.Server

func (*HTTP) Start

func (s *HTTP) Start(ctx context.Context) error

func (*HTTP) String

func (s *HTTP) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL