mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fn: pre-fork pool for namespace/network speedup (#874)
* fn: pre-fork pool experimental implementation
This commit is contained in:
@@ -52,6 +52,7 @@ type DockerDriver struct {
|
||||
docker dockerClient // retries on *docker.Client, restricts ad hoc *docker.Client usage / retries
|
||||
hostname string
|
||||
auths map[string]docker.AuthConfiguration
|
||||
pool DockerPool
|
||||
}
|
||||
|
||||
// implements drivers.Driver
|
||||
@@ -75,6 +76,10 @@ func NewDocker(conf drivers.Config) *DockerDriver {
|
||||
}
|
||||
}
|
||||
|
||||
if conf.PreForkPoolSize != 0 {
|
||||
driver.pool = NewDockerPool(conf, driver)
|
||||
}
|
||||
|
||||
return driver
|
||||
}
|
||||
|
||||
@@ -118,6 +123,14 @@ func registryFromEnv() map[string]docker.AuthConfiguration {
|
||||
return auths.Configs
|
||||
}
|
||||
|
||||
func (drv *DockerDriver) Close() error {
|
||||
var err error
|
||||
if drv.pool != nil {
|
||||
err = drv.pool.Close()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (drv *DockerDriver) Prepare(ctx context.Context, task drivers.ContainerTask) (drivers.Cookie, error) {
|
||||
ctx, log := common.LoggerWithFields(ctx, logrus.Fields{"stack": "Prepare"})
|
||||
var cmd []string
|
||||
@@ -141,7 +154,6 @@ func (drv *DockerDriver) Prepare(ctx context.Context, task drivers.ContainerTask
|
||||
MemorySwap: int64(task.Memory()), // disables swap
|
||||
KernelMemory: int64(task.Memory()),
|
||||
CPUShares: drv.conf.CPUShares,
|
||||
Hostname: drv.hostname,
|
||||
Image: task.Image(),
|
||||
Volumes: map[string]struct{}{},
|
||||
OpenStdin: true,
|
||||
@@ -159,6 +171,20 @@ func (drv *DockerDriver) Prepare(ctx context.Context, task drivers.ContainerTask
|
||||
Context: ctx,
|
||||
}
|
||||
|
||||
poolId := ""
|
||||
if drv.pool != nil {
|
||||
id, err := drv.pool.AllocPoolId()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Could not fetch pre fork pool container")
|
||||
} else {
|
||||
poolId = id
|
||||
container.HostConfig.NetworkMode = fmt.Sprintf("container:%s", id)
|
||||
}
|
||||
} else {
|
||||
// hostname and container NetworkMode is not compatible.
|
||||
container.Config.Hostname = drv.hostname
|
||||
}
|
||||
|
||||
// Translate milli cpus into CPUQuota & CPUPeriod (see Linux cGroups CFS cgroup v1 documentation)
|
||||
// eg: task.CPUQuota() of 8000 means CPUQuota of 8 * 100000 usecs in 100000 usec period,
|
||||
// which is approx 8 CPUS in CFS world.
|
||||
@@ -210,16 +236,20 @@ func (drv *DockerDriver) Prepare(ctx context.Context, task drivers.ContainerTask
|
||||
}
|
||||
|
||||
// discard removal error
|
||||
return &cookie{id: task.Id(), task: task, drv: drv}, nil
|
||||
return &cookie{id: task.Id(), task: task, drv: drv, poolId: poolId}, nil
|
||||
}
|
||||
|
||||
type cookie struct {
|
||||
id string
|
||||
task drivers.ContainerTask
|
||||
drv *DockerDriver
|
||||
id string
|
||||
poolId string
|
||||
task drivers.ContainerTask
|
||||
drv *DockerDriver
|
||||
}
|
||||
|
||||
func (c *cookie) Close(ctx context.Context) error {
|
||||
if c.poolId != "" && c.drv.pool != nil {
|
||||
defer c.drv.pool.FreePoolId(c.poolId)
|
||||
}
|
||||
return c.drv.removeContainer(ctx, c.id)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user