美文网首页
chan实现控制速率

chan实现控制速率

作者: 斯嘎啦 | 来源:发表于2018-09-21 10:07 被阅读0次
    MailWorkerChan := make(chan int, workerConfig.Mail)
    
    func SendMailList(L []*model.Mail) {
        for _, mail := range L {
            MailWorkerChan <- 1
            go SendMail(mail)
        }
    }
    
    func SendMail(mail *model.Mail) {
        defer func() {
            <-MailWorkerChan
        }()
    
        url := g.Config().Api.Mail
        r := httplib.Post(url).SetTimeout(5*time.Second, 30*time.Second)
        r.Param("tos", mail.Tos)
        r.Param("subject", mail.Subject)
        r.Param("content", mail.Content)
        resp, err := r.String()
        if err != nil {
            log.Errorf("send mail fail, receiver:%s, subject:%s, cotent:%s, error:%v", mail.Tos, mail.Subject, mail.Content, err)
        }
    
        log.Debugf("send mail:%v, resp:%v, url:%s", mail, resp, url)
    }
    
    

    该代码节选自open-falconalarm模块中发送邮件的部分,主要通过MailWorkerChan实现控制速率。

    相关文章

      网友评论

          本文标题:chan实现控制速率

          本文链接:https://www.haomeiwen.com/subject/vscanftx.html