go-judge/pkg/pool/cgrouppool.go
criyle 7951f37bd4 Implements the draft executor server
- Add new command executorserver as the draft executor server
- Move shared pool logic into /pkg/pool
2020-03-04 02:12:26 -05:00

58 lines
993 B
Go

package pool
import (
"sync"
"github.com/criyle/go-judge/pkg/envexec"
)
// CgroupPool implements cgroup pool
type CgroupPool struct {
builder CgroupBuilder
cgs []envexec.Cgroup
mu sync.Mutex
}
// NewCgroupPool creates new cgroup pool
func NewCgroupPool(builder CgroupBuilder) *CgroupPool {
return &CgroupPool{builder: builder}
}
// Get gets cgroup from pool, if pool is empty, creates new one
func (w *CgroupPool) Get() (envexec.Cgroup, error) {
w.mu.Lock()
defer w.mu.Unlock()
if len(w.cgs) > 0 {
rt := w.cgs[len(w.cgs)-1]
w.cgs = w.cgs[:len(w.cgs)-1]
return rt, nil
}
cg, err := w.builder.Build()
if err != nil {
return nil, err
}
return (*wCgroup)(cg), nil
}
// Put puts cgroup into the pool
func (w *CgroupPool) Put(c envexec.Cgroup) {
w.mu.Lock()
defer w.mu.Unlock()
c.Reset()
w.cgs = append(w.cgs, c)
}
// Shutdown destroy all cgroup
func (w *CgroupPool) Shutdown() {
w.mu.Lock()
defer w.mu.Unlock()
for _, c := range w.cgs {
c.Destory()
}
}