Documentation ¶
Index ¶
- Constants
- Variables
- func GetLanguage(filename string, content []byte) string
- type ControlService
- type Daemon
- type Driver
- type DriverInstance
- func (i *DriverInstance) ID() string
- func (i *DriverInstance) Service() protocol1.ProtocolServiceClient
- func (i *DriverInstance) ServiceV2() protocol2.DriverClient
- func (i *DriverInstance) Start() error
- func (i *DriverInstance) State() (*protocol.DriverInstanceState, error)
- func (i *DriverInstance) Status() (protocol.Status, error)
- func (i *DriverInstance) Stop() error
- type DriverPool
- func (dp *DriverPool) Current() []Driver
- func (dp *DriverPool) Execute(c Function, timeout time.Duration) error
- func (dp *DriverPool) ExecuteCtx(ctx context.Context, c FunctionCtx) error
- func (dp *DriverPool) Start() error
- func (dp *DriverPool) State() *protocol.DriverPoolState
- func (dp *DriverPool) Stop() error
- type FactoryFunction
- type Function
- type FunctionCtx
- type Options
- type ScalingPolicy
- type Service
- func (d *Service) NativeParse(req *protocol1.NativeParseRequest) *protocol1.NativeParseResponse
- func (d *Service) Parse(req *protocol1.ParseRequest) *protocol1.ParseResponse
- func (d *Service) SupportedLanguages(req *protocol1.SupportedLanguagesRequest) *protocol1.SupportedLanguagesResponse
- func (d *Service) Version(req *protocol1.VersionRequest) *protocol1.VersionResponse
- type ServiceV2
Constants ¶
const ( DriverBinary = "/opt/driver/bin/driver" GRPCSocket = "rpc.sock" TmpPathPattern = "/tmp/%s" ConnectionTimeout = 5 * time.Second )
const ( // DefaultPoolTimeout is the time a request to the DriverPool can wait // before getting a driver assigned. DefaultPoolTimeout = 5 * time.Second // MaxPoolTimeout maximum time allowed to wait for a driver be assigned. MaxPoolTimeout = 5 * time.Minute )
Variables ¶
var ( ErrUnexpected = errors.NewKind("unexpected error") ErrMissingDriver = errors.NewKind("missing driver for language %q") ErrRuntime = errors.NewKind("runtime failure") ErrAlreadyInstalled = errors.NewKind("driver already installed: %s (image reference: %s)") ErrLanguageDetection = errors.NewKind("could not autodetect language") )
var ( // DefaultMaxInstancesPerDriver is the maximum number of instances of // the same driver which can be launched following the default // scaling policy (see DefaultScalingPolicy()). DefaultMaxInstancesPerDriver = runtime.NumCPU() ErrPoolClosed = errors.NewKind("driver pool already closed") ErrPoolTimeout = errors.NewKind("timeout, all drivers are busy") ErrInvalidPoolTimeout = errors.NewKind(fmt.Sprintf("invalid timeout: %%v, max. timeout allowed %s", MaxPoolTimeout)) ErrNegativeInstances = errors.NewKind("cannot set instances to negative number") )
Functions ¶
func GetLanguage ¶
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 (*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(language string) (*DriverPool, error)
func (*Daemon) InstallDriver ¶
func (*Daemon) RemoveDriver ¶
type Driver ¶
type Driver interface { ID() string Start() 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 ¶
func (i *DriverInstance) Service() protocol1.ProtocolServiceClient
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() error
Start starts a container and connects to it.
func (*DriverInstance) State ¶ added in v1.2.0
func (i *DriverInstance) State() (*protocol.DriverInstanceState, error)
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 // Timeout time for wait until a driver instance is available. Timeout time.Duration // 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 ¶
func (dp *DriverPool) Execute(c Function, 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.
func (*DriverPool) ExecuteCtx ¶
func (dp *DriverPool) ExecuteCtx(ctx 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) 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 ¶
FactoryFunction is a factory function that creates new DriverInstance's.
type FunctionCtx ¶
FunctionCtx is a function to be executed using a given driver.
type ScalingPolicy ¶
type ScalingPolicy interface { // Scale takes the number of total instances and the load. The load is // the number of request waiting or, there is none, it is a negative // value indicating how many instances are ready. Scale(total, load 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 (ready / 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 (*Service) NativeParse ¶ added in v1.2.0
func (d *Service) NativeParse(req *protocol1.NativeParseRequest) *protocol1.NativeParseResponse
func (*Service) Parse ¶ added in v1.2.0
func (d *Service) Parse(req *protocol1.ParseRequest) *protocol1.ParseResponse
func (*Service) SupportedLanguages ¶
func (d *Service) SupportedLanguages(req *protocol1.SupportedLanguagesRequest) *protocol1.SupportedLanguagesResponse
func (*Service) Version ¶ added in v1.2.0
func (d *Service) Version(req *protocol1.VersionRequest) *protocol1.VersionResponse