improved some of the tasks to use fewer goroutines

This commit is contained in:
2025-10-04 13:56:30 -06:00
parent 445f50a5c0
commit c7f5c57e82
5 changed files with 77 additions and 32 deletions
+23 -17
View File
@@ -131,22 +131,28 @@ func AmNewEmailMessage(sender int32, ip string) Message {
return rc
}
// recycleMessage cleans out a message and puts it back on the free list.
func recycleMessage(m *amMessage) {
m.from = ""
m.fromAddr = ""
m.to = make([]string, 0)
m.toAddrs = make([]string, 0)
m.cc = make([]string, 0)
m.bcc = make([]string, 0)
m.subject = ""
m.text = ""
for k := range m.headers {
delete(m.headers, k)
// The "recycle bin" for messages.
var messageRecycleBin chan *amMessage
// recycleMessages is a goroutine that recycles the messages on its queue.
func recycleMessages(messages chan *amMessage, done chan bool) {
for m := range messages {
m.from = ""
m.fromAddr = ""
m.to = make([]string, 0)
m.toAddrs = make([]string, 0)
m.cc = make([]string, 0)
m.bcc = make([]string, 0)
m.subject = ""
m.text = ""
for k := range m.headers {
delete(m.headers, k)
}
m.template = ""
for k := range m.vars {
delete(m.vars, k)
}
freeMessages.Put(m)
}
m.template = ""
for k := range m.vars {
delete(m.vars, k)
}
freeMessages.Put(m)
done <- true
}
+13 -8
View File
@@ -174,7 +174,7 @@ func senderLoop(sent chan *amMessage, done chan bool) {
} else {
log.Errorf("unable to format message: %v", err)
}
go recycleMessage(m)
messageRecycleBin <- m
}
done <- true // signal done for synchronization
}
@@ -182,9 +182,6 @@ func senderLoop(sent chan *amMessage, done chan bool) {
// sendChan is the channel we put E-mail messages on to be sent.
var sendChan chan *amMessage
// doneChan is the channel that gets signaled when the senderLoop breaks.
var doneChan chan bool
// SetupMailSender starts the mail-sending goroutine.
func SetupMailSender() func() {
// Initialize mail host and authentication.
@@ -207,12 +204,20 @@ func SetupMailSender() func() {
jet.DevelopmentMode(true),
)
// Start the recycler.
messageRecycleBin = make(chan *amMessage, 16)
doneChan1 := make(chan bool)
go recycleMessages(messageRecycleBin, doneChan1)
// Start the sender loop.
sendChan = make(chan *amMessage, 16)
doneChan = make(chan bool)
go senderLoop(sendChan, doneChan)
doneChan2 := make(chan bool)
go senderLoop(sendChan, doneChan2)
return func() {
close(sendChan) // will break the loop in senderLoop
<-doneChan // wait for routine to complete
close(sendChan)
<-doneChan2
close(messageRecycleBin)
<-doneChan1
}
}