Open iredmail opened 1 year ago
Cleanup()
ist the right place. Abort()
e.g. also gets called after the client of the MTA sessions issued a STARTTLS
so not at the end of the transaction.
Cleanup()
might in deed be called multiple times – but it is guranteed to be at least called once for each SMTP transaction. Since your milter backend is only valid for one transaction, just set a boolean
func (b *MyBackend) Cleanup() {
if b.cleanupCalled {
return
}
b.cleanupCalled = true
// do cleanup
}
Thanks for the suggestion. Is it better add a new method to be called ONCE after processed?
On second thought, Cleanup()
is called at the end of the SMTP session (or when a SMFIC_QUIT_NC
message is sent, or when the connection to the MTA fails). An SMTP session can have multiple messages.
So Cleanup()
could log that the SMTP session has ended, but not that a single message has been processed.
The start and end of a message has to be figured out in the Milter backend. Have a look at https://github.com/d--j/go-milter/blob/main/mailfilter/backend.go to see how the mailfilter Backend does this.
Cleanup()
is for cleaning up resources you may have opened for this SMTP transaction, e.g.:
Can we add a new method which will be called ONCE after a single message has been processed?
For easy maintenance and troubleshooting, Milter server must log the final response and basic info of each message, so this method will be very neat and make milter development easier. :)
Maybe one more method for end of smtp transaction (and called just once)?
Checked session.go
, Cleanup()
is called in 4 places:
case wire.CodeQuitNewConn
: https://github.com/d--j/go-milter/blob/115badd800c854f60252bd087533e0d12a5f4525/session.go#L308-L310case wire.CodeQuit
: https://github.com/d--j/go-milter/blob/115badd800c854f60252bd087533e0d12a5f4525/session.go#L316-L317!resp.Continue()
: https://github.com/d--j/go-milter/blob/115badd800c854f60252bd087533e0d12a5f4525/session.go#L393-L394m.backend != nil
: https://github.com/d--j/go-milter/blob/115badd800c854f60252bd087533e0d12a5f4525/session.go#L329-L333Seems none of them can precisely match each processed message. I suggest adding a new method to be called at the end of each processed message.
Sorry for beeing a little bit slow in understanding – can you explain why you need something other than EndOfMessage()
?
https://github.com/d--j/go-milter/blob/30d4d62396d57db43116e28f8bb5c5b61ecf2478/server.go#L69-L75
In case the milter backend opts to reject the message before this callback, the milter backend already knows that the current message has ended, why should the serverSession
need to tell the backend what the backend already told it?
EndOfMessage
is not applicable at all, for example:
Connect
, Helo
, MailFrom
, RcptTo
state.There's no email message submitted at all, hence it doesn't go through EndOfMessage
, so it doesn't trigger the logging.
Hello @iredmail
Please have a look at PR #17 – I introduced a new method ConnectionClosed
that gets called when the the connection to the MTA was closed. Hopefully this might be useful for your logging purposes.
I tried to log final response of processed message in
Abort()
,Cleanup()
, but they both got called 2 or more times for one message.So the question is, where should i log final response of processed message? and it should be called only once.