daemon

package
v2.12.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: GPL-3.0 Imports: 28 Imported by: 3

Documentation

Index

Constants

View Source
const (
	DriverBinary   = "/opt/driver/bin/driver"
	GRPCSocket     = "rpc.sock"
	TmpPathPattern = "/tmp/%s"
)

Variables

View Source
var (
	// ErrUnexpected indicates unexpexted unrecoverable error condition.
	ErrUnexpected = errors.NewKind("unexpected error")
	// ErrMissingDriver indicates that a driver image for the given language
	// can not be found.
	ErrMissingDriver = errors.NewKind("missing driver for language %q")
	// ErrRuntime indicates unrecoverable error condition at runtime.
	ErrRuntime = errors.NewKind("runtime failure")
	// ErrAlreadyInstalled indicates that a driver image was already installed
	// from the reference for the given language.
	ErrAlreadyInstalled = protocol.ErrAlreadyInstalled
	// ErrUnauthorized indicates that image registry access failed
	// and it either requires authentication or does not exist.
	ErrUnauthorized = errors.NewKind("unauthorized: authentication required to access %s (image: %s)")
	// ErrLanguageDetection indicates that language was not detected by Enry.
	ErrLanguageDetection = errors.NewKind("could not autodetect language")
	// ErrUnknownEncoding is returned for parse requests with a file content in a non-UTF8 encoding.
	ErrUnknownEncoding = errors.NewKind("unknown source file encoding (expected UTF-8)")
)
View Source
var (
	// DefaultMaxInstancesPerDriver is the maximum number of instances of
	// the same driver which can be launched following the default
	// scaling policy (see DefaultScalingPolicy()).
	//
	// Can be changed by setting BBLFSHD_MAX_DRIVER_INSTANCES.
	DefaultMaxInstancesPerDriver = mustEnvInt("BBLFSHD_MAX_DRIVER_INSTANCES", runtime.NumCPU())
	// DefaultMinInstancesPerDriver is the minimal number of instances of
	// the same driver which will be launched following the default
	// scaling policy (see DefaultScalingPolicy()).
	//
	// Can be changed by setting BBLFSHD_MIN_DRIVER_INSTANCES.
	DefaultMinInstancesPerDriver = mustEnvInt("BBLFSHD_MIN_DRIVER_INSTANCES", 1)

	// ErrPoolClosed is returned if the pool was already closed or is being closed.
	ErrPoolClosed = errors.NewKind("driver pool already closed")
	// ErrPoolRunning is returned if the pool was already running.
	ErrPoolRunning = errors.NewKind("driver pool already running")
)

Functions

func GetLanguage

func GetLanguage(filename string, content []byte) string

GetLanguage detects the language of a file and returns it in a normalized form.

Types

type ControlService added in v1.2.0

type ControlService struct {
	*Daemon
}

func NewControlService added in v1.2.0

func NewControlService(d *Daemon) *ControlService

func (*ControlService) DriverInstanceStates added in v1.2.0

func (s *ControlService) DriverInstanceStates() ([]*protocol.DriverInstanceState, error)

func (*ControlService) DriverPoolStates added in v1.2.0

func (s *ControlService) DriverPoolStates() map[string]*protocol.DriverPoolState

func (*ControlService) DriverStates

func (s *ControlService) DriverStates() ([]*protocol.DriverImageState, error)

type Daemon

type Daemon struct {
	UserServer    *grpc.Server
	ControlServer *grpc.Server
	// contains filtered or unexported fields
}

Daemon is a Babelfish server.

func NewDaemon

func NewDaemon(version string, build time.Time, r *runtime.Runtime, opts ...grpc.ServerOption) *Daemon

NewDaemon creates a new server based on the runtime with the given version.

func (*Daemon) Current added in v1.2.0

func (d *Daemon) Current() map[string]*DriverPool

Current returns the current list of driver pools.

func (*Daemon) DriverPool

func (d *Daemon) DriverPool(ctx context.Context, language string) (*DriverPool, error)

func (*Daemon) InstallDriver

func (d *Daemon) InstallDriver(language string, image string, update bool) error

func (*Daemon) RemoveDriver

func (d *Daemon) RemoveDriver(language string) error

func (*Daemon) Stop

func (d *Daemon) Stop() error

Stop stops all the pools and containers.

type Driver

type Driver interface {
	ID() string
	Start(ctx context.Context) error
	Stop() error
	Status() (protocol.Status, error)
	State() (*protocol.DriverInstanceState, error)
	Service() protocol1.ProtocolServiceClient
	ServiceV2() protocol2.DriverClient
}

type DriverInstance

type DriverInstance struct {
	Language  string
	Process   *runtime.Process
	Container runtime.Container
	Image     runtime.DriverImage
	// contains filtered or unexported fields
}

DriverInstance represents an instance of a driver.

func NewDriverInstance

func NewDriverInstance(r *runtime.Runtime, lang string, i runtime.DriverImage, o *Options) (*DriverInstance, error)

NewDriverInstance represents a running Driver in the runtime. Its holds the container and the connection to the internal grpc server.

func (*DriverInstance) ID added in v1.2.0

func (i *DriverInstance) ID() string

ID returns the container id.

func (*DriverInstance) Service

Service returns the client using the grpc connection.

func (*DriverInstance) ServiceV2

func (i *DriverInstance) ServiceV2() protocol2.DriverClient

ServiceV2 returns the client using the grpc connection.

func (*DriverInstance) Start

func (i *DriverInstance) Start(ctx context.Context) error

Start starts a container and connects to it.

func (*DriverInstance) State added in v1.2.0

State returns the current state of the driver instance.

func (*DriverInstance) Status

func (i *DriverInstance) Status() (protocol.Status, error)

Status returns the current status of the container.

func (*DriverInstance) Stop

func (i *DriverInstance) Stop() error

Stop stops the inner running container.

type DriverPool

type DriverPool struct {
	// ScalingPolicy scaling policy used to scale up the instances.
	ScalingPolicy ScalingPolicy
	// Logger used during the live of the driver pool.
	Logger server.Logger
	// contains filtered or unexported fields
}

DriverPool controls a pool of drivers and balances requests among them, ensuring each driver does not get concurrent requests. The number of driver instances in the driver pool is controlled by a ScalingPolicy.

func NewDriverPool

func NewDriverPool(factory FactoryFunction) *DriverPool

NewDriverPool creates and starts a new DriverPool. It takes as parameters a FactoryFunction, used to instantiate new drivers.

func (*DriverPool) Current added in v1.2.0

func (dp *DriverPool) Current() []Driver

Current returns a list of the current instances from the pool, it includes the running ones and those being stopped.

func (*DriverPool) Execute deprecated

func (dp *DriverPool) Execute(c FunctionCtx, timeout time.Duration) error

Execute executes the given Function in the first available driver instance. It gets a driver from the pool and forwards the request to it. If all drivers are busy, it will return an error after the timeout passes. If the DriverPool is closed, an error will be returned.

Deprecated: use ExecuteCtx instead.

func (*DriverPool) ExecuteCtx

func (dp *DriverPool) ExecuteCtx(rctx context.Context, c FunctionCtx) error

ExecuteCtx executes the given Function in the first available driver instance. It gets a driver from the pool and forwards the request to it. If all drivers are busy, it will return an error after the timeout passes. If the DriverPool is closed, an error will be returned.

func (*DriverPool) Start

func (dp *DriverPool) Start(ctx context.Context) error

Start stats the driver pool.

func (*DriverPool) State added in v1.2.0

func (dp *DriverPool) State() *protocol.DriverPoolState

State current state of driver pool.

func (*DriverPool) Stop

func (dp *DriverPool) Stop() error

Stop stop the driver pool, including all its underlying driver instances.

type FactoryFunction

type FactoryFunction func(ctx context.Context) (Driver, error)

FactoryFunction is a factory function that creates new DriverInstance's.

type FunctionCtx

type FunctionCtx func(ctx context.Context, d Driver) error

FunctionCtx is a function to be executed using a given driver.

type Options

type Options struct {
	LogLevel  string
	LogFormat string
	Env       []string
}

type ScalingPolicy

type ScalingPolicy interface {
	// Scale takes the total number of active instances, number idle instances and the
	// number of requests waiting to get a driver instance. Idle may not be zero even if
	// number of waiting requests is non-zero.
	// Scale returns the new target number of instances to keep running. This number must
	// not be less than 1.
	Scale(total, idle, waiting int) int
}

ScalingPolicy specifies whether instances should be started or stopped to cope with load.

func AIMD

func AIMD(add int, mul float64) ScalingPolicy

AIMD returns a ScalingPolicy of additive increase / multiplicative decrease. Increases are of min(add, load). Decreases are of (unused * mul).

func DefaultScalingPolicy

func DefaultScalingPolicy() ScalingPolicy

DefaultScalingPolicy returns a new instance of the default scaling policy. Instances returned by this function should not be reused.

func MinMax

func MinMax(min, max int, p ScalingPolicy) ScalingPolicy

MinMax wraps a ScalingPolicy and applies a minimum and maximum to the number of instances.

func MovingAverage

func MovingAverage(window int, p ScalingPolicy) ScalingPolicy

MovingAverage computes a moving average of the load and forwards it to the underlying scaling policy. This policy is stateful and not thread-safe, do not reuse its instances for multiple pools.

type Service added in v1.2.0

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

func NewService added in v1.2.0

func NewService(d *Daemon) *Service

func (*Service) NativeParse added in v1.2.0

func (*Service) Parse added in v1.2.0

func (*Service) Version added in v1.2.0

type ServiceV2

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

func NewServiceV2

func NewServiceV2(d *Daemon) *ServiceV2

func (*ServiceV2) Parse

func (s *ServiceV2) Parse(rctx xcontext.Context, req *protocol2.ParseRequest) (resp *protocol2.ParseResponse, gerr error)

Directories

Path Synopsis
Package protocol is a generated protocol buffer package.
Package protocol is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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