Skip to content

Commit d9a9a4d

Browse files
committed
errdefs: mark ENOMEM & ENOSPC with ResourceExhausted code
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 7b2c453 commit d9a9a4d

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

errdefs/internal.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,24 @@ func IsInternal(err error) bool {
3636

3737
var errno syscall.Errno
3838
if errors.As(err, &errno) {
39-
if isInternalSyscall(errno) {
39+
if _, ok := isInternalSyscall(errno); ok {
4040
return true
4141
}
4242
}
4343
return false
4444
}
4545

46-
func isInternalSyscall(err syscall.Errno) bool {
47-
_, ok := syscallErrors()[err]
48-
return ok
46+
func IsResourceExhausted(err error) bool {
47+
var errno syscall.Errno
48+
if errors.As(err, &errno) {
49+
if v, ok := isInternalSyscall(errno); ok && v {
50+
return v
51+
}
52+
}
53+
return false
54+
}
55+
56+
func isInternalSyscall(err syscall.Errno) (bool, bool) {
57+
v, ok := syscallErrors()[err]
58+
return v, ok
4959
}

errdefs/internal_linux.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
"golang.org/x/sys/unix"
1010
)
1111

12-
func syscallErrors() map[syscall.Errno]struct{} {
13-
return map[syscall.Errno]struct{}{
14-
unix.EIO: {}, // I/O error
15-
unix.ENOMEM: {}, // Out of memory
16-
unix.EFAULT: {}, // Bad address
17-
unix.ENOSPC: {}, // No space left on device
18-
unix.ENOTRECOVERABLE: {}, // State not recoverable
19-
unix.EHWPOISON: {}, // Memory page has hardware error
12+
// syscallErrors returns a map of syscall errors that are considered internal.
13+
// value is true if the error is of type resource exhaustion, false otherwise.
14+
func syscallErrors() map[syscall.Errno]bool {
15+
return map[syscall.Errno]bool{
16+
unix.EIO: false, // I/O error
17+
unix.ENOMEM: true, // Out of memory
18+
unix.EFAULT: false, // Bad address
19+
unix.ENOSPC: true, // No space left on device
20+
unix.ENOTRECOVERABLE: false, // State not recoverable
21+
unix.EHWPOISON: false, // Memory page has hardware error
2022
}
2123
}

errdefs/internal_nolinux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ package errdefs
55

66
import "syscall"
77

8-
func syscallErrors() map[syscall.Errno]struct{} {
8+
func syscallErrors() map[syscall.Errno]bool {
99
return nil
1010
}

util/grpcerrors/grpcerrors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func withDetails(ctx context.Context, s *status.Status, details ...proto.Message
9696

9797
func Code(err error) codes.Code {
9898
if errdefs.IsInternal(err) {
99+
if errdefs.IsResourceExhausted(err) {
100+
return codes.ResourceExhausted
101+
}
99102
return codes.Internal
100103
}
101104

0 commit comments

Comments
 (0)