@@ -14,18 +14,13 @@ import (
1414
1515 "github.com/fsnotify/fsnotify"
1616 "go.uber.org/zap"
17- "golang.org/x/sync/semaphore"
1817
1918 "github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox"
20- featureflags "github.com/e2b-dev/infra/packages/shared/pkg/feature-flags"
2119 "github.com/e2b-dev/infra/packages/shared/pkg/smap"
22- "github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
2320)
2421
2522type Tracker struct {
26- featureFlags * featureflags.Client
27- watcher * fsnotify.Watcher
28- startingSandboxes * semaphore.Weighted
23+ watcher * fsnotify.Watcher
2924
3025 selfPath string
3126 selfSandboxResources * smap.Map [sandbox.Config ]
@@ -42,7 +37,7 @@ func (t *Tracker) OnRemove(sandboxID string) {
4237 t .selfSandboxResources .Remove (sandboxID )
4338}
4439
45- func NewTracker (maxStartingInstancesPerNode int64 , directory string , selfWriteInterval time.Duration , featureFlags * featureflags. Client ) (* Tracker , error ) {
40+ func NewTracker (directory string , selfWriteInterval time.Duration ) (* Tracker , error ) {
4641 filename := fmt .Sprintf ("%d.json" , os .Getpid ())
4742 selfPath := filepath .Join (directory , filename )
4843
@@ -59,17 +54,27 @@ func NewTracker(maxStartingInstancesPerNode int64, directory string, selfWriteIn
5954 }
6055
6156 return & Tracker {
62- featureFlags : featureFlags ,
6357 watcher : watcher ,
6458 otherMetrics : map [int ]Allocations {},
6559
6660 selfPath : selfPath ,
6761 selfWriteInterval : selfWriteInterval ,
6862 selfSandboxResources : smap .New [sandbox.Config ](),
69- startingSandboxes : semaphore .NewWeighted (maxStartingInstancesPerNode ),
7063 }, nil
7164}
7265
66+ func (t * Tracker ) TotalRunningCount () int {
67+ count := t .selfSandboxResources .Count ()
68+
69+ t .otherLock .RLock ()
70+ for _ , item := range t .otherMetrics {
71+ count += int (item .Sandboxes )
72+ }
73+ t .otherLock .RUnlock ()
74+
75+ return count
76+ }
77+
7378func (t * Tracker ) getSelfAllocated () Allocations {
7479 var allocated Allocations
7580 for _ , item := range t .selfSandboxResources .Items () {
@@ -185,55 +190,6 @@ func (t *Tracker) handleOtherWrite(name string) error {
185190 return nil
186191}
187192
188- var ErrTooManyStarting = errors .New ("too many starting sandboxes" )
189-
190- type TooManySandboxesRunningError struct {
191- Current , Max int
192- }
193-
194- func (t TooManySandboxesRunningError ) Error () string {
195- return fmt .Sprintf ("max number of running sandboxes on node reached (%d), please retry" , t .Max )
196- }
197-
198- var _ error = TooManySandboxesRunningError {}
199-
200- type TooManySandboxesStartingError struct {
201- Current , Max int
202- }
203-
204- var _ error = TooManySandboxesStartingError {}
205-
206- func (t TooManySandboxesStartingError ) Error () string {
207- return fmt .Sprintf ("max number of starting sandboxes on node reached (%d), please retry" , t .Max )
208- }
209-
210- func (t * Tracker ) AcquireStarting (ctx context.Context ) error {
211- maxRunningSandboxesPerNode , err := t .featureFlags .IntFlag (ctx , featureflags .MaxSandboxesPerNode )
212- if err != nil {
213- zap .L ().Error ("Failed to get MaxSandboxesPerNode flag" , zap .Error (err ))
214- }
215-
216- runningSandboxes := t .selfSandboxResources .Count ()
217- if runningSandboxes >= maxRunningSandboxesPerNode {
218- telemetry .ReportEvent (ctx , "max number of running sandboxes reached" )
219-
220- return TooManySandboxesRunningError {runningSandboxes , maxRunningSandboxesPerNode }
221- }
222-
223- // Check if we've reached the max number of starting instances on this node
224- acquired := t .startingSandboxes .TryAcquire (1 )
225- if ! acquired {
226- telemetry .ReportEvent (ctx , "too many starting sandboxes on node" )
227- return ErrTooManyStarting
228- }
229-
230- return nil
231- }
232-
233- func (t * Tracker ) ReleaseStarting () {
234- defer t .startingSandboxes .Release (1 )
235- }
236-
237193type Allocations struct {
238194 DiskBytes uint64 `json:"disk_bytes"`
239195 MemoryBytes uint64 `json:"memory_bytes"`
0 commit comments