use batch send mail
This commit is contained in:
parent
b63c853889
commit
caa37b087c
|
@ -61,6 +61,7 @@ User = "username"
|
|||
Pass = "password"
|
||||
From = "username@163.com"
|
||||
InsecureSkipVerify = true
|
||||
Batch = 5
|
||||
|
||||
[Alerting]
|
||||
TemplatesDir = "./etc/template"
|
||||
|
|
|
@ -131,6 +131,7 @@ type SMTPConfig struct {
|
|||
Pass string
|
||||
From string
|
||||
InsecureSkipVerify bool
|
||||
Batch int
|
||||
}
|
||||
|
||||
type Alerting struct {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue