@@ -395,35 +395,40 @@ func (api *API) updateEntitlements(ctx context.Context) error {
395
395
return nil
396
396
}
397
397
398
- featureChanged := func (featureName codersdk.FeatureName ) (changed bool , enabled bool ) {
398
+ featureChanged := func (featureName codersdk.FeatureName ) (initial , changed , enabled bool ) {
399
399
if api .entitlements .Features == nil {
400
- return true , entitlements .Features [featureName ].Enabled
400
+ return true , false , entitlements .Features [featureName ].Enabled
401
401
}
402
402
oldFeature := api .entitlements .Features [featureName ]
403
403
newFeature := entitlements .Features [featureName ]
404
404
if oldFeature .Enabled != newFeature .Enabled {
405
- return true , newFeature .Enabled
405
+ return false , true , newFeature .Enabled
406
406
}
407
- return false , newFeature .Enabled
407
+ return false , false , newFeature .Enabled
408
408
}
409
409
410
- if changed , enabled := featureChanged (codersdk .FeatureAuditLog ); changed {
410
+ shouldUpdate := func (initial , changed , enabled bool ) bool {
411
+ // Avoid an initial tick on startup unless the feature is enabled.
412
+ return changed || (initial && enabled )
413
+ }
414
+
415
+ if initial , changed , enabled := featureChanged (codersdk .FeatureAuditLog ); shouldUpdate (initial , changed , enabled ) {
411
416
auditor := agplaudit .NewNop ()
412
417
if enabled {
413
418
auditor = api .AGPL .Options .Auditor
414
419
}
415
420
api .AGPL .Auditor .Store (& auditor )
416
421
}
417
422
418
- if changed , enabled := featureChanged (codersdk .FeatureBrowserOnly ); changed {
423
+ if initial , changed , enabled := featureChanged (codersdk .FeatureBrowserOnly ); shouldUpdate ( initial , changed , enabled ) {
419
424
var handler func (rw http.ResponseWriter ) bool
420
425
if enabled {
421
426
handler = api .shouldBlockNonBrowserConnections
422
427
}
423
428
api .AGPL .WorkspaceClientCoordinateOverride .Store (& handler )
424
429
}
425
430
426
- if changed , enabled := featureChanged (codersdk .FeatureTemplateRBAC ); changed {
431
+ if initial , changed , enabled := featureChanged (codersdk .FeatureTemplateRBAC ); shouldUpdate ( initial , changed , enabled ) {
427
432
if enabled {
428
433
committer := committer {Database : api .Database }
429
434
ptr := proto .QuotaCommitter (& committer )
@@ -433,7 +438,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
433
438
}
434
439
}
435
440
436
- if changed , enabled := featureChanged (codersdk .FeatureAdvancedTemplateScheduling ); changed {
441
+ if initial , changed , enabled := featureChanged (codersdk .FeatureAdvancedTemplateScheduling ); shouldUpdate ( initial , changed , enabled ) {
437
442
if enabled {
438
443
store := & EnterpriseTemplateScheduleStore {}
439
444
ptr := schedule .TemplateScheduleStore (store )
@@ -444,8 +449,8 @@ func (api *API) updateEntitlements(ctx context.Context) error {
444
449
}
445
450
}
446
451
447
- if changed , enabled := featureChanged (codersdk .FeatureHighAvailability ); changed {
448
- coordinator := agpltailnet .NewCoordinator ( api . Logger )
452
+ if initial , changed , enabled := featureChanged (codersdk .FeatureHighAvailability ); shouldUpdate ( initial , changed , enabled ) {
453
+ var coordinator agpltailnet.Coordinator
449
454
if enabled {
450
455
var haCoordinator agpltailnet.Coordinator
451
456
if api .AGPL .Experiments .Enabled (codersdk .ExperimentTailnetHACoordinator ) {
@@ -457,9 +462,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
457
462
api .Logger .Error (ctx , "unable to set up high availability coordinator" , slog .Error (err ))
458
463
// If we try to setup the HA coordinator and it fails, nothing
459
464
// is actually changing.
460
- changed = false
461
465
} else {
462
- _ = coordinator .Close ()
463
466
coordinator = haCoordinator
464
467
}
465
468
@@ -472,6 +475,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
472
475
_ = api .updateEntitlements (ctx )
473
476
})
474
477
} else {
478
+ coordinator = agpltailnet .NewCoordinator (api .Logger )
475
479
api .derpMesh .SetAddresses ([]string {}, false )
476
480
api .replicaManager .SetCallback (func () {
477
481
// If the amount of replicas change, so should our entitlements.
@@ -481,7 +485,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
481
485
}
482
486
483
487
// Recheck changed in case the HA coordinator failed to set up.
484
- if changed {
488
+ if coordinator != nil {
485
489
oldCoordinator := * api .AGPL .TailnetCoordinator .Swap (& coordinator )
486
490
err := oldCoordinator .Close ()
487
491
if err != nil {
0 commit comments