Giter Site home page Giter Site logo

fast-retry's Introduction

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 用
}

fast-retry's People

Contributors

wangkechun avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.