-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrunnable.go
More file actions
31 lines (27 loc) · 787 Bytes
/
runnable.go
File metadata and controls
31 lines (27 loc) · 787 Bytes
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
package runnable
import (
"context"
"reflect"
"runtime"
)
// Runnable is the contract for anything that runs with a Go context, respects the cancellation contract,
// and expects the caller to handle errors.
type Runnable interface {
Run(context.Context) error
}
// namer is implemented by wrappers to provide a name for logging.
type namer interface {
runnableName() string
}
// runnableName returns the name of a runnable for logging.
// It checks for the namer interface first, then falls back to reflection.
func runnableName(v any) string {
if n, ok := v.(namer); ok {
return n.runnableName()
}
valueOf := reflect.ValueOf(v)
if valueOf.Kind() == reflect.Func {
return runtime.FuncForPC(valueOf.Pointer()).Name()
}
return reflect.Indirect(valueOf).Type().Name()
}