当前位置: 首页 > news >正文

关于茶叶的网站模板如何推广微信小程序

关于茶叶的网站模板,如何推广微信小程序,用什么程序做视频网站,成都旅游攻略详细前言 在编写应用程序时,有时候会遇到一些短暂的错误,例如网络请求、服务链接终端失败等,这些错误可能导致函数执行失败。 但是如果稍后执行可能会成功,那么在一些业务场景下就需要重试了,重试的概念很简单&#xff0c…
前言

在编写应用程序时,有时候会遇到一些短暂的错误,例如网络请求、服务链接终端失败等,这些错误可能导致函数执行失败。
但是如果稍后执行可能会成功,那么在一些业务场景下就需要重试了,重试的概念很简单,这里就不做过多阐述了

最近也正好在转golang语言,重试机制正好可以拿来练手,重试功能一般需要支持以下参数

  • execFunc:需要被执行的重试的函数
  • interval:重试的间隔时长
  • attempts:尝试次数
  • conditionMode:重试的条件模式,error和bool模式(这个参数用于控制传递的执行函数返回值类型检测

代码
package retryimplimport ("fmt""time"
)// RetryOptionV2 配置选项函数
type RetryOptionV2 func(retry *RetryV2)// RetryFunc 不带返回值的重试函数
type RetryFunc func() error// RetryFuncWithData 带返回值的重试函数
type RetryFuncWithData func() (any, error)// RetryV2 重试类
type RetryV2 struct {interval time.Duration // 重试的间隔时长attempts int           // 重试次数
}// NewRetryV2 构造函数
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 重试的次数
func WithAttemptsV2(attempts int) RetryOptionV2 {return func(retry *RetryV2) {retry.attempts = attempts}
}// DoV2 对外暴露的执行函数
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 对外暴露知的执行函数可以返回数据
func (r *RetryV2) DoV2WithData(execWithDataFunc RetryFuncWithData) (any, error) {fmt.Println("[Retry.DoV2WithData] begin execute func...")n := 0for n < r.attempts {res, err := execWithDataFunc()if err == nil {return res, nil}n++time.Sleep(r.interval)}return nil, nil
}
测试验证
package retryimplimport ("errors""fmt""testing""time"
)// TestRetryV2_DoFunc
func TestRetryV2_DoFunc(t *testing.T) {testSuites := []struct {exceptExecCount intactualExecCount 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 intresMessage      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 - 开发者的代码家园

http://www.yayakq.cn/news/61624/

相关文章:

  • 做跨境电商网站有哪些做网站表格
  • 定制型网站 成功案例韩国有哪些做潮牌的网站
  • 建设银行海门支行网站快速搭建网站前端
  • 常州网站外包网站不被收录了
  • 导购网站开发深圳工业设计机构
  • 扩展名网站wordpress显示栏目
  • 商标设计logo网站软件开发文档的需求分析
  • 鞋子 东莞网站建设有没有做外贸的网站啊
  • 越南人一般去哪个网站做贸易专门做网站搜索优化的公司
  • 东莞推广网站排名国家域名备案查询
  • 大学网站建设方案承德工程建设信息网站
  • vip视频网站怎么做校园网站建设网
  • 名创 网站建设宁波正规seo企业优化
  • 域名跟空间都有了怎么做网站wordpress京东客源码
  • 网站qq登录原理网站流媒体播放如何做
  • 大理州住房和城乡建设局网站如何把qq音乐导入到wordpress
  • 游戏交易网站开发wordpress获取所有图片
  • 新手怎么建立自己的网站网络运维工程师薪资待遇
  • 做网站需要营业执照嘛做窗帘网站图片大全
  • 丹徒网站建设报价西部数码网站管理助手安装
  • 蓝色科技企业网站模板网站建设维护是做什么的
  • phpcms 网站根目录ip池代理
  • 网站建设计入什么会计科目郑州做网站优化
  • 网站建设含义黄页网站推广效果怎么样
  • 网站页尾版权服务专业的网站开发设计
  • 物流网站开发策划网络营销与策划课程
  • 各大网站提交入口网站做浮动边框asp代码
  • 知名网站建设公司电话关键词搜索查询
  • 17. 整个网站建设中的关键是宜昌永东建设网站
  • 任丘市做网站价格企业seo推广外包