use batch send mail

This commit is contained in:
Ulric Qin 2022-03-01 13:44:46 +08:00
parent b63c853889
commit caa37b087c
5 changed files with 32 additions and 1 deletions

View File

@ -61,6 +61,7 @@ User = "username"
Pass = "password" Pass = "password"
From = "username@163.com" From = "username@163.com"
InsecureSkipVerify = true InsecureSkipVerify = true
Batch = 5
[Alerting] [Alerting]
TemplatesDir = "./etc/template" TemplatesDir = "./etc/template"

View File

@ -131,6 +131,7 @@ type SMTPConfig struct {
Pass string Pass string
From string From string
InsecureSkipVerify bool InsecureSkipVerify bool
Batch int
} }
type Alerting struct { type Alerting struct {

View File

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/didi/nightingale/v5/src/server/config" "github.com/didi/nightingale/v5/src/server/config"
"github.com/didi/nightingale/v5/src/server/sender"
promstat "github.com/didi/nightingale/v5/src/server/stat" promstat "github.com/didi/nightingale/v5/src/server/stat"
) )
@ -22,6 +23,8 @@ func Start(ctx context.Context) error {
go reportQueueSize() go reportQueueSize()
go sender.StartEmailSender()
return nil return nil
} }

View File

@ -175,7 +175,7 @@ func handleNotice(notice Notice, bs []byte) {
content = "mailbody.tpl not found" content = "mailbody.tpl not found"
} }
sender.SendEmail(subject, content, StringSetKeys(emailset)) sender.WriteEmail(subject, content, StringSetKeys(emailset))
case "dingtalk": case "dingtalk":
if len(dingtalkset) == 0 { if len(dingtalkset) == 0 {
continue continue

View File

@ -67,19 +67,45 @@ func StartEmailSender() {
var s gomail.SendCloser var s gomail.SendCloser
var open bool var open bool
var size int
for { for {
select { select {
case m, ok := <-mailch: case m, ok := <-mailch:
if !ok { if !ok {
return return
} }
if !open { if !open {
s = dialSmtp(d) s = dialSmtp(d)
open = true open = true
} }
if err := gomail.Send(s, m); err != nil { if err := gomail.Send(s, m); err != nil {
logger.Errorf("email_sender: failed to send: %s", err) logger.Errorf("email_sender: failed to send: %s", err)
// close and retry
if err := s.Close(); err != nil {
logger.Warningf("email_sender: failed to close smtp connection: %s", err)
}
s = dialSmtp(d)
open = true
if err := gomail.Send(s, m); err != nil {
logger.Errorf("email_sender: failed to retry send: %s", err)
}
} }
size++
if size >= conf.Batch {
if err := s.Close(); err != nil {
logger.Warningf("email_sender: failed to close smtp connection: %s", err)
}
open = false
size = 0
}
// Close the connection to the SMTP server if no email was sent in // Close the connection to the SMTP server if no email was sent in
// the last 30 seconds. // the last 30 seconds.
case <-time.After(30 * time.Second): case <-time.After(30 * time.Second):