Skip to content

Commit d474e62

Browse files
authored
Use goroutine instead of errGroup (#497)
Use goroutine instead of errGroup Adjust timings <!-- Thank you for contributing! Please make sure that your code changes are covered with tests. In case of new features or big changes remember to adjust the documentation. In case of an existing issue, reference it using one of the following: closes: #ISSUE related: #ISSUE How to write a good git commit message: http://chris.beams.io/posts/git-commit/ --> ## What *Describe what the change is solving* ## How *Describe the solution* ## Breaking Changes *Are there any breaking changes in this PR?*
2 parents 6e2c6e7 + 17bf76f commit d474e62

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

internal/controller/bindings/boundendpoint_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,9 @@ func (r *BoundEndpointReconciler) tryToBindEndpoint(ctx context.Context, boundEn
525525

526526
retries := 5
527527
attempt := 0
528-
waitDuration := 0 * time.Second // start immediately
529-
backoffDuration := 10 * time.Second // increasing duration to wait between retries
530-
dialTimeout := 5 * time.Second // timeout for dialing the targetService
528+
waitDuration := 0 * time.Second // start immediately
529+
backoffDuration := 3 * time.Second // increasing duration to wait between retries
530+
dialTimeout := 1 * time.Second // timeout for dialing the targetService
531531

532532
// to be filled in
533533
var bindErr error

internal/controller/bindings/boundendpoint_poller.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
bindingsv1alpha1 "github.com/ngrok/ngrok-operator/api/bindings/v1alpha1"
1414
ngrokv1alpha1 "github.com/ngrok/ngrok-operator/api/ngrok/v1alpha1"
1515
"github.com/ngrok/ngrok-operator/internal/ngrokapi"
16-
"golang.org/x/sync/errgroup"
1716
v1 "k8s.io/api/core/v1"
1817
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1918
"k8s.io/apimachinery/pkg/runtime"
@@ -216,22 +215,21 @@ func (r *BoundEndpointPoller) reconcileBoundEndpointsFromAPI(ctx context.Context
216215
toCreate, toUpdate, toDelete := r.filterBoundEndpointActions(ctx, existingBoundEndpoints, desiredBoundEndpoints)
217216

218217
// create context + errgroup for managing/closing the future goroutine in the reconcile actions loops
219-
errGroup, reconcileActionCtx := errgroup.WithContext(context.Background())
220-
reconcileActionCtx, cancel := context.WithCancel(reconcileActionCtx)
218+
reconcileActionCtx, cancel := context.WithCancel(context.Background())
221219
reconcileActionCtx = ctrl.LoggerInto(reconcileActionCtx, log)
222220
r.reconcilingCancel = cancel
223221

224222
// launch goroutines to reconcile the BoundEndpoints' actions in the background until the next polling loop
225223

226-
r.reconcileBoundEndpointAction(reconcileActionCtx, errGroup, toCreate, "create", func(reconcileActionCtx context.Context, binding bindingsv1alpha1.BoundEndpoint) error {
224+
r.reconcileBoundEndpointAction(reconcileActionCtx, toCreate, "create", func(reconcileActionCtx context.Context, binding bindingsv1alpha1.BoundEndpoint) error {
227225
return r.createBinding(reconcileActionCtx, binding)
228226
})
229227

230-
r.reconcileBoundEndpointAction(reconcileActionCtx, errGroup, toUpdate, "update", func(reconcileActionCtx context.Context, binding bindingsv1alpha1.BoundEndpoint) error {
228+
r.reconcileBoundEndpointAction(reconcileActionCtx, toUpdate, "update", func(reconcileActionCtx context.Context, binding bindingsv1alpha1.BoundEndpoint) error {
231229
return r.updateBinding(reconcileActionCtx, binding)
232230
})
233231

234-
r.reconcileBoundEndpointAction(reconcileActionCtx, errGroup, toDelete, "delete", func(reconcileActionCtx context.Context, binding bindingsv1alpha1.BoundEndpoint) error {
232+
r.reconcileBoundEndpointAction(reconcileActionCtx, toDelete, "delete", func(reconcileActionCtx context.Context, binding bindingsv1alpha1.BoundEndpoint) error {
235233
return r.deleteBinding(reconcileActionCtx, binding)
236234
})
237235

@@ -243,32 +241,32 @@ type boundEndpointActionFn func(context.Context, bindingsv1alpha1.BoundEndpoint)
243241

244242
// reconcileBoundEndpointAction runs a goroutine to try and process a list of BoundEndpoints
245243
// for their desired action over and over again until stopChan is closed or receives a value
246-
func (r *BoundEndpointPoller) reconcileBoundEndpointAction(ctx context.Context, errGroup *errgroup.Group, boundEndpoints []bindingsv1alpha1.BoundEndpoint, actionMsg string, action boundEndpointActionFn) {
244+
func (r *BoundEndpointPoller) reconcileBoundEndpointAction(ctx context.Context, boundEndpoints []bindingsv1alpha1.BoundEndpoint, actionMsg string, action boundEndpointActionFn) {
247245
log := ctrl.LoggerFrom(ctx)
248246

249247
if len(boundEndpoints) == 0 {
250248
// nothing to do
251249
return
252250
}
253251

254-
errGroup.Go(func() error {
252+
go func() {
255253
// attempt reconciliation actions every so often
256-
ticker := time.NewTicker(5 * time.Second)
254+
ticker := time.NewTicker(2 * time.Second)
257255
defer ticker.Stop()
258256

259257
// remainingBindings is the list of BoundEndpoints that still need to be actioned upon
260258
remainingBindings := boundEndpoints
261259

262260
for {
263261
if len(remainingBindings) == 0 {
264-
return nil // all bindings have been processed
262+
return
265263
}
266264

267265
select {
268266
// stop go routine and return, there is a new reconcile poll happening actively
269267
case <-ctx.Done():
270268
log.Info("Reconcile Action context canceled, stopping BoundEndpoint reconcile action loop early", "action", actionMsg)
271-
return nil
269+
return
272270
case <-ticker.C:
273271
log.V(9).Info("Received tick", "action", actionMsg, "remaining", remainingBindings)
274272

@@ -287,7 +285,7 @@ func (r *BoundEndpointPoller) reconcileBoundEndpointAction(ctx context.Context,
287285
remainingBindings = failedBindings
288286
}
289287
}
290-
})
288+
}()
291289
}
292290

293291
// filterBoundEndpointActions takse 2 sets of existing and desired BoundEndpoints

0 commit comments

Comments
 (0)