-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbatch_session_opts.go
More file actions
51 lines (43 loc) · 1.22 KB
/
batch_session_opts.go
File metadata and controls
51 lines (43 loc) · 1.22 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
package arksdk
import (
"fmt"
)
const maxRetryNum = 5
type BatchSessionOption func(options *batchSessionOptions) error
// ApplyBatchOptions applies the given BatchSessionOption functions to a new default
// batchSessionOptions struct and returns the first error encountered, if any.
// Exposed for use in external (arksdk_test) test packages.
func ApplyBatchSessionOptions(opts ...BatchSessionOption) error {
_, err := applyBatchSessionOptions(opts...)
return err
}
func WithRetries(num int) BatchSessionOption {
return func(o *batchSessionOptions) error {
if o.retryNum > 0 {
return fmt.Errorf("retry num already set")
}
if num <= 0 || num > maxRetryNum {
return fmt.Errorf("retry num must be in range [1, %d]", maxRetryNum)
}
o.retryNum = num
return nil
}
}
func applyBatchSessionOptions(opts ...BatchSessionOption) (*batchSessionOptions, error) {
o := newDefaultBatchSessionOptions()
for _, opt := range opts {
if opt == nil {
return nil, fmt.Errorf("batch session option cannot be nil")
}
if err := opt(o); err != nil {
return nil, err
}
}
return o, nil
}
type batchSessionOptions struct {
retryNum int
}
func newDefaultBatchSessionOptions() *batchSessionOptions {
return &batchSessionOptions{}
}