gpt4 book ai didi

go - Google Pub/Sub 的 RetryPolicy 中配置的指数退避如何工作?

转载 作者:行者123 更新时间:2023-12-01 20:25:55 25 4
gpt4 key购买 nike

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 )当前读取
enter image description here
我读过维基百科的文章,虽然它描述了离散时间的指数退避,但我看不出这篇文章与 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
}
其中每个随机区间计算为
randomized interval =
RetryInterval * (random value in range [1 - RandomizationFactor, 1 + RandomizationFactor])
哪里 RetryInterval是当前的重试间隔,据我所知,它的起始值为 InitialInterval并由 MaxInterval 封顶.
我是否正确理解 MinimumBackoffMaximumBackoff对应于 InitialIntervalMaxIntervalgithub.com/cenkalti/backoff ?也就是说, MinimumBackoff是初始等待期, MaximumBackoff重试之间允许的最大时间是多少?
为了测试我的理论,我编写了以下简化程序:
package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"time"

"cloud.google.com/go/pubsub"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var (
projectID string
minimumBackoff, maximumBackoff time.Duration
)

const (
topicName = "test-topic"
subName = "test-subscription"
defaultMinimumBackoff = 10 * time.Second
defaultMaximumBackoff = 10 * time.Minute
)

func main() {
flag.StringVar(&projectID, "projectID", "my-project", "Google Project ID")
flag.DurationVar(&minimumBackoff, "minimumBackoff", 5*time.Second, "minimum backoff")
flag.DurationVar(&maximumBackoff, "maximumBackoff", 60*time.Second, "maximum backoff")
flag.Parse()
log.Printf("Running with minumum backoff %v and maximum backoff %v...", minimumBackoff, maximumBackoff)

retryPolicy := &pubsub.RetryPolicy{MinimumBackoff: minimumBackoff, MaximumBackoff: maximumBackoff}

client, err := pubsub.NewClient(context.Background(), projectID)
if err != nil {
log.Fatalf("NewClient: %v", err)
}

topic, err := client.CreateTopic(context.Background(), topicName)
if err != nil {
log.Fatalf("CreateTopic: %v", err)
}
log.Printf("Created topic %q", topicName)
defer func() {
topic.Stop()
if err := topic.Delete(context.Background()); err != nil {
log.Fatalf("Delete topic: %v", err)
}
log.Printf("Deleted topic %s", topicName)
}()

sub, err := client.CreateSubscription(context.Background(), subName, pubsub.SubscriptionConfig{
Topic: topic,
RetryPolicy: retryPolicy,
})
if err != nil {
log.Fatalf("CreateSubscription: %v", err)
}
log.Printf("Created subscription %q", subName)
defer func() {
if err := sub.Delete(context.Background()); err != nil {
log.Fatalf("Delete subscription: %v", err)
}
log.Printf("Deleted subscription %q", subName)
}()

go func() {
sub.Receive(context.Background(), func(ctx context.Context, msg *pubsub.Message) {
log.Printf("Nacking message: %s", msg.Data)
msg.Nack()
})
}()

topic.Publish(context.Background(), &pubsub.Message{Data: []byte("Hello, world!")})
log.Println("Published message")
time.Sleep(60 * time.Second)
}
如果我使用 flag-default MinimumBackoff 运行它和 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
而如果我用 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
似乎在后一个例子中,nacks 之间的时间相当一致~3s,这大概代表了在 MaximumBackoff 中做到这一点的“最大努力”。 2s?我仍然不清楚的是是否有任何随机化,是否有乘数(从第一个例子来看,重试之间的时间似乎没有每次都增加一倍),以及是否有等价的的 MaxElapsedTime超过它没有更多的重试?

最佳答案

最小退避和最大退避的重试策略字段类似于上面示例中的 InitialInterval 和 MaxInterval。 Cloud Pub/Sub 使用您提到的类似公式来计算指数延迟。这也包括随机化。
超出 MaxInterval,每次后续重试都会增加 MaxInterval 延迟。如果您想在一定次数的尝试后停止重试,我们建议使用 Dead Letter Queues .

关于go - Google Pub/Sub 的 RetryPolicy 中配置的指数退避如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63165028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com