Skip to content

Commit 0274e83

Browse files
reorder code in apply_changes.go
1 parent ce00d0b commit 0274e83

File tree

1 file changed

+99
-99
lines changed

1 file changed

+99
-99
lines changed

internal/stackitprovider/apply_changes.go

Lines changed: 99 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,105 @@ func (d *StackitDNSProvider) createRRSets(
5151
return d.handleRRSetWithWorkers(ctx, endpoints, zones, CREATE)
5252
}
5353

54+
// updateRRSets patches (overrides) contents in the record sets in the stackitprovider for the given
55+
// endpoints that are in the update new field.
56+
func (d *StackitDNSProvider) updateRRSets(
57+
ctx context.Context,
58+
endpoints []*endpoint.Endpoint,
59+
) error {
60+
if len(endpoints) == 0 {
61+
return nil
62+
}
63+
64+
zones, err := d.zoneFetcherClient.zones(ctx)
65+
if err != nil {
66+
return err
67+
}
68+
69+
return d.handleRRSetWithWorkers(ctx, endpoints, zones, UPDATE)
70+
}
71+
72+
// deleteRRSets deletes record sets in the stackitprovider for the given endpoints that are in the
73+
// deletion field.
74+
func (d *StackitDNSProvider) deleteRRSets(
75+
ctx context.Context,
76+
endpoints []*endpoint.Endpoint,
77+
) error {
78+
if len(endpoints) == 0 {
79+
d.logger.Debug("no endpoints to delete")
80+
81+
return nil
82+
}
83+
84+
d.logger.Info("records to delete", zap.String("records", fmt.Sprintf("%v", endpoints)))
85+
86+
zones, err := d.zoneFetcherClient.zones(ctx)
87+
if err != nil {
88+
return err
89+
}
90+
91+
return d.handleRRSetWithWorkers(ctx, endpoints, zones, DELETE)
92+
}
93+
94+
// handleRRSetWithWorkers handles the given endpoints with workers to optimize speed.
95+
func (d *StackitDNSProvider) handleRRSetWithWorkers(
96+
ctx context.Context,
97+
endpoints []*endpoint.Endpoint,
98+
zones []stackitdnsclient.Zone,
99+
action string,
100+
) error {
101+
workerChannel := make(chan changeTask, len(endpoints))
102+
errorChannel := make(chan error, len(endpoints))
103+
104+
for i := 0; i < d.workers; i++ {
105+
go d.changeWorker(ctx, workerChannel, errorChannel, zones)
106+
}
107+
108+
for _, change := range endpoints {
109+
workerChannel <- changeTask{
110+
action: action,
111+
change: change,
112+
}
113+
}
114+
115+
for i := 0; i < len(endpoints); i++ {
116+
err := <-errorChannel
117+
if err != nil {
118+
close(workerChannel)
119+
120+
return err
121+
}
122+
}
123+
124+
close(workerChannel)
125+
126+
return nil
127+
}
128+
129+
// changeWorker is a worker that handles changes passed by a channel.
130+
func (d *StackitDNSProvider) changeWorker(
131+
ctx context.Context,
132+
changes chan changeTask,
133+
errorChannel chan error,
134+
zones []stackitdnsclient.Zone,
135+
) {
136+
for change := range changes {
137+
switch change.action {
138+
case CREATE:
139+
err := d.createRRSet(ctx, change.change, zones)
140+
errorChannel <- err
141+
case UPDATE:
142+
err := d.updateRRSet(ctx, change.change, zones)
143+
errorChannel <- err
144+
case DELETE:
145+
err := d.deleteRRSet(ctx, change.change, zones)
146+
errorChannel <- err
147+
}
148+
}
149+
150+
d.logger.Debug("change worker finished")
151+
}
152+
54153
// createRRSet creates a new record set in the stackitprovider for the given endpoint.
55154
func (d *StackitDNSProvider) createRRSet(
56155
ctx context.Context,
@@ -88,24 +187,6 @@ func (d *StackitDNSProvider) createRRSet(
88187
return nil
89188
}
90189

91-
// updateRRSets patches (overrides) contents in the record sets in the stackitprovider for the given
92-
// endpoints that are in the update new field.
93-
func (d *StackitDNSProvider) updateRRSets(
94-
ctx context.Context,
95-
endpoints []*endpoint.Endpoint,
96-
) error {
97-
if len(endpoints) == 0 {
98-
return nil
99-
}
100-
101-
zones, err := d.zoneFetcherClient.zones(ctx)
102-
if err != nil {
103-
return err
104-
}
105-
106-
return d.handleRRSetWithWorkers(ctx, endpoints, zones, UPDATE)
107-
}
108-
109190
// updateRRSet patches (overrides) contents in the record set in the stackitprovider.
110191
func (d *StackitDNSProvider) updateRRSet(
111192
ctx context.Context,
@@ -142,28 +223,6 @@ func (d *StackitDNSProvider) updateRRSet(
142223
return nil
143224
}
144225

145-
// deleteRRSets deletes record sets in the stackitprovider for the given endpoints that are in the
146-
// deletion field.
147-
func (d *StackitDNSProvider) deleteRRSets(
148-
ctx context.Context,
149-
endpoints []*endpoint.Endpoint,
150-
) error {
151-
if len(endpoints) == 0 {
152-
d.logger.Debug("no endpoints to delete")
153-
154-
return nil
155-
}
156-
157-
d.logger.Info("records to delete", zap.String("records", fmt.Sprintf("%v", endpoints)))
158-
159-
zones, err := d.zoneFetcherClient.zones(ctx)
160-
if err != nil {
161-
return err
162-
}
163-
164-
return d.handleRRSetWithWorkers(ctx, endpoints, zones, DELETE)
165-
}
166-
167226
// deleteRRSet deletes a record set in the stackitprovider for the given endpoint.
168227
func (d *StackitDNSProvider) deleteRRSet(
169228
ctx context.Context,
@@ -197,62 +256,3 @@ func (d *StackitDNSProvider) deleteRRSet(
197256

198257
return nil
199258
}
200-
201-
// handleRRSetWithWorkers handles the given endpoints with workers to optimize speed.
202-
func (d *StackitDNSProvider) handleRRSetWithWorkers(
203-
ctx context.Context,
204-
endpoints []*endpoint.Endpoint,
205-
zones []stackitdnsclient.Zone,
206-
action string,
207-
) error {
208-
workerChannel := make(chan changeTask, len(endpoints))
209-
errorChannel := make(chan error, len(endpoints))
210-
211-
for i := 0; i < d.workers; i++ {
212-
go d.changeWorker(ctx, workerChannel, errorChannel, zones)
213-
}
214-
215-
for _, change := range endpoints {
216-
workerChannel <- changeTask{
217-
action: action,
218-
change: change,
219-
}
220-
}
221-
222-
for i := 0; i < len(endpoints); i++ {
223-
err := <-errorChannel
224-
if err != nil {
225-
close(workerChannel)
226-
227-
return err
228-
}
229-
}
230-
231-
close(workerChannel)
232-
233-
return nil
234-
}
235-
236-
// changeWorker is a worker that handles changes passed by a channel.
237-
func (d *StackitDNSProvider) changeWorker(
238-
ctx context.Context,
239-
changes chan changeTask,
240-
errorChannel chan error,
241-
zones []stackitdnsclient.Zone,
242-
) {
243-
for change := range changes {
244-
switch change.action {
245-
case CREATE:
246-
err := d.createRRSet(ctx, change.change, zones)
247-
errorChannel <- err
248-
case UPDATE:
249-
err := d.updateRRSet(ctx, change.change, zones)
250-
errorChannel <- err
251-
case DELETE:
252-
err := d.deleteRRSet(ctx, change.change, zones)
253-
errorChannel <- err
254-
}
255-
}
256-
257-
d.logger.Debug("change worker finished")
258-
}

0 commit comments

Comments
 (0)