-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathoptions.go
73 lines (65 loc) · 2.26 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
package extension
import (
"context"
"log/slog"
)
// options represents configuration options for standard connectors.
type options struct {
logger *slog.Logger
ctx context.Context
ctxCancel context.CancelFunc
retryFunc func(context.Context, func() error) error
}
// makeDefaultOptions returns an options with default values.
func makeDefaultOptions() options {
return options{
logger: slog.Default(),
ctx: context.Background(),
ctxCancel: func() {},
retryFunc: func(_ context.Context, f func() error) error { return f() },
}
}
// Opt is a functional option type used to configure an options.
type Opt func(*options)
// WithLogger configures the options with a custom logger.
// If not specified, the default slog.Default() will be used.
func WithLogger(logger *slog.Logger) Opt {
return func(o *options) {
o.logger = logger
}
}
// WithContext configures the options with a context.
// Can be customized in source connectors to gracefully stop streaming data when
// cancelled. For sink connectors, this allows for configuring retry behavior.
//
// Sink connectors can use WithContextCancel to trigger cancellation of the source
// context upon encountering fatal errors.
func WithContext(ctx context.Context) Opt {
return func(o *options) {
o.ctx = ctx
}
}
// WithContextCancel configures the options with a context cancellation function.
// This function should be called when a fatal error occurs in the connector, allowing
// for proper cleanup.
//
// Primarily intended for sink connectors to signal to source connectors that they
// should stop producing data.
// Source connectors should generally ignore this option.
func WithContextCancel(ctxCancel context.CancelFunc) Opt {
return func(o *options) {
o.ctxCancel = ctxCancel
}
}
// WithRetryFunc configures the options with a custom retry function.
// This function will be used to retry operations that fail.
// If not specified, the default implementation will execute the function once
// without retries.
//
// The retry function receives the context and the function to be retried.
// It should handle the retry logic, respecting the context's cancellation signal.
func WithRetryFunc(retryFunc func(context.Context, func() error) error) Opt {
return func(o *options) {
o.retryFunc = retryFunc
}
}