all: Modernize error wrapping (#8491)
This replaces old style errors.Wrap with modern fmt.Errorf and removes the (direct) dependency on github.com/pkg/errors. A couple of cases are adjusted by hand as previously errors.Wrap(nil, ...) would return nil, which is not what fmt.Errorf does.
This commit is contained in:
@@ -10,10 +10,9 @@
|
||||
package osutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const ioprioClassShift = 13
|
||||
@@ -82,20 +81,22 @@ func SetLowPriority() error {
|
||||
// so we need this workaround...
|
||||
if pgid, err := syscall.Getpgid(pidSelf); err != nil {
|
||||
// This error really shouldn't happen
|
||||
return errors.Wrap(err, "get process group")
|
||||
return fmt.Errorf("get process group: %w", err)
|
||||
} else if pgid != os.Getpid() {
|
||||
// We are not process group leader. Elevate!
|
||||
if err := syscall.Setpgid(pidSelf, 0); err != nil {
|
||||
return errors.Wrap(err, "set process group")
|
||||
return fmt.Errorf("set process group: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := syscall.Setpriority(syscall.PRIO_PGRP, pidSelf, wantNiceLevel); err != nil {
|
||||
return errors.Wrap(err, "set niceness")
|
||||
return fmt.Errorf("set niceness: %w", err)
|
||||
}
|
||||
|
||||
// Best effort, somewhere to the end of the scale (0 through 7 being the
|
||||
// range).
|
||||
err := ioprioSet(ioprioClassBE, 5)
|
||||
return errors.Wrap(err, "set I/O priority") // wraps nil as nil
|
||||
if err := ioprioSet(ioprioClassBE, 5); err != nil {
|
||||
return fmt.Errorf("set I/O priority: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,9 +10,8 @@
|
||||
package osutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// SetLowPriority lowers the process CPU scheduling priority, and possibly
|
||||
@@ -30,6 +29,8 @@ func SetLowPriority() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := syscall.Setpriority(syscall.PRIO_PROCESS, pidSelf, wantNiceLevel)
|
||||
return errors.Wrap(err, "set niceness") // wraps nil as nil
|
||||
if err := syscall.Setpriority(syscall.PRIO_PROCESS, pidSelf, wantNiceLevel); err != nil {
|
||||
return fmt.Errorf("set niceness: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
package osutil
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
@@ -16,10 +17,12 @@ import (
|
||||
func SetLowPriority() error {
|
||||
handle, err := windows.GetCurrentProcess()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "get process handle")
|
||||
return fmt.Errorf("get process handle: %w", err)
|
||||
}
|
||||
defer windows.CloseHandle(handle)
|
||||
|
||||
err = windows.SetPriorityClass(handle, windows.BELOW_NORMAL_PRIORITY_CLASS)
|
||||
return errors.Wrap(err, "set priority class") // wraps nil as nil
|
||||
if err := windows.SetPriorityClass(handle, windows.BELOW_NORMAL_PRIORITY_CLASS); err != nil {
|
||||
return fmt.Errorf("set priority class: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user