Skip to content

Commit

Permalink
avoid spawning thousands of process
Browse files Browse the repository at this point in the history
when starting or creating containers in
environments which has thousands of
lines in /proc/mounts

https://work.aone.alibaba-inc.com/issue/22582695
Signed-off-by: shenling.yyb <[email protected]>
  • Loading branch information
shenling.yyb authored and zhuangqh committed Oct 22, 2019
1 parent a3e9d99 commit 0dc1e86
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
26 changes: 21 additions & 5 deletions storage/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package quota

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -341,11 +342,26 @@ func checkDevLimit(mountInfo *MountInfo, size uint64) error {
}

func getDevID(dir string) (uint64, error) {
// call "stat <dir>" to ensure stat not timeout
_, _, _, err := exec.Run(time.Second*5, "stat", dir)
if err != nil {
// ensure stat syscall don't timeout
idChan := make(chan uint64)
errChan := make(chan error)
timeoutChan := time.After(time.Second * 5)

go func() {
id, err := system.GetDevID(dir)
if err != nil {
errChan <- err
return
}
idChan <- id
}()

select {
case err := <-errChan:
return 0, err
case id := <-idChan:
return id, nil
case <-timeoutChan:
return 0, context.DeadlineExceeded
}

return system.GetDevID(dir)
}
30 changes: 30 additions & 0 deletions storage/quota/quota_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// +build linux

package quota

import (
"os"
"testing"

"github.com/alibaba/pouch/pkg/system"
)

func Test_getDevID(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("get work directory error %v", err)
}
expectID, err := system.GetDevID(wd)
if err != nil {
t.Fatalf("get dev id error of %s: %v", wd, err)
}

gotID, err := getDevID(wd)
if err != nil {
t.Fatalf("get dev id error of %s by getDevID: %v", wd, err)
}

if expectID != gotID {
t.Fatalf("getDevID error expect %d got %d", expectID, gotID)
}
}

0 comments on commit 0dc1e86

Please sign in to comment.