RobertIndie commented on code in PR #1256: URL: https://github.com/apache/pulsar-client-go/pull/1256#discussion_r1697802554
########## pulsar/internal/retry.go: ########## @@ -0,0 +1,62 @@ +// 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 internal + +import ( + "context" + "errors" + "time" +) + +type OpFn[T any] func() (T, error) + +// Retry the given operation until the returned error is nil or the context is done. +func Retry[T any](ctx context.Context, op OpFn[T], nextDuration func(error) time.Duration) (T, error) { + var ( + timer *time.Timer + res T + err error + ) + + cleanTimer := func() { + if timer != nil && !timer.Stop() { + <-timer.C + } + } + defer cleanTimer() + + for { + res, err = op() + if err == nil { + return res, nil + } + + duration := nextDuration(err) + if timer == nil { + timer = time.NewTimer(duration) + } else { + timer.Reset(duration) Review Comment: According to the [go doc of the timer](https://pkg.go.dev/time#Timer.Reset): > If a program has not yet received a value from t.C, however, the timer must be stopped and—if Stop reports that the timer expired before being stopped—the channel explicitly drained We should stop the timer and drain the channel before resetting the timer. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
