Skip to content

Commit

Permalink
Merge pull request #4 from moul/dev/moul/is-too-many-error
Browse files Browse the repository at this point in the history
feat: add IsTooManyError helper
  • Loading branch information
moul authored Jul 3, 2020
2 parents f066182 + 7330cf7 commit c82e0c4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
GOPKG ?= moul.io/openfiles

.PHONY: test-archs
test-archs:
GOOS=darwin GOARCH=386 go test -v -c -o /dev/null
GOOS=darwin GOARCH=amd64 go test -v -c -o /dev/null
GOOS=linux GOARCH=386 go test -v -c -o /dev/null
GOOS=linux GOARCH=amd64 go test -v -c -o /dev/null
GOOS=linux GOARCH=arm go test -v -c -o /dev/null
GOOS=linux GOARCH=arm64 go test -v -c -o /dev/null
GOOS=linux GOARCH=ppc64 go test -v -c -o /dev/null
GOOS=linux GOARCH=ppc64le go test -v -c -o /dev/null
GOOS=linux GOARCH=mips go test -v -c -o /dev/null
GOOS=linux GOARCH=mipsle go test -v -c -o /dev/null
GOOS=linux GOARCH=mips64 go test -v -c -o /dev/null
GOOS=linux GOARCH=mips64le go test -v -c -o /dev/null
GOOS=linux GOARCH=s390x go test -v -c -o /dev/null
GOOS=js GOARCH=wasm go test -v -c -o /dev/null
GOOS=dragonfly GOARCH=amd64 go test -v -c -o /dev/null
GOOS=freebsd GOARCH=386 go test -v -c -o /dev/null
GOOS=freebsd GOARCH=amd64 go test -v -c -o /dev/null
GOOS=freebsd GOARCH=arm go test -v -c -o /dev/null
GOOS=netbsd GOARCH=386 go test -v -c -o /dev/null
GOOS=netbsd GOARCH=amd64 go test -v -c -o /dev/null
GOOS=netbsd GOARCH=arm go test -v -c -o /dev/null
GOOS=openbsd GOARCH=386 go test -v -c -o /dev/null
GOOS=openbsd GOARCH=amd64 go test -v -c -o /dev/null
GOOS=openbsd GOARCH=arm go test -v -c -o /dev/null
GOOS=plan9 GOARCH=386 go test -v -c -o /dev/null
GOOS=plan9 GOARCH=amd64 go test -v -c -o /dev/null
GOOS=plan9 GOARCH=arm go test -v -c -o /dev/null
GOOS=solaris GOARCH=amd64 go test -v -c -o /dev/null
GOOS=windows GOARCH=386 go test -v -c -o /dev/null
GOOS=windows GOARCH=amd64 go test -v -c -o /dev/null

include rules.mk
27 changes: 24 additions & 3 deletions openfiles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
package openfiles

import "errors"
import (
"errors"
"os"
"syscall"
)

// ErrNotSupported indicates an unsupported GOOS or GOARCH
var ErrNotSupported = errors.New("not supported environment")
var (
// ErrNotSupported is raised on an unsupported GOOS or GOARCH
ErrNotSupported = errors.New("moul.io/openfiles: not supported environment")

// ErrInternal represents an internal error
ErrInternal = errors.New("moul.io/openfiles: internal error")
)

func IsTooManyError(err error) bool {
if err == syscall.EMFILE {
return true
}

if err, isPathError := err.(*os.PathError); isPathError {
return err.Err == syscall.EMFILE
}

return false
}
22 changes: 19 additions & 3 deletions openfiles_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,30 @@ package openfiles
import (
"fmt"
"os"
"path/filepath"
)

func Count() (int64, error) {
pid := os.Getpid()
m, err := filepath.Glob(fmt.Sprintf("/proc/%d/fd/*", pid))
dir := fmt.Sprintf("/proc/%d/fd", pid)
fi, err := os.Stat(dir)
if err != nil {
return -1, err
}
return int64(len(m)), nil

if !fi.IsDir() {
return -1, ErrInternal
}

d, err := os.Open(dir) // guardrails-disable-line
if err != nil {
return -1, err
}
defer d.Close()

fds, err := d.Readdirnames(-1)
if err != nil {
return -1, err
}

return int64(len(fds)), nil
}
23 changes: 23 additions & 0 deletions openfiles_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package openfiles_test

import (
"errors"
"fmt"
"os"
"syscall"
"testing"

"moul.io/openfiles"
Expand All @@ -19,3 +23,22 @@ func TestCount(t *testing.T) {
}
t.Logf("nofile: %d", nofile)
}

func TestIsTooManyError(t *testing.T) {
cases := []struct {
input error
isTooMany bool
}{
{errors.New("blah"), false},
{errors.New("too many open files"), false},
{syscall.EMFILE, true},
{&os.PathError{Err: syscall.EMFILE}, true},
}

for _, tt := range cases {
k := fmt.Sprintf("%v", tt.input)
t.Run(k, func(t *testing.T) {

})
}
}

0 comments on commit c82e0c4

Please sign in to comment.