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"
From = "username@163.com"
InsecureSkipVerify = true
Batch = 5
[Alerting]
TemplatesDir = "./etc/template"

View File

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

View File

@ -5,6 +5,7 @@ import (
"time"
"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"
)
@ -22,6 +23,8 @@ func Start(ctx context.Context) error {
go reportQueueSize()
go sender.StartEmailSender()
return nil
}

View File

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

View File

@ -67,19 +67,45 @@ func StartEmailSender() {
var s gomail.SendCloser
var open bool
var size int
for {
select {
case m, ok := <-mailch:
if !ok {
return
}
if !open {
s = dialSmtp(d)
open = true
}
if err := gomail.Send(s, m); err != nil {
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
// the last 30 seconds.
case <-time.After(30 * time.Second):