-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
79 lines (68 loc) · 1.54 KB
/
options.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package icmp
import (
"math"
"time"
)
// Options options
type Options struct {
count int
size int
Log func(string, ...interface{})
// Timeout (-W) 参数用于设置单个ICMP回显请求等待回应的超时时间,单位通常也是秒。
// 如果在指定的时间内没有收到回应,该ICMP请求就会被认为是丢失的。
// 例如:ping -W 2 192.168.1.1
// 这个命令将对192.168.1.1进行ping操作,每次发送请求后,如果2秒内没有收到回应,则该请求超时。
Timeout time.Duration // send packet timeout
wait time.Duration // wait between sending each packet
}
// Option Options function
type Option func(*Options)
func newOptions(opts []Option, extends ...Option) Options {
opt := Options{
size: 56,
count: 4,
Log: func(string, ...interface{}) {},
Timeout: 1 * time.Second,
wait: 1 * time.Second,
}
for _, o := range opts {
o(&opt)
}
for _, o := range extends {
o(&opt)
}
return opt
}
// Size set size
func Size(size int) Option {
return func(o *Options) {
o.size = size
}
}
// Wait seconds between sending each packet.
func Wait(wait time.Duration) Option {
return func(o *Options) {
o.wait = wait
}
}
// Timeout set timeout
func Timeout(timeout time.Duration) Option {
return func(o *Options) {
o.Timeout = timeout
}
}
// Count count
func Count(count int) Option {
return func(o *Options) {
if count == 0 {
count = math.MaxInt64
}
o.count = count
}
}
// Log ..
func Log(f func(string, ...any)) Option {
return func(o *Options) {
o.Log = f
}
}