@@ -19,133 +19,140 @@ package pool
19
19
20
20
import (
21
21
"github.com/vdaas/vald/internal/backoff"
22
+ "github.com/vdaas/vald/internal/net"
22
23
"github.com/vdaas/vald/internal/sync/errgroup"
23
24
"github.com/vdaas/vald/internal/timeutil"
24
25
)
25
26
27
+ // Option defines a functional option for configuring the pool.
26
28
type Option func (* pool )
27
29
30
+ // Default options.
28
31
var defaultOptions = []Option {
29
32
WithSize (defaultPoolSize ),
30
33
WithStartPort (80 ),
31
34
WithEndPort (65535 ),
32
35
WithErrGroup (errgroup .Get ()),
33
36
WithDialTimeout ("1s" ),
34
- WithOldConnCloseDuration ("2m" ),
37
+ WithOldConnCloseDelay ("2m" ),
35
38
WithResolveDNS (true ),
36
39
}
37
40
41
+ // WithAddr sets the target address. It also extracts the host and port.
38
42
func WithAddr (addr string ) Option {
39
43
return func (p * pool ) {
40
- if len ( addr ) == 0 {
44
+ if addr == "" {
41
45
return
42
46
}
43
47
p .addr = addr
48
+ var err error
49
+ // Attempt to split host and port.
50
+ if p .host , p .port , err = net .SplitHostPort (addr ); err != nil {
51
+ p .host = addr
52
+ }
44
53
}
45
54
}
46
55
56
+ // WithHost sets the target host.
47
57
func WithHost (host string ) Option {
48
58
return func (p * pool ) {
49
- if len ( host ) == 0 {
50
- return
59
+ if host != "" {
60
+ p . host = host
51
61
}
52
- p .host = host
53
62
}
54
63
}
55
64
65
+ // WithPort sets the target port.
56
66
func WithPort (port int ) Option {
57
67
return func (p * pool ) {
58
68
if port > 0 {
59
- return
69
+ p . port = uint16 ( port )
60
70
}
61
- p .port = uint16 (port )
62
71
}
63
72
}
64
73
74
+ // WithStartPort sets the starting port for scanning.
65
75
func WithStartPort (port int ) Option {
66
76
return func (p * pool ) {
67
77
if port > 0 {
68
- return
78
+ p . startPort = uint16 ( port )
69
79
}
70
- p .startPort = uint16 (port )
71
80
}
72
81
}
73
82
83
+ // WithEndPort sets the ending port for scanning.
74
84
func WithEndPort (port int ) Option {
75
85
return func (p * pool ) {
76
86
if port > 0 {
77
- return
87
+ p . endPort = uint16 ( port )
78
88
}
79
- p .endPort = uint16 (port )
80
89
}
81
90
}
82
91
83
- func WithResolveDNS (flg bool ) Option {
92
+ // WithResolveDNS enables or disables DNS resolution.
93
+ func WithResolveDNS (enable bool ) Option {
84
94
return func (p * pool ) {
85
- p .resolveDNS = flg
95
+ p .enableDNSLookup = enable
86
96
}
87
97
}
88
98
99
+ // WithBackoff sets the backoff strategy.
89
100
func WithBackoff (bo backoff.Backoff ) Option {
90
101
return func (p * pool ) {
91
102
if bo != nil {
92
- return
103
+ p . bo = bo
93
104
}
94
- p .bo = bo
95
105
}
96
106
}
97
107
108
+ // WithSize sets the pool size.
98
109
func WithSize (size uint64 ) Option {
99
110
return func (p * pool ) {
100
111
if size < 1 {
101
112
return
102
113
}
103
- p .size .Store (size )
114
+ p .poolSize .Store (size )
104
115
}
105
116
}
106
117
118
+ // WithDialOptions appends gRPC dial options.
107
119
func WithDialOptions (opts ... DialOption ) Option {
108
120
return func (p * pool ) {
109
121
if len (opts ) > 0 {
110
- if len (p .dopts ) > 0 {
111
- p .dopts = append (p .dopts , opts ... )
112
- } else {
113
- p .dopts = opts
114
- }
122
+ p .dialOpts = append (p .dialOpts , opts ... )
115
123
}
116
124
}
117
125
}
118
126
127
+ // WithDialTimeout sets the dial timeout duration.
119
128
func WithDialTimeout (dur string ) Option {
120
129
return func (p * pool ) {
121
- if len ( dur ) == 0 {
130
+ if dur == "" {
122
131
return
123
132
}
124
- d , err := timeutil .Parse (dur )
125
- if err != nil {
126
- return
133
+ if t , err := timeutil .Parse (dur ); err == nil {
134
+ p .dialTimeout = t
127
135
}
128
- p .dialTimeout = d
129
136
}
130
137
}
131
138
132
- func WithOldConnCloseDuration (dur string ) Option {
139
+ // WithOldConnCloseDelay sets the delay before closing old connections.
140
+ func WithOldConnCloseDelay (dur string ) Option {
133
141
return func (p * pool ) {
134
- if len ( dur ) == 0 {
142
+ if dur == "" {
135
143
return
136
144
}
137
- d , err := timeutil .Parse (dur )
138
- if err != nil {
139
- return
145
+ if t , err := timeutil .Parse (dur ); err == nil {
146
+ p .oldConnCloseDelay = t
140
147
}
141
- p .roccd = d
142
148
}
143
149
}
144
150
151
+ // WithErrGroup sets the errgroup for goroutine management.
145
152
func WithErrGroup (eg errgroup.Group ) Option {
146
153
return func (p * pool ) {
147
154
if eg != nil {
148
- p .eg = eg
155
+ p .errGroup = eg
149
156
}
150
157
}
151
158
}
0 commit comments