Skip to content

WIP: Core mailroom manager; RequestHandler - #1960

Draft
benalleng wants to merge 3 commits into
payjoin:masterfrom
benalleng:core-mailroom-manager
Draft

benalleng wants to merge 3 commits into
payjoin:masterfrom
benalleng:core-mailroom-manager

Conversation

@benalleng

Copy link
Copy Markdown
Collaborator

This pulls the mailroom management logic into the core payjoin library to prevent footguns for implementers where possible.

To note this is a pretty big change in the sense that it adds quite a bit of io gated logic.

Coded and reviewed with help from GLM-5.3 the initial 2 commits are a direct grab from #1696, thanks for your contribution @bc1cindy!

Pull Request Checklist

Please confirm the following before requesting review:

@benalleng
benalleng force-pushed the core-mailroom-manager branch from 040bc40 to 8edde8a Compare October 9, 2026 16:03
@coveralls

coveralls commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 47439821406

Coverage decreased (-0.06%) to 88.758%

Details

  • Coverage decreased (-0.06%) from the base build.
  • Patch coverage: 31 uncovered changes across 3 files (335 of 366 lines covered, 91.53%).
  • 1 coverage regression across 1 file.

Uncovered Changes

File Changed Covered %
payjoin/src/core/request_handler/mod.rs 234 209 89.32%
payjoin/src/core/request_handler/exchange.rs 26 22 84.62%
payjoin-cli/src/app/v2/ohttp.rs 79 77 97.47%
Total (7 files) 366 335 91.53%

Coverage Regressions

1 previously-covered line in 1 file lost coverage.

File Lines Losing Coverage Coverage
payjoin-cli/src/app/mod.rs 1 92.75%

Coverage Stats

Coverage Status
Relevant Lines: 19072
Covered Lines: 16928
Line Coverage: 88.76%
Coverage Strength: 103.38 hits per line

💛 - Coveralls

@benalleng
benalleng force-pushed the core-mailroom-manager branch from 8edde8a to bdf1a94 Compare October 9, 2026 16:26
@benalleng

Copy link
Copy Markdown
Collaborator Author

The TRANSIENT_RETRY_DELAY const still lives in the cli for now. @xstoicunicornx do you think we should go ahead and pull this out too to get started on the discussions we had in #440

@xstoicunicornx

Copy link
Copy Markdown
Collaborator

The TRANSIENT_RETRY_DELAY const still lives in the cli for now. @xstoicunicornx do you think we should go ahead and pull this out too to get started on the discussions we had in #440

I think this const is simply a guard to not retry too quickly and not related to the discussion in #440 . I think this should be left in the cli.

Also, not sure how I feel about naming this mailroom when we already have a payjoin-mailroom crate. It also does not represent a mailroom when using the established terms for OHTTP. It think something like OHTTP request handler or even mailroom handler would be better. IMO the name should intuitively convey that this is used to interface with the mailroom and isn't the mailroom itself.

@benalleng

benalleng commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator Author

I think this const is simply a guard to not retry too quickly and not related to the discussion in #440 . I think this should be left in the cli.

Oh yeah I got it tied up with what that was all about. we can leave it alone. I was thinking about this old comment #1658 (comment) and just got all the timing stuff mixed up.

Noted about the naming

@benalleng

This comment was marked as resolved.

@DanGould

This comment was marked as resolved.

@DanGould

This comment was marked as resolved.

@benalleng
benalleng force-pushed the core-mailroom-manager branch 4 times, most recently from bbde7cf to efd47fb Compare October 15, 2026 15:57
@benalleng
benalleng force-pushed the core-mailroom-manager branch from efd47fb to 44430a1 Compare October 15, 2026 17:22

@xstoicunicornx xstoicunicornx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Another API thing other than the comment below is that shouldn't ohttp keys be stored per directory?

Only really reviewed the shape of the request handler so far but I think these are design questions we want to think through first.

Comment thread payjoin/src/core/request_handler.rs Outdated
/// Pass the session directory so a directory is never reached through
/// itself: a server serving as both relay and directory rejects the
/// self-loop request.
pub(crate) fn select_relay_excluding<R: Rng>(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about this API, its not quite intuitive what the exclusion is for and expecting implementers to appropriate add the right directory here feels open to misuse.

I kinda feel like if we are creating a request handler the directory should be set from the start rather than having the request handler also have to manage directories. But I also understand that a single request handler might want to handle multiple sessions that all have different directories.

Another issue that just occurred to me for this API is that for send sessions the payjoin directory might be one which doesn't exist in our directory list, and that is a case which we should still handle.

Maybe we can have a sub struct to allow requests specific to a directory? So you can have an API like RequestHandler::for_directory(directory_url).select_relay(rng)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Once a directory is selected for a session by the receiver it is exclusive as that is where the original POST lives. I am a little confused about the purpose of this for the sender.

@benalleng
benalleng force-pushed the core-mailroom-manager branch from 44430a1 to 7c681c5 Compare November 23, 2026 18:04
@benalleng
benalleng force-pushed the core-mailroom-manager branch 4 times, most recently from eabe5c4 to 4b4f43d Compare December 1, 2026 20:24
@benalleng
benalleng force-pushed the core-mailroom-manager branch 3 times, most recently from 1d68b41 to 267e89a Compare December 4, 2026 22:22
Every v2 implementation had to copy payjoin-cli's relay selection and
failover logic to talk to a directory safely. Move that logic into the
library as `payjoin::request_handler` so implementations use the same
logic instead of copying the reference code.

`RequestHandler` holds the configured relays and an optional
`RelayObserver`. It keeps no failure state, so one handler can be
shared across sessions. Each request tries relays in a fresh random
order and fails over on any error. A relay counts as successful only
once the session accepts its response, and a relay equal to the
session directory is skipped.

The module is available with `v2`. The sealed `RelayExchange` trait is
implemented for every session typestate that talks to the directory,
so applications with their own HTTP client can drive the failover loop
themselves. With `io`, `RequestHandler::post` runs that loop with
reqwest, enforcing a 60s timeout and the OHTTP response size cap, and
`fetch_ohttp_keys_from` falls back across candidate directories. `io`
gates this because it pulls in reqwest.

Add `directory()` accessors on `PjParam`, `Sender` and `Receiver` so
requests are always routed via the session's own directory.
`relay_succeeded` and `relay_failed` mark the relay and don't carry
logic so no mutation brings value.
payjoin-cli carried its own relay selection, failover and
`RelayExchange` implementation. Replace them with the library's
`RequestHandler` and drop the CLI copy.

`MailroomManager` now builds a `RequestHandler` from the config and
records relay outcomes for `relay-stats` through a `RelayObserver`.
Pinned OHTTP keys are set on the chosen directory with
`DirectoryHandle::with_ohttp_keys`, so no key fetch goes through a
relay. With `_manual-tls` the root certificate is read once at startup
and a bad path fails immediately. `http_agent` is now only needed by
v1, so gate it on that feature.
@benalleng
benalleng force-pushed the core-mailroom-manager branch from 267e89a to c722b7e Compare December 4, 2026 22:37

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants