python-trio / python-trio/trio

SSL: optimize handling of packets/framing

Open
#196 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

polish TLS
Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

There are (at least) two subtle ways that our current SSL code is slightly inefficient:

  • Sometimes, it may happen the side the finishes the handshake is also the first to speak, and in this case it's slightly less overhead if the first data frame goes out in the same packet as the end of the handshake. (When this actually happens is complicated; it depends on which side speaks first, whether we're starting a new session or resuming and old one, and which version of TLS is in use.) What we could do is to have a kind of "cork" option, where if send_all triggers the handshake then it tells _retry to leave the last set of bytes-to-send in the buffer (if any), where they'll immediately get flushed out by the next call to _retry. (Note: this doesn't help if calling do_handshake explicitly; not sure there's much we can do about that.)

  • Currently, the user hands SSLStream.send_all chunks of data of whatever size, and then we let openssl break them into records. I believe that by default it will use the largest possible records (16 KiB). This is not necessarily the best approach. Even though ssl doesn't expose any specific knobs for this, I think each call to SSLObject.write has to generate a complete record, so we could do it by tracking the optimal record size and manually breaking data into pieces of that size before passing them to write. (Maybe with a bit of trickiness if we want to rejoin it all before calling send_all on the transport. Or maybe this doesn't really matter.)

    Also I guess if we're piggybacking on the end of a handshake, that should be taken into account when calculating the optimal size for the first record.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing SSLStream.send_all, _retry, do_handshake, and the SSLObject.write calls involved in handshake and record transmission. Establish how handshake output and application data are buffered, then define tests and completion criteria for handshake piggybacking and manually sized TLS records before attempting the optimization.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking, performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.