-
Notifications
You must be signed in to change notification settings - Fork 18
/
migrator_builder.go
116 lines (99 loc) · 3.61 KB
/
migrator_builder.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ldclient
import (
"errors"
"github.com/launchdarkly/go-sdk-common/v3/ldmigration"
"github.com/launchdarkly/go-sdk-common/v3/ldsampling"
)
// MigratorBuilder provides a mechanism to construct a Migrator instance.
type MigratorBuilder struct {
client MigrationCapableClient
readExecutionOrder ldmigration.ExecutionOrder
measureLatency bool
measureErrors bool
readConfig *migrationConfig
writeConfig *migrationConfig
}
// Migration creates a new MigratorBuilder instance with sane defaults.
//
// The builder defaults to tracking latency and error metrics, and will execute
// multiple migration-reads concurrently when possible.
func Migration(client MigrationCapableClient) *MigratorBuilder {
return &MigratorBuilder{
client: client,
measureLatency: true,
measureErrors: true,
readExecutionOrder: ldmigration.Concurrent,
}
}
// ReadExecutionOrder influences the level of concurrency when the migration stage calls for multiple execution reads.
func (b *MigratorBuilder) ReadExecutionOrder(order ldmigration.ExecutionOrder) *MigratorBuilder {
b.readExecutionOrder = order
return b
}
// TrackLatency can be used to enable or disable latency tracking methods. Tracking is enabled by default.
func (b *MigratorBuilder) TrackLatency(enabled bool) *MigratorBuilder {
b.measureLatency = enabled
return b
}
// TrackErrors can be used to enable or disable error tracking. Tracking is enabled by default.
func (b *MigratorBuilder) TrackErrors(enabled bool) *MigratorBuilder {
b.measureErrors = enabled
return b
}
// Read can be used to configure the migration-read behavior of the resulting Migrator instance.
//
// Users are required to provide two different read methods -- one to read from the old migration source, and one to
// read from the new source. Additionally, customers can opt-in to consistency tracking by providing a comparison
// function.
//
// Depending on the migration stage, one or both of these read methods may be called.
func (b *MigratorBuilder) Read(
oldReadFn, newReadFn MigrationImplFn,
comparisonFn *MigrationComparisonFn,
) *MigratorBuilder {
b.readConfig = &migrationConfig{
old: oldReadFn,
new: newReadFn,
compare: comparisonFn,
}
return b
}
// Write can be used to configure the migration-write behavior of the resulting Migrator instance.
//
// Users are required to provide two different write methods -- one to write to the old migration source, and one to
// write to the new source. Not every stage requires
//
// Depending on the migration stage, one or both of these write methods may be called.
func (b *MigratorBuilder) Write(oldWriteFn, newWriteFn MigrationImplFn) *MigratorBuilder {
b.writeConfig = &migrationConfig{
old: oldWriteFn,
new: newWriteFn,
}
return b
}
// Build constructs a Migrator instance to support migration-based reads and writes. An error will be returned if the
// build process fails.
func (b *MigratorBuilder) Build() (Migrator, error) {
if b == nil {
return nil, errors.New("calling build on nil pointer")
}
if b.client == nil {
return nil, errors.New("a valid client was not provided")
}
if b.readConfig == nil {
return nil, errors.New("no read configuration has been provided")
}
if b.writeConfig == nil {
return nil, errors.New("no write configuration has been provided")
}
migrator := migratorImpl{
client: b.client,
readExecutionOrder: b.readExecutionOrder,
readConfig: *b.readConfig,
writeConfig: *b.writeConfig,
measureLatency: b.measureLatency,
measureErrors: b.measureErrors,
sampler: ldsampling.NewSampler(),
}
return &migrator, nil
}