fast_retry

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 5 Imported by: 0

README

fast-retry

Release Software License Go Report Card GoDoc

Simple library for Backup Requests

Instructions

服务中的长尾请求增加了服务的整体延迟,长尾请求一般占比很低,但是响应时间可能比平均时间多出一个数量级。

相当于一部分长尾请求是偶发的,也就是说,立即重发请求,可以正常返回。如果简单的加上重试,无助于减少长尾请求数量。

这个包实现了一种叫 Backup Requests 的技术,在检查到当前请求处理时间超出 PCT99 之后,再发起一个请求,任意一次请求返回我们就认为请求成功,通过增加适当的负载,大大减少了响应时间的波动。

此包还支持配置最大增加的请求比例,防止错误的配置导致发出双倍的请求。

Example

package main

import (
	"context"
	"fmt"
	"time"

	fast_retry "github.com/wangkechun/fast-retry"
)

func main() {
	ctx := context.Background()
	retry := fast_retry.New(fast_retry.Config{FastRetryTime: time.Second / 10})
	resp, err := retry.BackupRetry(ctx, func() (interface{}, error) {
		return "hello", nil
	})
	if err != nil {
		fmt.Println("err", err)
	} else {
		fmt.Println("resp", resp.(string))
	}
}

Usage

type Config struct {
    FastRetryTime time.Duration // 默认 2s,建议设置成当前 API 的 PCT99
    MaxRetryRate  float64       // 最大重试百分比,0.05 代表 5%,默认 5%,超出返回 ErrRetryQuotaExceeded
    RetryCnt      int           // 最多发送的请求次数,默认 3 次,最少两次,其中一次是快速重试
    RetryWaitTime time.Duration // 重试间隔,默认是 FastRetryTime / 10
    Clock         Clock         // 模拟时钟,mock 用
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRetryQuotaExceeded = errors.New("fast_retry:retry quota exceeded")

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
	Sleep(d time.Duration)
}

type Config

type Config struct {
	FastRetryTime    time.Duration        // 默认 2s,建议设置成当前 API 的 PCT99
	MaxRetryRate     float64              // 最大重试百分比,0.05 代表 5%,默认 5%,超出返回 ErrRetryQuotaExceeded
	RetryCnt         int                  // 最多发送的请求次数,默认 3 次,最少两次,其中一次是快速重试
	RetryWaitTime    time.Duration        // 重试间隔,默认是 FastRetryTime / 10
	Clock            Clock                // 模拟时钟,mock 用
	MaxRetryCapacity int                  // 平时没有超时的时候,允许积攒的重启请求最大数目,默认 1000 个
	RetryIf          func(err error) bool // 出错的时候调用,判断是否对 error 进行重试,默认值 err != nil
}

type Retry

type Retry struct {
	// contains filtered or unexported fields
}

func New

func New(config Config) *Retry

func (*Retry) BackupRetry

func (r *Retry) BackupRetry(ctx context.Context, retryableFunc func() (resp interface{}, err error)) (resp interface{}, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL