Kur*_*eek 4 go publish-subscribe google-cloud-pubsub exponential-backoff
该cloud.google.com/go/pubsub
库最近发布(在 v1.5.0 中,参见https://github.com/googleapis/google-cloud-go/releases/tag/pubsub%2Fv1.5.0)支持新的RetryPolicy
服务器端功能。当前读取的文档(https://godoc.org/cloud.google.com/go/pubsub#RetryPolicy)
我读过维基百科文章,虽然它描述了离散时间的指数退避,但我没有看到这篇文章与MinimumBackoff
和MaximumBackoff
参数有什么具体关系。有关此指导下,我提到的文档github.com/cenkalti/backoff
,https://pkg.go.dev/github.com/cenkalti/backoff/v4?tab=doc#ExponentialBackOff。该库将一个定义ExponentialBackoff
为
type ExponentialBackOff struct {
InitialInterval time.Duration
RandomizationFactor float64
Multiplier float64
MaxInterval time.Duration
// After MaxElapsedTime the ExponentialBackOff returns Stop.
// It never stops if MaxElapsedTime == 0.
MaxElapsedTime time.Duration
Stop time.Duration
Clock Clock
// contains filtered or unexported fields
}
Run Code Online (Sandbox Code Playgroud)
其中每个随机区间计算为
randomized interval =
RetryInterval * (random value in range [1 - RandomizationFactor, 1 + RandomizationFactor])
Run Code Online (Sandbox Code Playgroud)
哪里RetryInterval
是当前的重试间隔,据我所知,它从 的值开始InitialInterval
并以MaxInterval
.
我是否正确理解MinimumBackoff
andMaximumBackoff
对应于InitialInterval
and MaxInterval
in github.com/cenkalti/backoff
?也就是说,MinimumBackoff
是初始等待期,MaximumBackoff
是重试之间允许的最大时间量?
为了测试我的理论,我编写了以下简化程序:
randomized interval =
RetryInterval * (random value in range [1 - RandomizationFactor, 1 + RandomizationFactor])
Run Code Online (Sandbox Code Playgroud)
如果我分别使用 flag-defaultMinimumBackoff
和MaximumBackoff
5s 和 60s运行它,我会得到以下输出:
> go run main.go
2020/07/29 18:49:32 Running with minumum backoff 5s and maximum backoff 1m0s...
2020/07/29 18:49:33 Created topic "test-topic"
2020/07/29 18:49:34 Created subscription "test-subscription"
2020/07/29 18:49:34 Published message
2020/07/29 18:49:36 Nacking message: Hello, world!
2020/07/29 18:49:45 Nacking message: Hello, world!
2020/07/29 18:49:56 Nacking message: Hello, world!
2020/07/29 18:50:06 Nacking message: Hello, world!
2020/07/29 18:50:17 Nacking message: Hello, world!
2020/07/29 18:50:30 Nacking message: Hello, world!
2020/07/29 18:50:35 Deleted subscription "test-subscription"
2020/07/29 18:50:35 Deleted topic test-topic
Run Code Online (Sandbox Code Playgroud)
而如果我运行它MinimumBackoff
,并MaximumBackoff
1S和2S的分别,我得到
> go run main.go --minimumBackoff=1s --maximumBackoff=2s
2020/07/29 18:50:42 Running with minumum backoff 1s and maximum backoff 2s...
2020/07/29 18:51:11 Created topic "test-topic"
2020/07/29 18:51:12 Created subscription "test-subscription"
2020/07/29 18:51:12 Published message
2020/07/29 18:51:15 Nacking message: Hello, world!
2020/07/29 18:51:18 Nacking message: Hello, world!
2020/07/29 18:51:21 Nacking message: Hello, world!
2020/07/29 18:51:25 Nacking message: Hello, world!
2020/07/29 18:51:28 Nacking message: Hello, world!
2020/07/29 18:51:31 Nacking message: Hello, world!
2020/07/29 18:51:35 Nacking message: Hello, world!
2020/07/29 18:51:38 Nacking message: Hello, world!
2020/07/29 18:51:40 Nacking message: Hello, world!
2020/07/29 18:51:44 Nacking message: Hello, world!
2020/07/29 18:51:47 Nacking message: Hello, world!
2020/07/29 18:51:50 Nacking message: Hello, world!
2020/07/29 18:51:52 Nacking message: Hello, world!
2020/07/29 18:51:54 Nacking message: Hello, world!
2020/07/29 18:51:57 Nacking message: Hello, world!
2020/07/29 18:52:00 Nacking message: Hello, world!
2020/07/29 18:52:03 Nacking message: Hello, world!
2020/07/29 18:52:06 Nacking message: Hello, world!
2020/07/29 18:52:09 Nacking message: Hello, world!
2020/07/29 18:52:12 Nacking message: Hello, world!
2020/07/29 18:52:13 Deleted subscription "test-subscription"
2020/07/29 18:52:13 Deleted topic test-topic
Run Code Online (Sandbox Code Playgroud)
似乎在后一个例子中,nacks 之间的时间非常一致~3s,这大概代表了在MaximumBackoff
2s 内做到这一点的“最大努力” ?对我来说仍然不清楚的是是否有任何随机化,是否有乘数(从第一个例子来看,重试之间的时间似乎没有每次都增加一倍),以及是否有等价的在的MaxElapsedTime
超越其中有没有更重?
小智 5
最小退避和最大退避的重试策略字段类似于上面示例中的 InitialInterval 和 MaxInterval。Cloud Pub/Sub 使用您提到的类似公式来计算指数延迟。这也包括随机化。
超出 MaxInterval,每次后续重试都会增加 MaxInterval 延迟。如果您想在一定次数的尝试后停止重试,我们建议使用Dead Letter Queues。
归档时间: |
|
查看次数: |
1917 次 |
最近记录: |