最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang函數(shù)重試機(jī)制實(shí)現(xiàn)代碼

 更新時間:2024年04月23日 10:14:21   作者:alden_ygq  
在編寫應(yīng)用程序時,有時候會遇到一些短暫的錯誤,例如網(wǎng)絡(luò)請求、服務(wù)鏈接終端失敗等,這些錯誤可能導(dǎo)致函數(shù)執(zhí)行失敗,這篇文章主要介紹了Golang函數(shù)重試機(jī)制實(shí)現(xiàn)代碼,需要的朋友可以參考下

前言

在編寫應(yīng)用程序時,有時候會遇到一些短暫的錯誤,例如網(wǎng)絡(luò)請求、服務(wù)鏈接終端失敗等,這些錯誤可能導(dǎo)致函數(shù)執(zhí)行失敗。
但是如果稍后執(zhí)行可能會成功,那么在一些業(yè)務(wù)場景下就需要重試了,重試的概念很簡單,這里就不做過多闡述了

最近也正好在轉(zhuǎn)golang語言,重試機(jī)制正好可以拿來練手,重試功能一般需要支持以下參數(shù)

  • execFunc:需要被執(zhí)行的重試的函數(shù)
  • interval:重試的間隔時長
  • attempts:嘗試次數(shù)
  • conditionMode:重試的條件模式,error和bool模式(這個參數(shù)用于控制傳遞的執(zhí)行函數(shù)返回值類型檢測

代碼

package retryimpl
import (
	"fmt"
	"time"
)
// RetryOptionV2 配置選項(xiàng)函數(shù)
type RetryOptionV2 func(retry *RetryV2)
// RetryFunc 不帶返回值的重試函數(shù)
type RetryFunc func() error
// RetryFuncWithData 帶返回值的重試函數(shù)
type RetryFuncWithData func() (any, error)
// RetryV2 重試類
type RetryV2 struct {
	interval time.Duration // 重試的間隔時長
	attempts int           // 重試次數(shù)
}
// NewRetryV2 構(gòu)造函數(shù)
func NewRetryV2(opts ...RetryOptionV2) *RetryV2 {
	retry := RetryV2{
		interval: DefaultInterval,
		attempts: DefaultAttempts,
	}
	for _, opt := range opts {
		opt(&retry)
	}
	return &retry
}
// WithIntervalV2 重試的時間間隔配置
func WithIntervalV2(interval time.Duration) RetryOptionV2 {
	return func(retry *RetryV2) {
		retry.interval = interval
	}
}
// WithAttemptsV2 重試的次數(shù)
func WithAttemptsV2(attempts int) RetryOptionV2 {
	return func(retry *RetryV2) {
		retry.attempts = attempts
	}
}
// DoV2 對外暴露的執(zhí)行函數(shù)
func (r *RetryV2) DoV2(executeFunc RetryFunc) error {
	fmt.Println("[Retry.DoV2] begin execute func...")
	retryFuncWithData := func() (any, error) {
		return nil, executeFunc()
	}
	_, err := r.DoV2WithData(retryFuncWithData)
	return err
}
// DoV2WithData 對外暴露知的執(zhí)行函數(shù)可以返回?cái)?shù)據(jù)
func (r *RetryV2) DoV2WithData(execWithDataFunc RetryFuncWithData) (any, error) {
	fmt.Println("[Retry.DoV2WithData] begin execute func...")
	n := 0
	for n < r.attempts {
		res, err := execWithDataFunc()
		if err == nil {
			return res, nil
		}
		n++
		time.Sleep(r.interval)
	}
	return nil, nil
}

測試驗(yàn)證

package retryimpl
import (
	"errors"
	"fmt"
	"testing"
	"time"
)
// TestRetryV2_DoFunc
func TestRetryV2_DoFunc(t *testing.T) {
	testSuites := []struct {
		exceptExecCount int
		actualExecCount int
	}{
		{exceptExecCount: 3, actualExecCount: 0},
		{exceptExecCount: 1, actualExecCount: 1},
	}
	for _, testSuite := range testSuites {
		retry := NewRetryV2(
			WithAttemptsV2(testSuite.exceptExecCount),
			WithIntervalV2(1*time.Second),
		)
		err := retry.DoV2(func() error {
			fmt.Println("[TestRetry_DoFuncBoolMode] was called ...")
			if testSuite.exceptExecCount == 1 {
				return nil
			}
			testSuite.actualExecCount++
			return errors.New("raise error")
		})
		if err != nil {
			t.Errorf("[TestRetryV2_DoFunc] retyr.DoV2 execute failed and err:%+v", err)
			continue
		}
		if testSuite.actualExecCount != testSuite.exceptExecCount {
			t.Errorf("[TestRetryV2_DoFunc] got actualExecCount:%v != exceptExecCount:%v", testSuite.actualExecCount, testSuite.exceptExecCount)
		}
	}
}
// TestRetryV2_DoFuncWithData
func TestRetryV2_DoFuncWithData(t *testing.T) {
	testSuites := []struct {
		exceptExecCount int
		resMessage      string
	}{
		{exceptExecCount: 3, resMessage: "fail"},
		{exceptExecCount: 1, resMessage: "ok"},
	}
	for _, testSuite := range testSuites {
		retry := NewRetryV2(
			WithAttemptsV2(testSuite.exceptExecCount),
			WithIntervalV2(1*time.Second),
		)
		res, err := retry.DoV2WithData(func() (any, error) {
			fmt.Println("[TestRetryV2_DoFuncWithData] DoV2WithData was called ...")
			if testSuite.exceptExecCount == 1 {
				return testSuite.resMessage, nil
			}
			return testSuite.resMessage, errors.New("raise error")
		})
		if err != nil {
			t.Errorf("[TestRetryV2_DoFuncWithData] retyr.DoV2 execute failed and err:%+v", err)
			continue
		}
		if val, ok := res.(string); ok && val != testSuite.resMessage {
			t.Errorf("[TestRetryV2_DoFuncWithData] got unexcept result:%+v", val)
			continue
		}
		t.Logf("[TestRetryV2_DoFuncWithData] got result:%+v", testSuite.resMessage)
	}
}

參考:GitCode - 開發(fā)者的代碼家園

到此這篇關(guān)于Golang函數(shù)重試機(jī)制實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Golang重試機(jī)制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go中阻塞以及非阻塞操作實(shí)現(xiàn)(Goroutine和main Goroutine)

    Go中阻塞以及非阻塞操作實(shí)現(xiàn)(Goroutine和main Goroutine)

    本文主要介紹了Go中阻塞以及非阻塞操作實(shí)現(xiàn)(Goroutine和main Goroutine),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Go語言中的匿名結(jié)構(gòu)體用法實(shí)例

    Go語言中的匿名結(jié)構(gòu)體用法實(shí)例

    這篇文章主要介紹了Go語言中的匿名結(jié)構(gòu)體用法,實(shí)例分析了匿名結(jié)構(gòu)體的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • 一些關(guān)于Go程序錯誤處理的相關(guān)建議

    一些關(guān)于Go程序錯誤處理的相關(guān)建議

    錯誤處理在每個語言中都是一項(xiàng)重要內(nèi)容,眾所周知,通常寫程序時遇到的分為異常與錯誤兩種,Golang中也不例外,這篇文章主要給大家介紹了一些關(guān)于Go程序錯誤處理的相關(guān)建議,需要的朋友可以參考下
    2021-09-09
  • go使用consul實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)及配置共享實(shí)現(xiàn)詳解

    go使用consul實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)及配置共享實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了go使用consul實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)及配置共享實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 詳解Golang并發(fā)控制的三種方案

    詳解Golang并發(fā)控制的三種方案

    本文主要介紹了詳解Golang并發(fā)控制的三種方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 詳解如何通過Go來操作Redis實(shí)現(xiàn)簡單的讀寫操作

    詳解如何通過Go來操作Redis實(shí)現(xiàn)簡單的讀寫操作

    作為最常用的分布式緩存中間件——Redis,了解運(yùn)作原理和如何使用是十分有必要的,今天來學(xué)習(xí)如何通過Go來操作Redis實(shí)現(xiàn)基本的讀寫操作,需要的朋友可以參考下
    2023-09-09
  • golang中context的作用詳解

    golang中context的作用詳解

    這篇文章主要介紹了golang中context的作用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Go語言利用excelize庫自動化操作Excel的實(shí)戰(zhàn)指南

    Go語言利用excelize庫自動化操作Excel的實(shí)戰(zhàn)指南

    這篇文章主要為大家詳細(xì)介紹了如何使用Go語言自動化操作Excel,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下吧
    2026-03-03
  • go實(shí)現(xiàn)自動復(fù)制U盤小工具demo

    go實(shí)現(xiàn)自動復(fù)制U盤小工具demo

    這篇文章主要為大家介紹了go實(shí)現(xiàn)自動復(fù)制U盤小工具demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Golang 文件操作:刪除指定的文件方式

    Golang 文件操作:刪除指定的文件方式

    這篇文章主要介紹了Golang 文件操作:刪除指定的文件方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04

最新評論

普兰店市| 成安县| 南澳县| 个旧市| 景洪市| 龙州县| 庐江县| 乐亭县| 墨江| 容城县| 宁蒗| 勐海县| 峡江县| 锡林浩特市| 十堰市| 花莲市| 弥勒县| 富锦市| 大英县| 天津市| 平南县| 屏边| 桓台县| 交城县| 海南省| 香港| 田阳县| 五台县| 蓬莱市| 仁化县| 友谊县| 鹤山市| 陇西县| 永泰县| 巨野县| 海晏县| 福州市| 江西省| 黄梅县| 内江市| 万盛区|