Skip to content

Commit 47cfaa9

Browse files
committed
connection: export timeout and metrics options
If they are set, they will override the default values that are set when calling `Connect`.
1 parent c487210 commit 47cfaa9

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

connection/connection.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func SetMaxGRPCLogLength(characterCount int) {
5858
// The function tries to connect for 30 seconds, and returns an error if no connection has been established at that point.
5959
// The function automatically disables TLS and adds interceptor for logging of all gRPC messages at level 5.
6060
// If the metricsManager is 'nil', no metrics will be recorded on the gRPC calls.
61+
// The function behaviour can be tweaked with options.
6162
//
6263
// For a connection to a Unix Domain socket, the behavior after
6364
// loosing the connection is configurable. The default is to
@@ -72,9 +73,10 @@ func SetMaxGRPCLogLength(characterCount int) {
7273
// For other connections, the default behavior from gRPC is used and
7374
// loss of connection is not detected reliably.
7475
func Connect(address string, metricsManager metrics.CSIMetricsManager, options ...Option) (*grpc.ClientConn, error) {
75-
options = append(options, withTimeout(time.Second*30))
76+
// Prepend default options
77+
options = append([]Option{WithTimeout(time.Second * 30)}, options...)
7678
if metricsManager != nil {
77-
options = append(options, withMetrics(metricsManager))
79+
options = append([]Option{WithMetrics(metricsManager)}, options...)
7880
}
7981
return connect(address, options)
8082
}
@@ -114,15 +116,15 @@ func ExitOnConnectionLoss() func() bool {
114116
}
115117
}
116118

117-
// withTimeout adds a configurable timeout on the gRPC calls.
118-
func withTimeout(timeout time.Duration) Option {
119+
// WithTimeout adds a configurable timeout on the gRPC calls.
120+
func WithTimeout(timeout time.Duration) Option {
119121
return func(o *options) {
120122
o.timeout = timeout
121123
}
122124
}
123125

124-
// withMetrics enables the recording of metrics on the gRPC calls with the provided CSIMetricsManager.
125-
func withMetrics(metricsManager metrics.CSIMetricsManager) Option {
126+
// WithMetrics enables the recording of metrics on the gRPC calls with the provided CSIMetricsManager.
127+
func WithMetrics(metricsManager metrics.CSIMetricsManager) Option {
126128
return func(o *options) {
127129
o.metricsManager = metricsManager
128130
}

connection/connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestTimeout(t *testing.T) {
223223

224224
startTime := time.Now()
225225
timeout := 5 * time.Second
226-
conn, err := connect(path.Join(tmp, "no-such.sock"), []Option{withTimeout(timeout)})
226+
conn, err := connect(path.Join(tmp, "no-such.sock"), []Option{WithTimeout(timeout)})
227227
endTime := time.Now()
228228
if assert.Error(t, err, "connection should fail") {
229229
assert.InEpsilon(t, timeout, endTime.Sub(startTime), 1, "connection timeout")

0 commit comments

Comments
 (0)