-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
64 lines (55 loc) · 1.48 KB
/
Copy pathoptions.go
File metadata and controls
64 lines (55 loc) · 1.48 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
package blockdev
import "time"
// Option is the functional-option type. Variadic in New keeps the constructor
// extensible without forcing a breaking change every time a knob is added.
type Option func(*config)
type config struct {
name string
observer func(Event)
}
// WithName tags the device so a single observer can distinguish events from
// many devices (one per agent in the demo).
func WithName(name string) Option {
return func(c *config) { c.name = name }
}
// WithObserver installs a synchronous callback invoked after every public
// operation. Panics from the callback are recovered so user instrumentation
// cannot crash the I/O path.
func WithObserver(fn func(Event)) Option {
return func(c *config) { c.observer = fn }
}
// Op identifies the operation that produced an Event.
type Op uint8
const (
OpRead Op = iota + 1
OpWrite
OpSerialize
OpDeserialize
)
func (o Op) String() string {
switch o {
case OpRead:
return "read"
case OpWrite:
return "write"
case OpSerialize:
return "serialize"
case OpDeserialize:
return "deserialize"
default:
return "unknown"
}
}
// Event is the per-operation record passed to WithObserver's callback.
// Length and Blocks have op-specific meaning: for Read/Write they describe
// the request; for Serialize/Deserialize they describe the blob size and the
// number of changed-block entries.
type Event struct {
Op Op
Device string
Offset int64
Length int
Blocks int
Duration time.Duration
Err error
}