| 35 | } |
| 36 | |
| 37 | func NewVMPool(baseVM *VM, config *VMPoolConfig) *VMPool { |
| 38 | validateVMPoolConfig(config) |
| 39 | p := &VMPool{ |
| 40 | base: baseVM, |
| 41 | config: *config, |
| 42 | idle: make([]idleVM, 0, config.MaxIdle), |
| 43 | changed: make(chan struct{}), |
| 44 | stop: make(chan struct{}), |
| 45 | } |
| 46 | |
| 47 | now := time.Now() |
| 48 | for range config.MinIdle { |
| 49 | p.idle = append(p.idle, idleVM{vm: baseVM.Fork(), returnedAt: now}) |
| 50 | p.total++ |
| 51 | } |
| 52 | if config.IdleTime > 0 { |
| 53 | p.cleaner.Add(1) |
| 54 | go p.runCleaner(cleanPeriod(config.IdleTime)) |
| 55 | } |
| 56 | return p |
| 57 | } |
| 58 | |
| 59 | func validateVMPoolConfig(config *VMPoolConfig) { |
| 60 | if config == nil { |