marp | theme | _class | title |
---|---|---|---|
true |
gaia |
invert |
go flexible |
A collection of packages for building Go services.
- Provide
flex
ible primitives to build upon. - Simplify process lifecycle and graceful shutdown.
- No third party dependencies allowed within
flex
itself. - All published packages must be tested.
A Runner
represents anything which can "run" itself.
For example, an HTTP
server.
// Runner represents the behaviour for running a service worker.
type Runner interface {
// Run should run start processing the worker and be a blocking operation.
Run(context.Context) error
}
A Halter
represents anything which can "halt" itself.
For example a Kafka
broker.
// Halter represents the behaviour for stopping a service worker.
type Halter interface {
// Halt should tell the worker to stop doing work.
Halt(context.Context) error
}
A Worker
represents anything that can both "run" and "halt" itself.
For example, a cron
job.
// Worker represents the behaviour for a service worker.
type Worker interface {
Runner
Halter
}
- Consider
io.Reader
andio.Writer
. - Both single method interfaces.
- Both result in incredibly varried uses.
The goal of flex
is to provide a similar experience, for building k8s
native applications.
type Server struct{ *http.Server }
func NewHTTPServer(s *http.Server) *Server {
return &Server{Server: s}
}
func (s *Server) Run(_ context.Context) error {
log.Printf("serving on: http://localhost%s\n", s.Addr)
return s.ListenAndServe()
}
func (s *Server) Halt(ctx context.Context) error {
return s.Shutdown(ctx)
}
flexmetrics
exposes prometheus and pprof metrics on an http server.
It's Worker
is implemented by the concrete Server
type.
type Server struct {
Path string
Server *http.Server
}
// Run will start the metrics server.
func (s *Server) Run(_ context.Context) error {
/* abbreviated... */
mux := http.NewServeMux()
mux.Handle(s.Path, promhttp.Handler())
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
/* abbreviated... */
}
- Environment variables for configuration
- Configurable port-bindings
- Configurable http server
- Sane defaults if you choose to use them
- Build a non-trivial order system (think e-commerce).
- Demonstrate how
flex
eliminates a lot of complicated setup. - Provide working example usecases for future reference.