-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpool.go
42 lines (35 loc) · 912 Bytes
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"context"
"time"
log "github.com/sirupsen/logrus"
)
func fillVMPool(ctx context.Context, WarmVMs chan<- runningFirecracker) {
for {
select {
case <-ctx.Done():
// Program is stopping, WarmVMs will be cleaned up, bye
return
default:
vm, err := createAndStartVM(ctx)
if err != nil {
log.Error("failed to create VMM")
time.Sleep(time.Second)
continue
}
log.WithField("ip", vm.ip).Info("New VM created and started")
// Don't wait forever, if the VM is not available after 10s, move on
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
err = waitForVMToBoot(ctx, vm.ip)
if err != nil {
log.WithError(err).Info("VM not ready yet")
vm.vmmCancel()
continue
}
// Add the new microVM to the pool.
// If the pool is full, this line will block until a slot is available.
WarmVMs <- *vm
}
}
}