如本例所示,应用程序的邮件系统 运行 之类的东西是否应该在单独的频道中?

Should things like an app's mailer system run in a separate channel as shown in this example?

想象一下具有大量不同路由的 Web 服务。其中一些会触发发送给用户的交易电子邮件。初始化一个 mailer 实例似乎很奇怪,例如每次请求想要发送东西时使用 github.com/aws/aws-sdk-go/service/sns 的东西。

相反,我假设有一个 mailer 实例,并且一切都发生在一个单独的频道上,message 被发布到该频道。

例子

我创建了一个简单示例来说明问题。全局 Mailer 实例配置一次,Index 处理程序请求通道并传递 Message

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
)

// Message is the custom type used to pass the channel
type Message struct {
    To      string
    Subject string
    Body    string
}

// Mailer is responsible to send out emails
type Mailer struct{}

// send sends out the email
func (m *Mailer) send(message Message) {
    fmt.Printf("Sending email to:`%s`\nSubject: %s\n%s\n\n", message.To, message.Subject, message.Body)
}

// Messages returns the channel to which messages can be passed
func (m *Mailer) Messages() chan<- Message {
    cm := make(chan Message)

    go func() {
        msg := <-cm
        m.send(msg)

        close(cm)
    }()

    return cm
}

// mailer is a global var in this example, would probably be part of some
// sort of app context that's accessible from any handler.
//
// Note the mailer is NOT handler-scoped.
var mailer = Mailer{} // would this be thread-safe?

// Index handler
func Index(w http.ResponseWriter, r *http.Request) {
    m := Message{"email@example.com", fmt.Sprintf("visited `%s`", r.URL.Path[1:]), "Lorem ipsum"}
    mailer.Messages() <- m

    fmt.Fprintf(w, "Sent out email with subject line `%s`\n", m.Subject)
}

func main() {
    http.HandleFunc("/", Index)

    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    if err := http.ListenAndServe(":"+port, nil); err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

输出

访问 http://localhost:8080/hello-world 将呈现...

Sent out email with subject line `visited `hello-world``

… 和日志

sending email to `email@example.com`:
visited `hello-world`
Lorem ipsum

问题

  1. 这是正确的方法吗?
  2. 它是线程安全的吗?如果不是,如何让它成为线程安全的?

在这个例子中你并没有真正做任何事情,但是通过通道传递消息总是安全的——通道是语言中的基本并发原语之一。你让自己对竞争条件的可能性持开放态度,这取决于 send 实际最终做了什么。另一种处理方法是让 send 从单个频道接收。

type Mailer struct{
    Messages chan Message
}

func (m *Mailer) send() {
    for message := range m.Messages {
        fmt.Printf("Sending email to:`%s`\nSubject: %s\n%s\n\n", message.To, message.Subject, message.Body)
    }
}

var mailer *Mailer

func Index(w http.ResponseWriter, r *http.Request) {
    m := Message{"email@example.com", fmt.Sprintf("visited `%s`", r.URL.Path[1:]), "Lorem ipsum"}
    mailer.Messages <- m

    fmt.Fprintf(w, "Sent out email with subject line `%s`\n", m.Subject)
}


func main() {
    mailer = &Mailer{
        // buffer up to 100 message to be sent before blocking
        Messages: make(chan Message, 100),
    }
    // start the mailer send loop
    go mailer.send()

    ...