-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathsync.go
More file actions
71 lines (59 loc) · 1.77 KB
/
sync.go
File metadata and controls
71 lines (59 loc) · 1.77 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package memory
import (
"context"
"time"
"github.com/e2b-dev/infra/packages/api/internal/sandbox"
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
)
// TODO: this should be removed once we have a better way to handle node sync
// Don't remove sandboxes that were started in the grace period on node sync
// This is to prevent remove instances that are still being started
const syncSandboxRemoveGracePeriod = 10 * time.Second
func (s *Storage) Sync(sandboxes []sandbox.Sandbox, nodeID string) []sandbox.Sandbox {
sandboxMap := make(map[string]sandbox.Sandbox)
now := time.Now()
// Use a map for faster lookup
for _, sandbox := range sandboxes {
sandboxMap[sandbox.SandboxID] = sandbox
}
// Remove sandboxes that are not in Orchestrator anymore
s.items.IterCb(func(_ string, item *memorySandbox) {
data := item.Data()
if data.IsExpired(now) {
return
}
if data.NodeID != nodeID {
return
}
if time.Since(data.StartTime) <= syncSandboxRemoveGracePeriod {
return
}
_, found := sandboxMap[data.SandboxID]
if !found {
logger.L().Debug(
context.Background(),
"sync expiring sandbox missing from node report",
logger.WithSandboxID(data.SandboxID),
logger.WithTeamID(data.TeamID.String()),
logger.WithNodeID(nodeID),
)
item.SetExpired()
}
})
toBeAdded := make([]sandbox.Sandbox, 0, len(sandboxes))
// Add sandboxes that are not in the cache with the default TTL
for _, sandbox := range sandboxes {
if s.exists(sandbox.SandboxID) {
continue
}
logger.L().Debug(
context.Background(),
"sync discovered sandbox missing from cache",
logger.WithSandboxID(sandbox.SandboxID),
logger.WithTeamID(sandbox.TeamID.String()),
logger.WithNodeID(nodeID),
)
toBeAdded = append(toBeAdded, sandbox)
}
return toBeAdded
}