dongque3797 2014-06-09 03:13
浏览 247
已采纳

Golang中延迟电子邮件发送到队列的最佳和最简单方法

Were running a simple web application on Amazon EC2, cassandra as the database.

It would be good if the user signup email could be deferred/queued, and maybe even rate limited.

What is the most "go like" way to achieve this without making things too complicated? I know there are lots of queue/task/worker like solutions, but I am more interested in something that is lightweight.

  • 写回答

2条回答 默认 最新

  • duanbei1598 2014-06-09 03:25
    关注

    Have 1 goroutine to handle the emails and communicate with it using channels, for example :

    var emailch = make(chan *UserInfo) 
    //you could make it buffered, that way it will block if requests are coming too fast
    //var emailch = make(chan *UserInfo, 1000)
    
    func init() {
        go func() {
            for ui := range emailch {
                send_email(ui.Email)
            }
        }()
    }
    
    func Register(rw http.ResponseWriter, req *http.Request) {
        //code
        emailch <- &UserInfo{....}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗