You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "AlinsRan (via GitHub)" <gi...@apache.org> on 2025/09/01 00:22:38 UTC
[PR] feat: support retry in case of sync failure [apisix-ingress-controller]
AlinsRan opened a new pull request, #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534
<!-- Please answer these questions before submitting a pull request -->
### Type of change:
<!-- Please delete options that are not relevant. -->
<!-- Select all the options from below that matches the type your PR best -->
- [ ] Bugfix
- [ ] New feature provided
- [ ] Improve performance
- [ ] Backport patches
- [ ] Documentation
- [ ] Refactor
- [ ] Chore
- [ ] CI/CD or Tests
### What this PR does / why we need it:
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
### Pre-submission checklist:
<!--
Please follow the requirements:
1. Use Draft if the PR is not ready to be reviewed
2. Test is required for the feat/fix PR, unless you have a good reason
3. Doc is required for the feat PR
4. Use a new commit to resolve review instead of `push -f`
5. Use "request review" to notify the reviewer once you have resolved the review
-->
- [ ] Did you explain what problem does this PR solve? Or what new features have been added?
- [ ] Have you added corresponding test cases?
- [ ] Have you modified the corresponding document?
- [ ] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix-ingress-controller#community) first**
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313438584
##########
internal/provider/apisix/provider.go:
##########
@@ -223,32 +231,38 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
- time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
- log.Error(err)
- return
- }
- })
+ time.AfterFunc(initalSyncDelay, d.syncNotify)
}
- if d.SyncPeriod < 1 {
- return nil
+ syncPeriod := d.SyncPeriod
+ if syncPeriod < MinSyncPeriod {
+ syncPeriod = MinSyncPeriod
}
Review Comment:
This change modifies the sync behavior by enforcing a minimum sync period of 1 second, even when users configure a shorter period or disable syncing with 0. This is a breaking change that could affect users who intentionally want to disable periodic syncing by setting SyncPeriod to 0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313418181
##########
internal/provider/apisix/provider.go:
##########
@@ -224,31 +230,42 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
+ if d.SyncPeriod > 0 {
+ d.syncNotify()
Review Comment:
When SyncPeriod > 0, the initial sync is skipped and only a sync notification is sent. This means if the sync fails during the notification, there's no retry mechanism in place for the initial sync. The retry logic only applies to the main sync loop. Consider ensuring initial sync failures are also handled with retries.
```suggestion
// Retry syncNotify with exponential backoff until it succeeds or context is canceled
retrier := common.NewRetrier(common.NewExponentialBackoff(RetryBaseDelay, RetryMaxDelay))
for {
select {
case <-ctx.Done():
retrier.Cancel()
return
case <-retrier.C():
err := d.syncNotify()
if err != nil {
log.Error("initial syncNotify failed, will retry", zap.Error(err))
retrier.Trigger()
} else {
retrier.Cancel()
return
}
default:
// First attempt immediately
err := d.syncNotify()
if err != nil {
log.Error("initial syncNotify failed, will retry", zap.Error(err))
retrier.Trigger()
// Wait for retrier.C() to fire
time.Sleep(RetryBaseDelay)
} else {
retrier.Cancel()
return
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "ronething (via GitHub)" <gi...@apache.org>.
ronething commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313510215
##########
internal/provider/common/retrier.go:
##########
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package common
+
+import (
+ "sync"
+ "time"
+)
+
+type Backoff interface {
+ Next() time.Duration
+ Reset()
+}
+
+type ExponentialBackoff struct {
+ base, max, current time.Duration
+}
+
+func NewExponentialBackoff(base, max time.Duration) *ExponentialBackoff {
+ return &ExponentialBackoff{base: base, max: max, current: base}
+}
+
+func (b *ExponentialBackoff) Next() time.Duration {
+ delay := b.current
+ b.current *= 2
+ if b.current > b.max {
+ b.current = b.max
+ }
+ return delay
+}
+
+func (b *ExponentialBackoff) Reset() {
+ b.current = b.base
+}
+
+type Retrier struct {
+ mu sync.Mutex
+ ch chan struct{}
+ timer *time.Timer
+ backoff Backoff
+}
+
+func NewRetrier(b Backoff) *Retrier {
+ return &Retrier{
+ ch: make(chan struct{}, 1),
+ backoff: b,
+ }
+}
+
+func (r *Retrier) Cancel() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ if r.timer != nil {
+ r.timer.Stop()
+ r.timer = nil
+ }
+ r.backoff.Reset()
+}
+
+func (r *Retrier) Trigger() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ delay := r.backoff.Next()
+ r.timer = time.AfterFunc(delay, func() {
+ select {
+ case r.ch <- struct{}{}:
+ default:
+ }
+ })
Review Comment:
If the Trigger is triggered multiple times, and it is not necessarily from the last timer trigger (for example, triggered by the syncPeriod synchronization time and also failed to synchronize), does this indicate a timer leak here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313482304
##########
internal/provider/apisix/provider.go:
##########
@@ -223,32 +231,38 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
- time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
- log.Error(err)
- return
- }
- })
+ time.AfterFunc(initalSyncDelay, d.syncNotify)
}
- if d.SyncPeriod < 1 {
- return nil
+ syncPeriod := d.SyncPeriod
+ if syncPeriod < MinSyncPeriod {
+ syncPeriod = MinSyncPeriod
}
Review Comment:
https://github.com/apache/apisix-ingress-controller/blob/master/internal/controller/config/config.go#L122
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313602954
##########
internal/provider/common/retrier.go:
##########
@@ -0,0 +1,96 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package common
+
+import (
+ "sync"
+ "time"
+)
+
+type Backoff interface {
+ Next() time.Duration
+ Reset()
+}
+
+type ExponentialBackoff struct {
+ base, max, current time.Duration
+}
+
+func NewExponentialBackoff(base, max time.Duration) *ExponentialBackoff {
+ return &ExponentialBackoff{base: base, max: max, current: base}
+}
+
+func (b *ExponentialBackoff) Next() time.Duration {
+ delay := b.current
+ b.current *= 2
+ if b.current > b.max {
+ b.current = b.max
+ }
+ return delay
+}
+
+func (b *ExponentialBackoff) Reset() {
+ b.current = b.base
+}
+
+type Retrier struct {
+ mu sync.Mutex
+ ch chan struct{}
+ timer *time.Timer
+ backoff Backoff
+}
+
+func NewRetrier(b Backoff) *Retrier {
+ return &Retrier{
+ ch: make(chan struct{}, 1),
+ backoff: b,
+ }
+}
+
+func (r *Retrier) Reset() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ if r.timer != nil {
+ r.timer.Stop()
+ r.timer = nil
+ }
+ r.backoff.Reset()
+}
+
+func (r *Retrier) Next() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ if r.timer != nil {
+ r.timer.Stop()
+ r.timer = nil
+ }
+
+ delay := r.backoff.Next()
+ r.timer = time.AfterFunc(delay, func() {
+ select {
+ case r.ch <- struct{}{}:
+ default:
+ }
+ })
Review Comment:
Potential race condition: the timer callback accesses r.ch without holding the mutex, while Reset() and Next() methods modify r.timer under mutex protection. This could lead to inconsistent state if Reset() is called while the timer callback is executing.
##########
internal/provider/apisix/provider.go:
##########
@@ -223,33 +231,32 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
- time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
- log.Error(err)
- return
- }
- })
+ time.AfterFunc(initalSyncDelay, d.syncNotify)
}
- if d.SyncPeriod < 1 {
- return nil
+ syncPeriod := d.SyncPeriod
+ if syncPeriod < MinSyncPeriod {
+ syncPeriod = MinSyncPeriod
}
- ticker := time.NewTicker(d.SyncPeriod)
+ ticker := time.NewTicker(syncPeriod)
defer ticker.Stop()
+
+ retrier := common.NewRetrier(common.NewExponentialBackoff(RetryBaseDelay, RetryMaxDelay))
+
for {
- synced := false
select {
case <-d.syncCh:
- synced = true
case <-ticker.C:
- synced = true
+ case <-retrier.C():
case <-ctx.Done():
+ retrier.Reset()
return nil
Review Comment:
The SyncPeriod validation logic is incorrect. The original code returned early when SyncPeriod < 1, meaning no periodic syncing would occur. This change forces a minimum sync period of 1 second even when the user explicitly disables periodic syncing by setting it to 0.
##########
test/e2e/framework/manifests/ingress.yaml:
##########
@@ -333,7 +333,7 @@ data:
# The period between two consecutive syncs.
# The default value is 0 seconds, which means the controller will not sync.
# If you want to enable the sync, set it to a positive value.
- init_sync_delay: {{ .InitSyncDelay | default "1m" }}
+ init_sync_delay: {{ .InitSyncDelay | default "20m" }}
Review Comment:
[nitpick] Increasing the default init_sync_delay from 1m to 20m significantly impacts test execution time and may not be necessary for all test scenarios. Consider making this configurable per test case instead of changing the global default.
```suggestion
init_sync_delay: {{ .InitSyncDelay | default "1m" }}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313400550
##########
internal/provider/apisix/provider.go:
##########
@@ -223,32 +229,46 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
- time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
- log.Error(err)
- return
- }
- })
+ if d.SyncPeriod < 1 {
Review Comment:
The condition checks if SyncPeriod is less than 1, but this comparison against an integer literal doesn't match the time.Duration type. This should compare against a time.Duration value like `time.Duration(0)` or use `d.SyncPeriod <= 0`.
##########
internal/provider/apisix/provider.go:
##########
@@ -223,32 +229,46 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
- time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
- log.Error(err)
- return
- }
- })
+ if d.SyncPeriod < 1 {
+ time.AfterFunc(initalSyncDelay, func() {
+ if err := d.sync(ctx); err != nil {
+ log.Error(err)
+ return
+ }
+ })
+ } else {
+ time.AfterFunc(initalSyncDelay, d.syncNotify)
+ }
}
if d.SyncPeriod < 1 {
Review Comment:
Same issue as above - comparing time.Duration with integer literal 1. This should be `d.SyncPeriod <= 0` to properly check if periodic sync is disabled.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313560177
##########
test/e2e/framework/manifests/ingress.yaml:
##########
@@ -333,7 +333,7 @@ data:
# The period between two consecutive syncs.
# The default value is 0 seconds, which means the controller will not sync.
# If you want to enable the sync, set it to a positive value.
- init_sync_delay: {{ .InitSyncDelay | default "1m" }}
+ init_sync_delay: {{ .InitSyncDelay | default "20m" }}
Review Comment:
The default init sync delay has been changed from '1m' to '20m' which significantly increases test execution time. This change appears unrelated to the retry functionality and may slow down the test suite unnecessarily.
```suggestion
init_sync_delay: {{ .InitSyncDelay | default "1m" }}
```
##########
internal/provider/apisix/provider.go:
##########
@@ -223,33 +231,31 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
- time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
- log.Error(err)
- return
- }
- })
+ time.AfterFunc(initalSyncDelay, d.syncNotify)
Review Comment:
The method `d.syncNotify` is called but doesn't exist in the visible code. This will cause a compilation error.
##########
internal/provider/apisix/provider.go:
##########
@@ -223,33 +231,31 @@ func (d *apisixProvider) Start(ctx context.Context) error {
initalSyncDelay := d.InitSyncDelay
if initalSyncDelay > 0 {
- time.AfterFunc(initalSyncDelay, func() {
- if err := d.sync(ctx); err != nil {
- log.Error(err)
- return
- }
- })
+ time.AfterFunc(initalSyncDelay, d.syncNotify)
}
- if d.SyncPeriod < 1 {
- return nil
+ syncPeriod := d.SyncPeriod
+ if syncPeriod < MinSyncPeriod {
+ syncPeriod = MinSyncPeriod
Review Comment:
The logic changes the minimum sync period behavior. Previously, if `d.SyncPeriod < 1`, the sync would be disabled (return early). Now it sets a minimum of 1 second, which changes the existing behavior and may cause unexpected periodic syncing when users intended to disable it.
```suggestion
if syncPeriod < 1*time.Second {
log.Infow("sync period < 1s, periodic syncing is disabled")
return nil
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2312793846
##########
internal/provider/apisix/provider.go:
##########
@@ -236,19 +237,29 @@ func (d *apisixProvider) Start(ctx context.Context) error {
}
ticker := time.NewTicker(d.SyncPeriod)
defer ticker.Stop()
+
+ retrier := common.NewRetrier(common.NewExponentialBackoff(1*time.Second, 1000*time.Second))
Review Comment:
The magic numbers for backoff timing (1 second base, 1000 seconds max) should be extracted as configurable constants or parameters to make the retry behavior configurable.
##########
internal/provider/common/retrier.go:
##########
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package common
+
+import (
+ "sync"
+ "time"
+)
+
+type Backoff interface {
+ Next() time.Duration
+ Reset()
+}
+
+type ExponentialBackoff struct {
+ base, max, current time.Duration
+}
+
+func NewExponentialBackoff(base, max time.Duration) *ExponentialBackoff {
+ return &ExponentialBackoff{base: base, max: max, current: base}
+}
+
+func (b *ExponentialBackoff) Next() time.Duration {
+ delay := b.current
+ b.current *= 2
+ if b.current > b.max {
+ b.current = b.max
+ }
+ return delay
+}
+
+func (b *ExponentialBackoff) Reset() {
+ b.current = b.base
+}
+
+type Retrier struct {
+ mu sync.Mutex
+ ch chan struct{}
+ timer *time.Timer
+ backoff Backoff
+}
+
+func NewRetrier(b Backoff) *Retrier {
+ return &Retrier{
+ ch: make(chan struct{}, 1),
+ backoff: b,
+ }
+}
+
+func (r *Retrier) Cancel() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ if r.timer != nil {
+ r.timer.Stop()
+ r.timer = nil
+ }
+ r.backoff.Reset()
+}
+
+func (r *Retrier) Trigger() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ delay := r.backoff.Next()
+ r.timer = time.AfterFunc(delay, func() {
+ select {
+ case r.ch <- struct{}{}:
+ default:
+ }
+ })
Review Comment:
The timer creation and channel send operation could race with the Cancel() method. Consider setting r.timer to nil in the AfterFunc callback after sending to the channel to prevent potential issues with subsequent Cancel() calls.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313523235
##########
internal/provider/common/retrier.go:
##########
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package common
+
+import (
+ "sync"
+ "time"
+)
+
+type Backoff interface {
+ Next() time.Duration
+ Reset()
+}
+
+type ExponentialBackoff struct {
+ base, max, current time.Duration
+}
+
+func NewExponentialBackoff(base, max time.Duration) *ExponentialBackoff {
+ return &ExponentialBackoff{base: base, max: max, current: base}
+}
+
+func (b *ExponentialBackoff) Next() time.Duration {
+ delay := b.current
+ b.current *= 2
+ if b.current > b.max {
+ b.current = b.max
+ }
+ return delay
+}
+
+func (b *ExponentialBackoff) Reset() {
+ b.current = b.base
+}
+
+type Retrier struct {
+ mu sync.Mutex
+ ch chan struct{}
+ timer *time.Timer
+ backoff Backoff
+}
+
+func NewRetrier(b Backoff) *Retrier {
+ return &Retrier{
+ ch: make(chan struct{}, 1),
+ backoff: b,
+ }
+}
+
+func (r *Retrier) Cancel() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ if r.timer != nil {
+ r.timer.Stop()
+ r.timer = nil
+ }
+ r.backoff.Reset()
+}
+
+func (r *Retrier) Trigger() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ delay := r.backoff.Next()
+ r.timer = time.AfterFunc(delay, func() {
+ select {
+ case r.ch <- struct{}{}:
+ default:
+ }
+ })
Review Comment:
If it keeps failing, it will continue to run until it succeeds. At the same time, it will also be reset by other events. It will not run again after successful synchronization.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan merged PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat: support retry in case of sync failure [apisix-ingress-controller]
Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #2534:
URL: https://github.com/apache/apisix-ingress-controller/pull/2534#discussion_r2313563532
##########
internal/provider/common/retrier.go:
##########
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package common
+
+import (
+ "sync"
+ "time"
+)
+
+type Backoff interface {
+ Next() time.Duration
+ Reset()
+}
+
+type ExponentialBackoff struct {
+ base, max, current time.Duration
+}
+
+func NewExponentialBackoff(base, max time.Duration) *ExponentialBackoff {
+ return &ExponentialBackoff{base: base, max: max, current: base}
+}
+
+func (b *ExponentialBackoff) Next() time.Duration {
+ delay := b.current
+ b.current *= 2
+ if b.current > b.max {
+ b.current = b.max
+ }
+ return delay
+}
+
+func (b *ExponentialBackoff) Reset() {
+ b.current = b.base
+}
+
+type Retrier struct {
+ mu sync.Mutex
+ ch chan struct{}
+ timer *time.Timer
+ backoff Backoff
+}
+
+func NewRetrier(b Backoff) *Retrier {
+ return &Retrier{
+ ch: make(chan struct{}, 1),
+ backoff: b,
+ }
+}
+
+func (r *Retrier) Cancel() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ if r.timer != nil {
+ r.timer.Stop()
+ r.timer = nil
+ }
+ r.backoff.Reset()
+}
+
+func (r *Retrier) Trigger() {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ delay := r.backoff.Next()
+ r.timer = time.AfterFunc(delay, func() {
+ select {
+ case r.ch <- struct{}{}:
+ default:
+ }
+ })
Review Comment:
You're right, this will burden the user. It's more appropriate to simply stop the timer and proceed to the next one.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org