baseplate

package module
v0.9.17 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: BSD-3-Clause Imports: 16 Imported by: 0

README

baseplate.go

Baseplate implemented in go.

Documentation

Code documentation

IDE/Editor setup

See here.

Code style guide

See here.

Thrift generated files

The internal/gen-go/ directory contains thrift generated files. They are excluded from the linter. DO NOT EDIT.

They were generated with thrift compiler v0.17.0 against baseplate.thrift using the following commands under internal/:

thrift --gen go:package_prefix=github.com/reddit/baseplate.go/,skip_remote path/to/baseplate.thrift

They are needed by some of the Baseplate.go packages. We did not include those thrift files into this repo to avoid duplications. This directory will be regenerated when either thrift compiler or the thrift files changed significantly.

Documentation

Overview

Package baseplate provides a batteries-included starting point for services using Baseplate.go.

This package provides convenient initialization bundles needed in your main function, and initialize the subpackages for you. For concrete features provided by Baseplate.go packages, please refer to the documentation of the subdirectories.

For an example of how your main function should look like, please refer to https://pkg.go.dev/github.com/reddit/baseplate.go/thriftbp?tab=doc#example-NewBaseplateServer for thrift services and https://pkg.go.dev/github.com/reddit/baseplate.go/httpbp?tab=doc#example-NewBaseplateServer for HTTP services.

Index

Examples

Constants

View Source
const (
	// DefaultStopDelay is the default StopDelay to be used in Serve.
	DefaultStopDelay = 5 * time.Second
)

Variables

This section is empty.

Functions

func ParseConfigYAML added in v0.9.0

func ParseConfigYAML(cfgPointer Configer) error

ParseConfigYAML loads the baseplate config into the structed pointed to by cfgPointer.

The configuration file is located based on the $BASEPLATE_CONFIG_PATH environment variable.

To avoid easy mistakes, "strict" mode is enabled while parsing the file, which means that all values in the YAML config must have a matching struct or value during decoding.

If you don't have any customized configurations to decode from YAML, you can just pass in a *pointer* to baseplate.Config:

var cfg baseplate.Config
if err := baseplate.ParseConfigYAML(&cfg); err != nil {
  log.Fatalf("Parsing config: %s", err)
}
ctx, bp, err := baseplate.New(baseplate.NewArgs{
  EdgeContextFactory: edgecontext.Factory(...),
  Config:             cfg,
})

If you do have customized configurations to decode from YAML, embed a baseplate.Config with `yaml:",inline"` yaml tags, for example:

type myServiceConfig struct {
  // The yaml tag is required to pass strict parsing.
  baseplate.Config `yaml:",inline"`

  // Actual configs
  FancyName string `yaml:"fancy_name"`
}
var cfg myServiceCfg
if err := baseplate.ParseConfigYAML(&cfg); err != nil {
  log.Fatalf("Parsing config: %s", err)
}
ctx, bp, err := baseplate.New(baseplate.NewArgs{
  EdgeContextFactory: edgecontext.Factory(...),
  Config:             cfg,
})

Environment variable references (e.g. $FOO and ${FOO}) are substituted into the YAML from the process-level environment before parsing the configuration.

func Serve

func Serve(ctx context.Context, args ServeArgs) error

Serve runs the given Server until it is given an external shutdown signal.

It uses runtimebp.HandleShutdown to handle the signal and gracefully shut down, in order:

* any provided PreShutdown closers,

* the Server, and

* any provided PostShutdown closers.

Returns the (possibly nil) error returned by "Close", or context.DeadlineExceeded if it times out.

If a StopTimeout is configured, Serve will wait for that duration for the server to stop before timing out and returning to force a shutdown.

This is the recommended way to run a Baseplate Server rather than calling server.Start/Stop directly.

Types

type Baseplate

type Baseplate interface {
	io.Closer

	Configer

	EdgeContextImpl() ecinterface.Interface
	Secrets() *secrets.Store
}

Baseplate is the general purpose object that you build a Server on.

func New

New initializes Baseplate libraries with the given config, (logging, secrets, tracing, edge context, etc.), and returns the "serve" context and a new Baseplate to run your service on. The returned context will be cancelled when the Baseplate is closed.

func NewTestBaseplate

func NewTestBaseplate(args NewTestBaseplateArgs) Baseplate

NewTestBaseplate returns a new Baseplate using the given Config and secrets Store that can be used in testing.

NewTestBaseplate only returns a Baseplate, it does not initialize any of the monitoring or logging frameworks.

type Config

type Config struct {
	// Addr is the local address to run your server on.
	//
	// It should be in the format "${IP}:${Port}", "localhost:${Port}",
	// or simply ":${Port}".
	Addr string `yaml:"addr"`

	// Deprecated: No-op for now, will be removed in a future release.
	Timeout time.Duration `yaml:"timeout"`

	// StopTimeout is the timeout for the Stop command for the service.
	//
	// If this is not set, then a default value of 30 seconds will be used.
	// If this is less than 0, then no timeout will be set on the Stop command.
	StopTimeout time.Duration `yaml:"stopTimeout"`

	// Delay after receiving termination signal (SIGTERM, etc.) before kicking off
	// the graceful shutdown process. This happens before the PreShutdown closers.
	//
	// By default this is 1s (DefaultStopDelay).
	// To disable it, set it to a negative value.
	StopDelay time.Duration `yaml:"stopDelay"`

	Log     log.Config       `yaml:"log"`
	Runtime runtimebp.Config `yaml:"runtime"`
	Secrets secrets.Config   `yaml:"secrets"`
	Sentry  log.SentryConfig `yaml:"sentry"`
	Tracing tracing.Config   `yaml:"tracing"`

	// Deprecated: statsd metrics are deprecated.
	Metrics metricsbp.Config `yaml:"metrics"`
}

Config is a general purpose config for assembling a Baseplate server.

It implements Configer.

func (Config) GetConfig added in v0.9.0

func (c Config) GetConfig() Config

GetConfig implements Configer.

type Configer added in v0.9.0

type Configer interface {
	GetConfig() Config
}

Configer defines the interface that allows you to extend Config with your own configurations.

type HealthCheckCloser added in v0.9.0

type HealthCheckCloser interface {
	HealthChecker
	io.Closer
}

HealthCheckCloser is the combination of HealthChecker and io.Closer.

func Drainer added in v0.9.0

func Drainer() HealthCheckCloser

Drainer creates a HealthCheckCloser implementation that can be used to drain service during graceful shutdown.

The HealthCheckCloser returned would start to report healthy once created, and start to report unhealthy as soon as its Close is called.

Please refer to the example on how to use this feature in your service. Basically you should create a Drainer in your main function, add it to the PreShutdown closers list in baseplate.Serve, and then fail your readiness health check when drainer is reporting unhealty.

Its Close function would never return an error. It's also OK to call Close function multiple times. Calls after the first one are essentially no-ops.

Example

This example demonstrates how to use baseplate.Drainer in your main function and service's IsHealthy handler.

package main

import (
	"context"
	"flag"
	"io"

	"github.com/reddit/baseplate.go"
	baseplatethrift "github.com/reddit/baseplate.go/internal/gen-go/reddit/baseplate"
	"github.com/reddit/baseplate.go/log"
	"github.com/reddit/baseplate.go/thriftbp"
)

// A placeholder thrift service for the example.
type Service struct {
	drainer baseplate.HealthCheckCloser
}

func (s *Service) IsHealthy(ctx context.Context, req *baseplatethrift.IsHealthyRequest) (bool, error) {
	switch req.GetProbe() {
	default:
		// For unknown probes, default to readiness.
		fallthrough
	case baseplatethrift.IsHealthyProbe_READINESS:
		return s.drainer.IsHealthy(ctx) /* && other healthy dependencies */, nil

	case baseplatethrift.IsHealthyProbe_LIVENESS:
		return true /* && other healthy dependencies */, nil
	}
}

// This example demonstrates how to use baseplate.Drainer in your main function
// and service's IsHealthy handler.
func main() {
	flag.Parse()
	ctx, bp, err := baseplate.New(context.Background(), baseplate.NewArgs{
		// TODO: fill in NewArgs.
	})
	if err != nil {
		log.Fatal(err)
	}
	defer bp.Close()

	// TODO: Other initializations
	drainer := baseplate.Drainer()
	// TODO: Other initializations

	processor := baseplatethrift.NewBaseplateServiceV2Processor(&Service{
		drainer: drainer,
	})
	server, err := thriftbp.NewBaseplateServer(bp, thriftbp.ServerConfig{
		Processor: processor,
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Info(baseplate.Serve(ctx, baseplate.ServeArgs{
		Server: server,
		PreShutdown: []io.Closer{
			drainer,
			// TODO: Other pre-shutdown closers
		},
		PostShutdown: []io.Closer{
			// TODO: Post-shutdown closers
		},
	}))
}
Output:

type HealthChecker added in v0.9.0

type HealthChecker interface {
	IsHealthy(ctx context.Context) bool
}

HealthChecker defines an interface to report healthy status.

type NewArgs added in v0.8.0

type NewArgs struct {
	// Required. New will panic if this is nil.
	Config Configer

	// Required. New will panic if this is nil.
	//
	// The factory to be used to create edge context implementation.
	EdgeContextFactory ecinterface.Factory
}

NewArgs defines the args used in New functino.

type NewTestBaseplateArgs added in v0.8.0

type NewTestBaseplateArgs struct {
	Config          Config
	Store           *secrets.Store
	EdgeContextImpl ecinterface.Interface
}

NewTestBaseplateArgs defines the args used by NewTestBaseplate.

type ServeArgs added in v0.7.0

type ServeArgs struct {
	// Server is the Server that should be run until receiving a shutdown signal.
	// This is a required argument and baseplate.Serve will panic if this is nil.
	Server Server

	// PreShutdown is an optional slice of io.Closers that should be gracefully
	// shut down before the server upon receipt of a shutdown signal.
	PreShutdown []io.Closer

	// PostShutdown is an optional slice of io.Closers that should be gracefully
	// shut down after the server upon receipt of a shutdown signal.
	PostShutdown []io.Closer
}

ServeArgs provides a list of arguments to Serve.

type Server

type Server interface {
	// Close should stop the server gracefully and only return after the server has
	// finished shutting down.
	//
	// It is recommended that you use baseplate.Serve() rather than calling Close
	// directly as baseplate.Serve will manage starting your service as well as
	// shutting it down gracefully in response to a shutdown signal.
	io.Closer

	// Baseplate returns the Baseplate object the server is built on.
	Baseplate() Baseplate

	// Serve should start the Server on the Addr given by the Config and only
	// return once the Server has stopped.
	//
	// It is recommended that you use baseplate.Serve() rather than calling Serve
	// directly as baseplate.Serve will manage starting your service as well as
	// shutting it down gracefully.
	Serve() error
}

Server is the primary interface for baseplate servers.

Directories

Path Synopsis
Package batchcloser provides an object "BatchCloser" that collects multiple io.Closers and closes them all when Closers.Close is called.
Package batchcloser provides an object "BatchCloser" that collects multiple io.Closers and closes them all when Closers.Close is called.
Package breakerbp integrates with https://github.com/sony/gobreaker and provides a thrift compatible circuit breaker implementation.
Package breakerbp integrates with https://github.com/sony/gobreaker and provides a thrift compatible circuit breaker implementation.
Package clientpool provides implementations of a generic client pool.
Package clientpool provides implementations of a generic client pool.
cmd
lib/healthcheck
Package healthcheck implements the logic for healthcheck binary.
Package healthcheck implements the logic for healthcheck binary.
Package configbp parses configurations per the baseplate spec.
Package configbp parses configurations per the baseplate spec.
Package detach provides utility function for getting new contexts that are detached from their parent's timeouts and cancel calls but inheriting any values registered with the library.
Package detach provides utility function for getting new contexts that are detached from their parent's timeouts and cancel calls but inheriting any values registered with the library.
Package ecinterface defines the interfaces of edgecontext package used in Baseplate.go.
Package ecinterface defines the interfaces of edgecontext package used in Baseplate.go.
Package errorsbp provides some error utilities for Baseplate.go project.
Package errorsbp provides some error utilities for Baseplate.go project.
Package events implements event publisher per baseplate spec.
Package events implements event publisher per baseplate spec.
Package experiments provides implementation of experiments config parsing and other experiments related features according to baseplate spec.
Package experiments provides implementation of experiments config parsing and other experiments related features according to baseplate spec.
Package filewatcher provides a go implementation of baseplate's FileWatcher: https://baseplate.readthedocs.io/en/stable/api/baseplate/lib/file_watcher.html
Package filewatcher provides a go implementation of baseplate's FileWatcher: https://baseplate.readthedocs.io/en/stable/api/baseplate/lib/file_watcher.html
Package grpcbp provides Baseplate specific gRPC related helpers.
Package grpcbp provides Baseplate specific gRPC related helpers.
Package httpbp provides Baseplate specific helpers and integrations for http services.
Package httpbp provides Baseplate specific helpers and integrations for http services.
internal
admin
Package admin is baseplate boilerplate for common internal admin features.
Package admin is baseplate boilerplate for common internal admin features.
limitopen
Package limitopen provides an alternative to os.Open, to limit the size of the file read into memory.
Package limitopen provides an alternative to os.Open, to limit the size of the file read into memory.
prometheusbpint
Package prometheusbpint provides common code used by Baseplate.go regarding Prometheus metrics.
Package prometheusbpint provides common code used by Baseplate.go regarding Prometheus metrics.
prometheusbpint/spectest
Package spectest contains helpers to test for baseplate spec compliance for Prometheus metrics in baseplate.go packages.
Package spectest contains helpers to test for baseplate spec compliance for Prometheus metrics in baseplate.go packages.
thriftint
Package thriftint provides internal thrift related helpers to avoid circular dependencies.
Package thriftint provides internal thrift related helpers to avoid circular dependencies.
Package internalv2compat is an INTERNAL ONLY library provided for transitional projects that need to use baseplate v2 and v0 in the same module.
Package internalv2compat is an INTERNAL ONLY library provided for transitional projects that need to use baseplate v2 and v0 in the same module.
Package iobp provides useful implementations of interfaces defined in stdlib io package.
Package iobp provides useful implementations of interfaces defined in stdlib io package.
Package kafkabp provides Apache Kafka library implementations.
Package kafkabp provides Apache Kafka library implementations.
Package log provides a wrapped zap logger interface for microservices to use, and also a simple Wrapper interface to be used by other Baseplate.go packages.
Package log provides a wrapped zap logger interface for microservices to use, and also a simple Wrapper interface to be used by other Baseplate.go packages.
Package metricsbp provides metrics related features for baseplate.go, based on go-kit metrics package.
Package metricsbp provides metrics related features for baseplate.go, based on go-kit metrics package.
Package mqsend is a pure go implementation of posix message queue for Linux, using syscalls.
Package mqsend is a pure go implementation of posix message queue for Linux, using syscalls.
Package prometheusbp provides common code used for exporting Prometheus metrics.
Package prometheusbp provides common code used for exporting Prometheus metrics.
promtest
Package promtest provides test utilites for testing Prometheus metrics.
Package promtest provides test utilites for testing Prometheus metrics.
Package randbp provides some random generator related features:
Package randbp provides some random generator related features:
redis
cache/redispipebp
Package redispipebp provides baseplate.go integrations for the redispipe library that conform to the redix.Sync interface.
Package redispipebp provides baseplate.go integrations for the redispipe library that conform to the redix.Sync interface.
cache/redisx
Package redisx wraps https://github.com/joomcode/redispipe in an interface where rather than returning the result, you pass in a pointer to the variable you want to put the result into and it uses reflection to do that.
Package redisx wraps https://github.com/joomcode/redispipe in an interface where rather than returning the result, you pass in a pointer to the variable you want to put the result into and it uses reflection to do that.
db/redisbp
Package redisbp provides Baseplate integrations for go-redis.
Package redisbp provides Baseplate integrations for go-redis.
internal/redisprom
Package redisprom provides objects to report metrics according to the Baseplate.spec
Package redisprom provides objects to report metrics according to the Baseplate.spec
Package retrybp integrates with https://github.com/avast/retry-go that provides some baseplate-specific logic and changes some defaults.
Package retrybp integrates with https://github.com/avast/retry-go that provides some baseplate-specific logic and changes some defaults.
Package runtimebp provides extensions to the stdlib runtime package.
Package runtimebp provides extensions to the stdlib runtime package.
internal/maxprocs
Package maxprocs optimizes GOMAXPROCS for Infrared environments.
Package maxprocs optimizes GOMAXPROCS for Infrared environments.
Package secrets provides the functionality to access secrets from Vault by reading them out of a JSON file with automatic refresh on change.
Package secrets provides the functionality to access secrets from Vault by reading them out of a JSON file with automatic refresh on change.
Package signing implements Baseplate's message signing protocol.
Package signing implements Baseplate's message signing protocol.
Package thriftbp provides Baseplate specific thrift related helpers.
Package thriftbp provides Baseplate specific thrift related helpers.
thrifttest
Package thrifttest contains objects and utility methods to aid with testing code using Thrift clients and servers.
Package thrifttest contains objects and utility methods to aid with testing code using Thrift clients and servers.
Package timebp defines some time related types used by various baseplate components, along with their helper functions.
Package timebp defines some time related types used by various baseplate components, along with their helper functions.
Package tracing provides tracing integration with zipkin.
Package tracing provides tracing integration with zipkin.
Package transport provides common variables and types that are used across different transport implementations, i.e.
Package transport provides common variables and types that are used across different transport implementations, i.e.

Jump to

Keyboard shortcuts

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