use batch send mail
This commit is contained in:
parent
b63c853889
commit
caa37b087c
|
@ -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"
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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):
|
||||||
|
|
Loading…
Reference in New Issue