-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_self_clearing.go
46 lines (36 loc) · 1 KB
/
manager_self_clearing.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
43
44
45
46
package jasper
import (
"context"
"errors"
"github.com/tychoish/jasper/options"
)
type selfClearingProcessManager struct {
*basicProcessManager
maxProcs int
}
func (m *selfClearingProcessManager) checkProcCapacity(ctx context.Context) error {
if len(m.basicProcessManager.procs) == m.maxProcs {
// We are at capacity, we can try to perform a clear.
m.Clear(ctx)
if len(m.basicProcessManager.procs) == m.maxProcs {
return errors.New("cannot create any more processes")
}
}
return nil
}
func (m *selfClearingProcessManager) CreateProcess(ctx context.Context, opts *options.Create) (Process, error) {
if err := m.checkProcCapacity(ctx); err != nil {
return nil, err
}
proc, err := m.basicProcessManager.CreateProcess(ctx, opts)
if err != nil {
return nil, err
}
return proc, nil
}
func (m *selfClearingProcessManager) Register(ctx context.Context, proc Process) error {
if err := m.checkProcCapacity(ctx); err != nil {
return err
}
return m.basicProcessManager.Register(ctx, proc)
}