Go-异常重试

文章目录

项目中常有发送短信发送邮件站内信等应用。由网络、处理超时等引发发送失败,常规的做法是让它重试几次 :

代码实现

// 短信
type Sms struct {
    Title /*标题*/, From /*发送者*/, To /*接收者*/, Content /*消息内容*/ string
}

// 模拟发短信
func (p *Sms) Send() error {
    if (time.Now().Second())%5 == 0 {
        log.Printf("【%s】向【%s】发送了一条短信:「%s」%s\n", p.From, p.To, p.Title, p.Content)
        return nil
    }
    return errors.New("( ¯▽¯;) 网络异常( ¯▽¯;)  ")
}

// 发送短信(失败重试)
//  count: 尝试发送次数
//  interval: 第一次失败重试间隔时间(后续间隔翻倍)
func (p *Sms) SendWithRetry(count uint8, interval time.Duration) {
    if err := p.Send(); err != nil {
        log.Println(err.Error())
        if count--; count > 0 {
            log.Printf("短信发送失败,%s后再次重试 #%d. \n", interval, count)
            <-time.After(interval)
            p.SendWithRetry(count, interval*2)
        }
    }
}

func TestRetry(t *testing.T) {
    sms := &Sms{Title: "今晚吃鸡", From: "Lucy", To: "Lily", Content: "八点半开黑"}
    sms.SendWithRetry(5, time.Second*2)
}

带有取消的重试


// 自定义Stop错误
type Stop struct {
    error
}
func NewStop(msg string) Stop {
    return Stop{errors.New(msg)}
}

// 短信
type Sms struct {
    Title /*标题*/, From /*发送者*/, To /*接收者*/, Content /*消息内容*/ string
}

// 模拟发短信
func (p *Sms) Send() error {
    if (time.Now().Second())%7 == 0 {
        log.Printf("【%s】向【%s】发送了一条短信:「%s」%s\n", p.From, p.To, p.Title, p.Content)
        return nil
    }
    //
    if (time.Now().Second())%9 == 0 {
        return NewStop("已取消重试")
    }
    return errors.New("( ¯▽¯;) 网络异常( ¯▽¯;)  ")
}

// 发送短信(失败重试)
//  count: 尝试发送次数
//  interval: 第一次失败重试间隔时间(后续间隔翻倍)
func (p *Sms) SendWithRetry(count uint8, interval time.Duration) {
    if err := p.Send(); err != nil {
        if stop, ok := err.(Stop); ok {
            log.Println(stop.Error())
            return
        }

        log.Println(err.Error())
        if count--; count > 0 {
            log.Printf("短信发送失败,%s后再次重试 #%d. \n", interval, count)
            <-time.After(interval)
            p.SendWithRetry(count, interval)
        }
    }
}

func TestRetry(t *testing.T) {
    sms := &Sms{Title: "今晚吃鸡", From: "Lucy", To: "Lily", Content: "八点半开黑"}
    sms.SendWithRetry(5, time.Second*2)
}

引用:
https://github.com/mnhkahn/gogogo/blob/master/util/retry_util.go

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值