Path: blob/main/components/ws-daemon/pkg/cgroup/plugin_proc_limit_v2.go
2499 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cgroup56import (7"context"8"path/filepath"9"sync"1011v2 "github.com/containerd/cgroups/v2"12"github.com/gitpod-io/gitpod/common-go/log"13)1415type ProcLimiterV2 struct {16limits *v2.Resources1718cond *sync.Cond19}2021func NewProcLimiterV2(processes int64) (*ProcLimiterV2, error) {22return &ProcLimiterV2{23limits: &v2.Resources{24Pids: &v2.Pids{25Max: processes,26},27},2829cond: sync.NewCond(&sync.Mutex{}),30}, nil31}3233func (c *ProcLimiterV2) Name() string { return "proc-limiter-v2" }34func (c *ProcLimiterV2) Type() Version { return Version2 }3536func (c *ProcLimiterV2) Apply(ctx context.Context, opts *PluginOptions) error {37update := make(chan struct{}, 1)38go func() {39defer close(update)4041for {42c.cond.L.Lock()43c.cond.Wait()44c.cond.L.Unlock()4546if ctx.Err() != nil {47return48}4950update <- struct{}{}51}52}()5354go func() {55log.WithField("cgroupPath", opts.CgroupPath).Debug("starting proc limiting")5657_, err := v2.NewManager(opts.BasePath, filepath.Join("/", opts.CgroupPath), c.limits)58if err != nil {59log.WithError(err).WithFields(log.OWI("", "", opts.InstanceId)).WithField("basePath", opts.BasePath).WithField("cgroupPath", opts.CgroupPath).WithField("limits", c.limits).Error("cannot write proc limits")60}6162for {63select {64case <-update:65_, err := v2.NewManager(opts.BasePath, filepath.Join("/", opts.CgroupPath), c.limits)66if err != nil {67log.WithError(err).WithFields(log.OWI("", "", opts.InstanceId)).WithField("basePath", opts.BasePath).WithField("cgroupPath", opts.CgroupPath).WithField("limits", c.limits).Error("cannot write proc limits")68}69case <-ctx.Done():70return71}72}73}()7475return nil76}7778func (c *ProcLimiterV2) Update(processes int64) {79c.cond.L.Lock()80defer c.cond.L.Unlock()8182c.limits = &v2.Resources{83Pids: &v2.Pids{84Max: processes,85},86}8788log.WithField("limits", c.limits.Pids).Info("updating proc cgroups v2 limits")89c.cond.Broadcast()90}919293