github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/storage/backoff.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package storage
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/cenkalti/backoff"
    21  	"github.com/vescale/zgraph/internal/logutil"
    22  )
    23  
    24  func expoBackoff() backoff.BackOff {
    25  	b := &backoff.ExponentialBackOff{
    26  		InitialInterval:     10 * time.Millisecond,
    27  		RandomizationFactor: backoff.DefaultRandomizationFactor,
    28  		Multiplier:          backoff.DefaultMultiplier,
    29  		MaxInterval:         5 * time.Second,
    30  		MaxElapsedTime:      20 * time.Second,
    31  		Clock:               backoff.SystemClock,
    32  	}
    33  	b.Reset()
    34  	return b
    35  }
    36  
    37  // BackoffErrReporter reports backoff error events.
    38  func BackoffErrReporter(site string) backoff.Notify {
    39  	return func(err error, dur time.Duration) {
    40  		logutil.Infof("Backoff in [%s](retry in %s) caused by: %+v", site, dur, err)
    41  	}
    42  }