-
Notifications
You must be signed in to change notification settings - Fork 7
/
internal.go
executable file
·52 lines (47 loc) · 1.08 KB
/
internal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package internal
import (
"errors"
"fmt"
"runtime"
"runtime/debug"
)
// ComputeNofBatches divides the size of the range (high - low) by n. If n is 0,
// a default is used that takes runtime.GOMAXPROCS(0) into account.
func ComputeNofBatches(low, high, n int) (batches int) {
switch size := high - low; {
case size > 0:
switch {
case n == 0:
batches = 2 * runtime.GOMAXPROCS(0)
case n > 0:
batches = n
default:
panic(fmt.Sprintf("invalid number of batches: %v", n))
}
if batches > size {
batches = size
}
case size == 0:
batches = 1
default:
panic(fmt.Sprintf("invalid range: %v:%v", low, high))
}
return
}
type runtimeError struct{ error }
func (runtimeError) RuntimeError() {}
// WrapPanic adds stack trace information to a recovered panic.
func WrapPanic(p interface{}) interface{} {
if p != nil {
s := fmt.Sprintf("%v\n%s\nrethrown at", p, debug.Stack())
if _, isError := p.(error); isError {
r := errors.New(s)
if _, isRuntimeError := p.(runtime.Error); isRuntimeError {
return runtimeError{r}
}
return r
}
return s
}
return nil
}