github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/binlogstream/util.go (about)

     1  // Copyright 2022 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 binlogstream
    15  
    16  import (
    17  	"time"
    18  
    19  	"github.com/pingcap/tiflow/dm/relay"
    20  )
    21  
    22  // BinlogType represents binlog type from syncer's view.
    23  type BinlogType uint8
    24  
    25  const (
    26  	// RemoteBinlog means syncer is connected to MySQL and reading binlog.
    27  	RemoteBinlog BinlogType = iota + 1
    28  	// LocalBinlog means syncer is reading binlog from relay log.
    29  	LocalBinlog
    30  )
    31  
    32  // RelayToBinlogType converts relay.Process to BinlogType.
    33  func RelayToBinlogType(relay relay.Process) BinlogType {
    34  	if relay != nil {
    35  		return LocalBinlog
    36  	}
    37  
    38  	return RemoteBinlog
    39  }
    40  
    41  func (t BinlogType) String() string {
    42  	switch t {
    43  	case RemoteBinlog:
    44  		return "remote"
    45  	case LocalBinlog:
    46  		return "local"
    47  	default:
    48  		return "unknown"
    49  	}
    50  }
    51  
    52  // the minimal interval to retry reset binlog streamer.
    53  var minErrorRetryInterval = 1 * time.Minute
    54  
    55  type retryStrategy interface {
    56  	CanRetry(error) bool
    57  }
    58  
    59  type alwaysRetryStrategy struct{}
    60  
    61  func (s alwaysRetryStrategy) CanRetry(error) bool {
    62  	return true
    63  }
    64  
    65  // maxIntervalRetryStrategy allows retry when the retry interval is greater than the set interval.
    66  type maxIntervalRetryStrategy struct {
    67  	interval    time.Duration
    68  	lastErr     error
    69  	lastErrTime time.Time
    70  }
    71  
    72  func (s *maxIntervalRetryStrategy) CanRetry(err error) bool {
    73  	if err == nil {
    74  		return true
    75  	}
    76  
    77  	now := time.Now()
    78  	lastErrTime := s.lastErrTime
    79  	s.lastErrTime = now
    80  	s.lastErr = err
    81  	return lastErrTime.Add(s.interval).Before(now)
    82  }