Skip to content

Improve mail transmission perf, invoice creation#32

Merged
nspo merged 1 commit intomasterfrom
feature/mail_tx_perf
Feb 4, 2026
Merged

Improve mail transmission perf, invoice creation#32
nspo merged 1 commit intomasterfrom
feature/mail_tx_perf

Conversation

@nspo
Copy link
Copy Markdown
Owner

@nspo nspo commented Jan 25, 2026

Previously, pybarsys opened a new connection for each mail. With this change, it (for the most part) opens one connection and keeps that open for all mails.
This seems to improve perf by roughly 10x in some
simple tests, making it less frustrating to generate a lot of invoices.

If there is an error during mail tx, new invoices now are automatically deleted to not make it seem like they were delivered to the user.

There was a small validation bug which made it impossible to create a new user for which someone else has to pay in one go (how was this never a bug report?!).

Updated some dependency to make it compatible with recent Python versions. Actually, a lot more updates are needed...

@nspo
Copy link
Copy Markdown
Owner Author

nspo commented Jan 25, 2026

fyi @Merlijnv

@nspo nspo force-pushed the feature/mail_tx_perf branch from 7d561ea to 2ab87bb Compare January 27, 2026 13:01
@nspo nspo linked an issue Jan 30, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Collaborator

@jallmenroeder jallmenroeder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but I did not test it myself. Maybe there's further improvement in submitting a list of all mails to the send_messages() function, instead of one message at a time. But I'm not sure if it's worth it.

@nspo
Copy link
Copy Markdown
Owner Author

nspo commented Jan 31, 2026

Thanks!

Maybe there's further improvement in submitting a list of all mails to the send_messages() function, instead of one message at a time. But I'm not sure if it's worth it.

I don't think that should make a real perf difference because send_messages itself is just a for loop: https://github.com/django/django/blob/674eda1c03a3187905f48afee0f15226aa62fdf3/django/core/mail/backends/smtp.py#L137

and the heavy stuff in that function (establish connection) is already done/skipped with each call

I agree that a more extensive refactoring could have been nice though. But one disadvantage would've been that you just get one error for all of the (potentially) sent mails and don't know which were delivered and which were not. So it'd be an issue to decide whether you delete the local invoices if an error occurs because maybe 49/50 invoices were delivered correctly but the last one failed. Edit: never mind, the function returns the number of successfully sent mails 🙈 didn't see that when writing the code. But the first argument still stands

Fixes issue #31

Previously, pybarsys opened a new connection for each
mail. With this change, it (for the most part) opens
one connection and keeps that open for all mails.
This seems to improve perf by roughly 10x in some
simple tests, making it less frustrating to generate
a lot of invoices.

If there is an error during mail tx, new invoices now
are automatically deleted to not make it seem like they
were delivered to the user.

There was a small validation bug which made it impossible
to create a new user for which someone else has to pay
in one go (how was this never a bug report?!).

Updated some dependency to make it compatible with
recent Python versions. Actually, a lot more updates
are needed...
@nspo nspo force-pushed the feature/mail_tx_perf branch from 2ab87bb to 21ace07 Compare February 4, 2026 18:06
@nspo nspo merged commit ec38c3f into master Feb 4, 2026
2 checks passed
@nspo nspo deleted the feature/mail_tx_perf branch February 4, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve shipping of mails

2 participants