Files
Eugene Burkov 38fbc77925 Pull request 403: AGDNS-2744 Close listeners on error
Merge in GO/dnsproxy from AGDNS-2744-close-listeners-on-err to master

Squashed commit of the following:

commit e19682135682e8ea48110e05e58c62190e9be1ab
Author: Eugene Burkov <[email protected]>
Date:   Thu Jun 5 20:16:40 2025 +0300

    all: upd go, deps

commit 3bc328054291232673547bbd7cc7955a25c17287
Author: Eugene Burkov <[email protected]>
Date:   Thu Jun 5 20:13:07 2025 +0300

    proxy: fix doc

commit 6f4ae4c6402e6d214c4f720f2224fda960e62c5a
Author: Eugene Burkov <[email protected]>
Date:   Thu Jun 5 20:09:49 2025 +0300

    proxy: imp code

commit e25567a28e0d82a41a9c8f39d68cef6cdbae546a
Author: Eugene Burkov <[email protected]>
Date:   Thu Jun 5 19:36:40 2025 +0300

    proxy: add test

commit 4a9a677f9ca60b6e6bd1e9b766994040f8feecc4
Author: Eugene Burkov <[email protected]>
Date:   Thu Jun 5 19:12:09 2025 +0300

    all: close active listeners on error
2025-06-10 15:05:09 +03:00

48 lines
1.2 KiB
Go

package proxy
import (
"context"
"time"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// BindRetryConfig contains configuration for the listeners binding retry
// mechanism.
type BindRetryConfig struct {
// Interval is the minimum time to wait after the latest failure. It must
// not be negative if Enabled is true.
Interval time.Duration
// Count is the maximum number of retries after the first attempt.
Count uint
// Enabled indicates whether the binding should be retried.
Enabled bool
}
// bindWithRetry calls f until it returns no error or the retries limit is
// reached, sleeping for configured interval between attempts. bindFunc must
// not be nil and should carry the result of the binding operation itself.
func (p *Proxy) bindWithRetry(ctx context.Context, bindFunc func() (err error)) (err error) {
err = bindFunc()
if err == nil {
return nil
}
p.logger.WarnContext(ctx, "binding", "attempt", 1, slogutil.KeyError, err)
for attempt := uint(1); attempt <= p.bindRetryCount; attempt++ {
time.Sleep(p.bindRetryIvl)
retryErr := bindFunc()
if retryErr == nil {
return nil
}
p.logger.WarnContext(ctx, "binding", "attempt", attempt+1, slogutil.KeyError, retryErr)
}
return err
}