servenv

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2019 License: Apache-2.0 Imports: 43 Imported by: 0

Documentation

Overview

Package servenv contains functionality that is common for all Vitess server programs. It defines and initializes command line flags that control the runtime environment.

After a server program has called flag.Parse, it needs to call env.Init to make env use the command line variables to initialize the environment. It also needs to call env.Close before exiting.

Note: If you need to plug in any custom initialization/cleanup for a vitess distribution, register them using OnInit and onClose. A clean way of achieving that is adding to this package a file with an init() function that registers the hooks.

Index

Constants

View Source
const JQueryIncludes = `` /* 287-byte string literal not displayed */

JQueryIncludes is the include to use to be able to use jquery and jquery-ui

Variables

View Source
var (
	// GRPCPort is the port to listen on for gRPC. If not set or zero, don't listen.
	GRPCPort = flag.Int("grpc_port", 0, "Port to listen on for gRPC calls")

	// GRPCCert is the cert to use if TLS is enabled
	GRPCCert = flag.String("grpc_cert", "", "certificate to use, requires grpc_key, enables TLS")

	// GRPCKey is the key to use if TLS is enabled
	GRPCKey = flag.String("grpc_key", "", "key to use, requires grpc_cert, enables TLS")

	// GRPCCA is the CA to use if TLS is enabled
	GRPCCA = flag.String("grpc_ca", "", "ca to use, requires TLS, and enforces client cert check")

	// GRPCAuth which auth plugin to use (at the moment now only static is supported)
	GRPCAuth = flag.String("grpc_auth_mode", "", "Which auth plugin implementation to use (eg: static)")

	// GRPCServer is the global server to serve gRPC.
	GRPCServer *grpc.Server

	// GRPCMaxConnectionAge is the maximum age of a client connection, before GoAway is sent.
	// This is useful for L4 loadbalancing to ensure rebalancing after scaling.
	GRPCMaxConnectionAge = flag.Duration("grpc_max_connection_age", time.Duration(math.MaxInt64), "Maximum age of a client connection before GoAway is sent.")

	// GRPCMaxConnectionAgeGrace is an additional grace period after GRPCMaxConnectionAge, after which
	// connections are forcibly closed.
	GRPCMaxConnectionAgeGrace = flag.Duration("grpc_max_connection_age_grace", time.Duration(math.MaxInt64), "Additional grace period after grpc_max_connection_age, after which connections are forcibly closed.")

	// GRPCInitialConnWindowSize ServerOption that sets window size for a connection.
	// The lower bound for window size is 64K and any value smaller than that will be ignored.
	GRPCInitialConnWindowSize = flag.Int("grpc_server_initial_conn_window_size", 0, "grpc server initial connection window size")

	// GRPCInitialWindowSize ServerOption that sets window size for stream.
	// The lower bound for window size is 64K and any value smaller than that will be ignored.
	GRPCInitialWindowSize = flag.Int("grpc_server_initial_window_size", 0, "grpc server initial window size")

	// EnforcementPolicy MinTime that sets the keepalive enforcement policy on the server.
	// This is the minimum amount of time a client should wait before sending a keepalive ping.
	GRPCKeepAliveEnforcementPolicyMinTime = flag.Duration("grpc_server_keepalive_enforcement_policy_min_time", 5*time.Minute, "grpc server minimum keepalive time")

	// EnforcementPolicy PermitWithoutStream - If true, server allows keepalive pings
	// even when there are no active streams (RPCs). If false, and client sends ping when
	// there are no active streams, server will send GOAWAY and close the connection.
	GRPCKeepAliveEnforcementPolicyPermitWithoutStream = flag.Bool("grpc_server_keepalive_enforcement_policy_permit_without_stream", false, "grpc server permit client keepalive pings even when there are no active streams (RPCs)")
)

This file handles gRPC server, on its own port. Clients register servers, based on service map:

servenv.RegisterGRPCFlags()

servenv.OnRun(func() {
  if servenv.GRPCCheckServiceMap("XXX") {
    pb.RegisterXXX(servenv.GRPCServer, XXX)
  }
}

Note servenv.GRPCServer can only be used in servenv.OnRun, and not before, as it is initialized right before calling OnRun.

View Source
var (
	// Port is part of the flags used when calling RegisterDefaultFlags.
	Port *int

	// ListeningURL is filled in when calling Run, contains the server URL.
	ListeningURL url.URL
)
View Source
var AppVersion versionInfo

AppVersion is the struct to store build info.

View Source
var (
	// SocketFile has the flag used when calling
	// RegisterDefaultSocketFileFlags.
	SocketFile *string
)
View Source
var (

	// Version registers the command line flag to expose build info.
	Version = flag.Bool("version", false, "print binary version")
)

Functions

func AddStatusFuncs

func AddStatusFuncs(fmap template.FuncMap)

AddStatusFuncs merges the provided functions into the set of functions used to render /debug/status. Call this before AddStatusPart if your template requires custom functions.

func AddStatusPart

func AddStatusPart(banner, frag string, f func() interface{})

AddStatusPart adds a new section to status. frag is used as a subtemplate of the template used to render /debug/status, and will be executed using the value of invoking f at the time of the /debug/status request. frag is parsed and executed with the html/template package. Functions registered with AddStatusFuncs may be used in the template.

func AddStatusSection

func AddStatusSection(banner string, f func() string)

AddStatusSection registers a function that generates extra information for /debug/status. If banner is not empty, it will be used as a header before the information. If more complex output than a simple string is required use AddStatusPart instead.

func CheckServiceMap

func CheckServiceMap(protocol, name string) bool

CheckServiceMap returns if we should register a RPC service (and also logs how to enable / disable it)

func Close

func Close()

Close runs any registered exit hooks in parallel.

func FakeAuthStreamInterceptor

func FakeAuthStreamInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

FakeAuthStreamInterceptor fake interceptor to test plugin

func FakeAuthUnaryInterceptor

func FakeAuthUnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

FakeAuthUnaryInterceptor fake interceptor to test plugin

func FireRunHooks

func FireRunHooks()

FireRunHooks fires the hooks registered by OnHook. Use this in a non-server to run the hooks registered by servenv.OnRun().

func GRPCCheckServiceMap

func GRPCCheckServiceMap(name string) bool

GRPCCheckServiceMap returns if we should register a gRPC service (and also logs how to enable / disable it)

func GetAuthenticator

func GetAuthenticator(name string) func() (Authenticator, error)

GetAuthenticator returns an AuthPlugin by name, or log.Fatalf.

func HandlePanic

func HandlePanic(component string, err *error)

HandlePanic should be called using 'defer' in the RPC code that executes the command.

func Init

func Init()

Init is the first phase of the server startup.

func InitServiceMap

func InitServiceMap(protocol, name string)

InitServiceMap will set the default value for a protocol/name to be served.

func OnClose

func OnClose(f func())

OnClose registers f to be run at the end of the app lifecycle. This happens after the lameduck period just before the program exits. All hooks are run in parallel.

func OnInit added in v1.7.0

func OnInit(f func())

OnInit registers f to be run at the beginning of the app lifecycle. It should be called in an init() function.

func OnRun

func OnRun(f func())

OnRun registers f to be run right at the beginning of Run. All hooks are run in parallel.

func OnTerm

func OnTerm(f func())

OnTerm registers a function to be run when the process receives a SIGTERM. This allows the program to change its behavior during the lameduck period.

All hooks are run in parallel, and there is no guarantee that the process will wait for them to finish before dying when the lameduck period expires.

See also: OnTermSync

func OnTermSync

func OnTermSync(f func())

OnTermSync registers a function to be run when the process receives SIGTERM. This allows the program to change its behavior during the lameduck period.

All hooks are run in parallel, and the process will do its best to wait (up to -onterm_timeout) for all of them to finish before dying.

See also: OnTerm

func ParseFlags

func ParseFlags(cmd string)

ParseFlags initializes flags and handles the common case when no positional arguments are expected.

func ParseFlagsWithArgs

func ParseFlagsWithArgs(cmd string) []string

ParseFlagsWithArgs initializes flags and returns the positional arguments

func RegisterAuthPlugin

func RegisterAuthPlugin(name string, authPlugin func() (Authenticator, error))

RegisterAuthPlugin registers an implementation of AuthServer.

func RegisterDefaultFlags

func RegisterDefaultFlags()

RegisterDefaultFlags registers the default flags for listening to a given port for standard connections. If calling this, then call RunDefault()

func RegisterDefaultSocketFileFlags

func RegisterDefaultSocketFileFlags()

RegisterDefaultSocketFileFlags registers the default flags for listening to a socket. This needs to be called before flags are parsed.

func Run

func Run(port int)

Run starts listening for RPC and HTTP requests, and blocks until it the process gets a signal.

func RunDefault

func RunDefault()

RunDefault calls Run() with the parameters from the flags.

func StatusURLPath

func StatusURLPath() string

StatusURLPath returns the path to the status page.

Types

type Authenticator

type Authenticator interface {
	Authenticate(ctx context.Context, fullMethod string) (context.Context, error)
}

Authenticator provides an interface to implement auth in Vitess in grpc server

type StaticAuthConfigEntry

type StaticAuthConfigEntry struct {
	Username string
	Password string
}

StaticAuthConfigEntry is the container for server side credentials. Current implementation matches the the one from the client but this will change in the future as we hooked this pluging into ACL features.

type StaticAuthPlugin

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

StaticAuthPlugin implements static username/password authentication for grpc. It contains an array of username/passwords that will be authorized to connect to the grpc server.

func (*StaticAuthPlugin) Authenticate

func (sa *StaticAuthPlugin) Authenticate(ctx context.Context, fullMethod string) (context.Context, error)

Authenticate implements AuthPlugin interface. This method will be used inside a middleware in grpc_server to authenticate incoming requests.

type WrappedServerStream

type WrappedServerStream struct {
	grpc.ServerStream
	WrappedContext context.Context
}

WrappedServerStream is based on the service stream wrapper from: https://github.com/grpc-ecosystem/go-grpc-middleware

func WrapServerStream

func WrapServerStream(stream grpc.ServerStream) *WrappedServerStream

WrapServerStream returns a ServerStream that has the ability to overwrite context.

func (*WrappedServerStream) Context

func (w *WrappedServerStream) Context() context.Context

Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context()

Jump to

Keyboard shortcuts

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