github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/relay/remote_retry.go (about) 1 // Copyright 2019 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package relay 15 16 import ( 17 "context" 18 "time" 19 20 "github.com/pingcap/failpoint" 21 "github.com/pingcap/tiflow/dm/pkg/backoff" 22 "github.com/pingcap/tiflow/dm/pkg/retry" 23 "github.com/pingcap/tiflow/dm/pkg/terror" 24 ) 25 26 // ReaderRetryConfig is the configuration used for binlog reader retry backoff. 27 // we always enable this now. 28 type ReaderRetryConfig struct { 29 BackoffRollback time.Duration `toml:"backoff-rollback" json:"backoff-rollback"` 30 BackoffMax time.Duration `toml:"backoff-max" json:"backoff-max"` 31 // unexposed config 32 BackoffMin time.Duration `json:"-"` 33 BackoffJitter bool `json:"-"` 34 BackoffFactor float64 `json:"-"` 35 } 36 37 // ReaderRetry is used to control the retry for the ReaderRetry. 38 // It is not thread-safe. 39 type ReaderRetry struct { 40 cfg ReaderRetryConfig 41 bf *backoff.Backoff 42 lastRetryTime time.Time 43 } 44 45 // NewReaderRetry creates a new ReaderRetry instance. 46 func NewReaderRetry(cfg ReaderRetryConfig) (*ReaderRetry, error) { 47 bf, err := backoff.NewBackoff(cfg.BackoffFactor, cfg.BackoffJitter, cfg.BackoffMin, cfg.BackoffMax) 48 if err != nil { 49 return nil, terror.WithClass(err, terror.ClassRelayUnit) 50 } 51 return &ReaderRetry{ 52 cfg: cfg, 53 bf: bf, 54 lastRetryTime: time.Now(), 55 }, nil 56 } 57 58 // Check checks whether should retry for the error. 59 func (rr *ReaderRetry) Check(ctx context.Context, err error) bool { 60 failpoint.Inject("RelayAllowRetry", func() { 61 failpoint.Return(true) 62 }) 63 if !retry.IsConnectionError(err) { 64 return false 65 } 66 67 for lrt := rr.lastRetryTime; time.Since(lrt) > rr.cfg.BackoffRollback; lrt = lrt.Add(rr.cfg.BackoffRollback) { 68 rr.bf.Rollback() 69 } 70 71 duration := rr.bf.Current() 72 select { 73 case <-ctx.Done(): 74 return false 75 case <-time.After(duration): 76 } 77 78 rr.lastRetryTime = time.Now() 79 rr.bf.BoundaryForward() 80 return true 81 }