baseserver

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: Apache-2.0 Imports: 34 Imported by: 2

Documentation

Index

Constants

View Source
const (
	FAILURE_CODE   = -10
	ENV_CH_NUM_CPU = "CH_NUM_CPU"
)
View Source
const (
	MS_LOG_PREFIX = "baseserver.metaserver "
)

Variables

View Source
var BaseServerConfigurationDefaults = BaseServerConfiguration{
	LogToStdout:             true,
	LogLevel:                "info",
	MetricsDestination:      "none",
	MetaListen:              "",
	ShutdownSettleTime:      1 * time.Second,
	HealthCheckStartupDelay: 5 * time.Second,
	HealthCheckPeriod:       30 * time.Second,
	HealthCheckTimeout:      5 * time.Second,
	PropsRefreshPeriod:      5 * time.Minute,
	OllyDataset:             "",
	OllyWriteKey:            "",
}
View Source
var ENV_BLACKLIST = []string{
	"connection_string",
	"secret",
	"password",
	"token",
	"credential",
	"pg_connection",
	"pg_write_connection",
	"mailer",
	"alert_connection",
	"new_relic_api_key",
}

All lowercase here please

Functions

func GetGlobalFeatureService

func GetGlobalFeatureService() features.FeatureService

GetGlobalFeatureService looks up the feature service on globalBaseServer. If it doesn't exist (i.e. during tests), that's ok, give us an empty one.

func GetGlobalPropertyService

func GetGlobalPropertyService() properties.PropertyService

GetGlobalPropertyService looks up the property service on globalBaseServer. If it doesn't exist (i.e. during tests), that's ok, give us an empty one.

func InitGlobalBaseServerTestingOnly

func InitGlobalBaseServerTestingOnly(propertyMap map[string]string, defaultPropertyBacking properties.PropertyBacking)

InitGlobalBaseServerTestingOnly is for tests to create a BaseServer and play with its PropertyService and FeatureService.

func ResetGlobalBaseServerTestingOnly

func ResetGlobalBaseServerTestingOnly()

ResetGlobalBaseServerTestingOnly should be defered after a call to InitGlobalBaseServerTestingOnly.

Types

type BaseServer

type BaseServer struct {
	*BaseServerConfiguration

	Logger *logger.Logger
	// contains filtered or unexported fields
}

func Boilerplate

func Boilerplate(serviceName string, versionInfo version.VersionInfo, defaultPropertyBacking properties.PropertyBacking, mextra interface{}, cfg *ktranslate.ServerConfig) *BaseServer

Perform baseserver initialization steps -- hopefully 9 out of 10 services can just call this and Run()

func BoilerplateWithPrefix

func BoilerplateWithPrefix(serviceName string, versionInfo version.VersionInfo, metricsPrefix string, defaultPropertyBacking properties.PropertyBacking, mextra interface{}, cfg *ktranslate.ServerConfig) *BaseServer

For when you need to set metrics prefix.

func GetGlobalBaseServer

func GetGlobalBaseServer() *BaseServer

func NewBaseServer

func NewBaseServer(serviceName string, version version.VersionInfo, metricsPrefix string, defaultPropertyBacking properties.PropertyBacking, cfg *ktranslate.ServerConfig) *BaseServer

func (*BaseServer) Fail

func (bs *BaseServer) Fail(msg string)

func (*BaseServer) FinishLogger

func (bs *BaseServer) FinishLogger()

func (*BaseServer) FinishOlly

func (bs *BaseServer) FinishOlly()

func (*BaseServer) GetFeatureService

func (bs *BaseServer) GetFeatureService() features.FeatureService

func (*BaseServer) GetHealthCheckHandler

func (bs *BaseServer) GetHealthCheckHandler() func(w http.ResponseWriter, r *http.Request)

func (*BaseServer) GetPropertyService

func (bs *BaseServer) GetPropertyService() properties.PropertyService

func (*BaseServer) Init

func (bs *BaseServer) Init(mextra interface{})

Perform some early initialization steps -- things it makes sense to do before callers start building/initializing anything from the actual service. Most things should probably be started from Run(). Called by Boilerplate().

func (*BaseServer) InitLogger

func (bs *BaseServer) InitLogger(stdout bool, loglevel string)

Initialize logging.

func (*BaseServer) InitMaxProcs

func (bs *BaseServer) InitMaxProcs()

Set the number of cpus this process can use.

func (*BaseServer) InitMetrics

func (bs *BaseServer) InitMetrics(extra interface{})

Initialize metrics.

func (*BaseServer) InitOlly

func (bs *BaseServer) InitOlly()

Initialize olly observability.

func (*BaseServer) OllyBuilder

func (bs *BaseServer) OllyBuilder() *olly.Builder

func (*BaseServer) Run

func (bs *BaseServer) Run(service Service)

Finish initializing and run until signaled otherwise. Spawns sub routines.

func (*BaseServer) SetLogTee

func (bs *BaseServer) SetLogTee(logChan chan string)

Tell base logger to also log messages along this tee

func (*BaseServer) Shutdown

func (bs *BaseServer) Shutdown(reason string)

func (*BaseServer) WaitUntilReady

func (bs *BaseServer) WaitUntilReady(timeout time.Duration)

type BaseServerConfiguration

type BaseServerConfiguration struct {
	// base service properties
	ServiceName string
	VersionInfo version.VersionInfo

	// operational
	ShutdownSettleTime time.Duration

	// logging
	LogToStdout bool
	LogLevel    string
	LogPrefix   string

	// metrics
	MetricsPrefix      string
	MetricsDestination string

	// olly
	OllyWriteKey string
	OllyDataset  string

	// meta server properties
	MetaListen string

	// healthchecks
	HealthCheckStartupDelay time.Duration
	HealthCheckPeriod       time.Duration
	HealthCheckTimeout      time.Duration

	// props
	PropsRefreshPeriod time.Duration

	// Skip env dump
	SkipEnvDump bool
}

type HealthCheckExecutor

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

func NewHealthCheckExecutor

func NewHealthCheckExecutor(service Service, startupDelay time.Duration, period time.Duration, timeout time.Duration) *HealthCheckExecutor

func (*HealthCheckExecutor) GetResult

func (hce *HealthCheckExecutor) GetResult() *HealthCheckResult

func (*HealthCheckExecutor) Run

func (hce *HealthCheckExecutor) Run(ctx context.Context)

type HealthCheckResult

type HealthCheckResult struct {
	Stamp   time.Time `json:"stamp"`
	Success bool      `json:"success"`
	Error   string    `json:"error"`
}

func (*HealthCheckResult) Fail

func (hcr *HealthCheckResult) Fail(message string)

Fail this healthcheck execution with the given message and panics.

func (*HealthCheckResult) FailOnError

func (hcr *HealthCheckResult) FailOnError(description string, err error)

Fail this healthcheck execution and panics if err is not nil.

func (*HealthCheckResult) FailOnSqlDbError

func (hcr *HealthCheckResult) FailOnSqlDbError(ctx context.Context, description string, db *sql.DB)

Fail this healthcheck execution and panics if the given sql.DB is dead

type MetaServer

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

func NewMetaServer

func NewMetaServer(listen string, serviceName string, version version.VersionInfo, service Service, log *logger.Logger, initialLogLevel logger.Level, hce *HealthCheckExecutor) *MetaServer

func (*MetaServer) Run

func (ms *MetaServer) Run(ctx context.Context) error

type Service

type Service interface {
	// Run this service until the given context is closed, after which the service cannot be re-started. Blocks.
	Run(ctx context.Context) error

	GetStatus() []byte                                             // for legacy healthcheck
	RunHealthCheck(ctx context.Context, result *HealthCheckResult) // new style healthcheck

	HttpInfo(w http.ResponseWriter, req *http.Request) // Hook to provide http info via metaserver

	// These are needed in case we are running under windows.
	Init(svc.Environment) error

	Start() error

	Stop() error
}

Service interface - baseserver/metaserver use these methods to interact with actual services

Jump to

Keyboard shortcuts

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