MIT License

项目介绍

Rican7/retry 是一个托管在 GitHub 上的开源项目,主要使用 Go 编写,采用 MIT License 许可证,目前获得 479 个 Star、28 次 Fork。

仓库全名
Rican7/retry
开源许可证
MIT License
Star 数
479
Fork 数
28
未关闭 Issue
3

README 摘要

retry A simple, stateless, functional mechanism to perform actions repetitively until successful. Project Status This project is currently in "pre-release". While the code is heavily tested, the API may change. Use a tagged version or vendor this dependency if you plan on using it. That said, this code has been used in production without issue for years, and has been used by some relatively high-profile…

完整内容见 GitHub 原始 README 或本页 。

常见问题

retry 用什么语言开发?

主要使用 Go 编写,采用 MIT License 许可证,目前有 479 个 Star。

retry 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 retry 的源码?

可以直接克隆仓库:git clone https://github.com/Rican7/retry.git,默认分支为 master;安装与运行步骤见仓库 README。

retry 最近还在维护吗?

仓库最近一次更新为 2023年2月15日,当前有 3 个未关闭 Issue;Star 数为 479,可作为社区活跃度的参考。

关于

A simple, stateless, functional mechanism to perform actions repetitively until successful.

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
541
价值分
296
活跃状态
活跃

语言占比

Go
Makefile

贡献者

README

retry

Build Status Coverage Status Go Report Card Go Reference Latest Stable Version

A simple, stateless, functional mechanism to perform actions repetitively until successful.

Project Status

This project is currently in “pre-release”. While the code is heavily tested, the API may change. Use a tagged version or vendor this dependency if you plan on using it.

That said, this code has been used in production without issue for years, and has been used by some relatively high-profile projects/codebases.

Examples

Basic

retry.Retry(func(attempt uint) error {
	return nil // Do something that may or may not cause an error
})

File Open

const logFilePath = "/var/log/myapp.log"

var logFile *os.File

err := retry.Retry(func(attempt uint) error {
	var err error

	logFile, err = os.Open(logFilePath)

	return err
})

if err != nil {
	log.Fatalf("Unable to open file %q with error %q", logFilePath, err)
}

logFile.Chdir() // Do something with the file

HTTP request with strategies and backoff

var response *http.Response

action := func(attempt uint) error {
	var err error

	response, err = http.Get("https://api.github.com/repos/Rican7/retry")

	if err == nil && response != nil && response.StatusCode > 200 {
		err = fmt.Errorf("failed to fetch (attempt #%d) with status code: %d", attempt, response.StatusCode)
	}

	return err
}

err := retry.Retry(
	action,
	strategy.Limit(5),
	strategy.Backoff(backoff.Fibonacci(10*time.Millisecond)),
)

if err != nil {
	log.Fatalf("Failed to fetch repository with error %q", err)
}

Retry with backoff jitter

action := func(attempt uint) error {
	return errors.New("something happened")
}

seed := time.Now().UnixNano()
random := rand.New(rand.NewSource(seed))

retry.Retry(
	action,
	strategy.Limit(5),
	strategy.BackoffWithJitter(
		backoff.BinaryExponential(10*time.Millisecond),
		jitter.Deviation(random, 0.5),
	),
)

评论

暂无评论