第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

應(yīng)用程序的郵件系統(tǒng)之類的東西是否應(yīng)該在一個單獨(dú)的頻道中運(yùn)行,如本例所示?

應(yīng)用程序的郵件系統(tǒng)之類的東西是否應(yīng)該在一個單獨(dú)的頻道中運(yùn)行,如本例所示?

Go
寶慕林4294392 2021-11-08 10:04:06
想象一個具有大量不同路由的 Web 服務(wù)。其中一些會觸發(fā)發(fā)送給用戶的交易電子郵件。初始化郵件程序?qū)嵗坪鹾芷婀?,例如github.com/aws/aws-sdk-go/service/sns每次請求想要發(fā)送某些東西時使用的東西。相反,我假設(shè)有一個郵件程序?qū)嵗?,并且一切都發(fā)生在一個單獨(dú)的頻道上,amessage被發(fā)布到該頻道。例子我創(chuàng)建了一個簡單的例子來說明這個問題。全局Mailer實(shí)例配置一次,Index處理程序請求一個通道并傳遞一個Message.package mainimport (    "fmt"    "log"    "net/http"    "os")// Message is the custom type used to pass the channeltype Message struct {    To      string    Subject string    Body    string}// Mailer is responsible to send out emailstype Mailer struct{}// send sends out the emailfunc (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 passedfunc (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 handlerfunc 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將呈現(xiàn)…發(fā)送主題為“visited `hello-world”的電子郵件...并記錄發(fā)送電子郵件到`email@example.com`:訪問了`hello-world`Lorem ipsum問題這是正確的方法嗎?它是線程安全的——如果不是,如何讓它成為線程安全的?
查看完整描述

1 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊

在這個例子中,你并沒有真正做任何事情,但是通過通道傳遞消息總是安全的——通道是語言中的基本并發(fā)原語之一。你讓自己對競爭條件的可能性持開放態(tài)度,這取決于send實(shí)際最終做什么。處理此問題的另一種方法是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()


    ...


查看完整回答
反對 回復(fù) 2021-11-08
  • 1 回答
  • 0 關(guān)注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號