Skip to content

Http2 keepalive - #5144

Merged
mhils merged 26 commits into
mitmproxy:mainfrom
EndUser509:http2_keepalive
Apr 22, 2022
Merged

mhils merged 26 commits into
mitmproxy:mainfrom
EndUser509:http2_keepalive

Conversation

@EndUser509

Copy link
Copy Markdown
Contributor

Description

This is a first PR for the implementation of the HTTP/2 keep-alive via ping as discussed in #5044 and the subsequent open issue #5046.

I am open about naming of options, classes and functions, just tell me what you would like. The RequestKeepAlive command can be extended with a version int to deal with future HTTP versions that require a similar strategy but other intervals and messages.

Questions that I am not sure:

  • Keeping connections alive leads to more concurrently open connections. On my iPad I managed to get beyond 1200 open file handles on my Linux mitmproxy. Is that an issue?
  • Is there any additional safety needed in case somebody toys with layers, like accepting but ignoring events or commands in other layers?

Checklist

  • I have updated tests where applicable.
  • I have added an entry to the CHANGELOG.

@EndUser509

Copy link
Copy Markdown
Contributor Author

Any ideas how get the additional test coverage. Just adding #pragma: no cover for all of them would be lame.

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks fantastic, thanks! 🍰

I don't have time for a proper review in the next few days, but a few high-level points:

  • The RequestKeepAlive/KeepAlive command/event pair is too specific. Let's aim for a generic timer functionality that can be reused in other protocols. For example, name it ScheduleWakeup/Wakeup or SetTimer/TimerExpired. The wakup event would then always be executed after the specified amount of seconds. In the HTTP/2 layer, we keep a timestamp of the last activity. If we are woken up while last_activity + http2_ping_threshold > time.time() (because something happened in the meantime), we just reschedule a new timer with the remaining time.
  • I think we should enable this by default with the same 58s setting as Firefox. There's no harm, is there?

@mhils

mhils commented Feb 18, 2022

Copy link
Copy Markdown
Member

Any ideas how get the additional test coverage. Just adding #pragma: no cover for all of them would be lame.

The idle watchdog would go away with the changes proposed in my previous comment. Let's focus on the test coverage of the HTTP/2 bits. :)

@EndUser509

Copy link
Copy Markdown
Contributor Author

I finally found some time to reorganize this in the way you requested. Only one missing piece:

 In the HTTP/2 layer, we keep a timestamp of the last activity.

I looked for time in the code but I could not find this timestamp. Can you point me out which variable it is?

@mhils

mhils commented Mar 15, 2022

Copy link
Copy Markdown
Member

I looked for time in the code but I could not find this timestamp. Can you point me out which variable it is?

My proposal was to do this, we aren't doing it yet. Sorry, that was a bit unclear. 😅

@mhils
mhils force-pushed the main branch 3 times, most recently from f0383ea to 7352811 Compare March 19, 2022 16:20
@EndUser509

EndUser509 commented Mar 20, 2022 •

Copy link
Copy Markdown
Contributor Author

OK, next try. I have restructured this as discussed, RequestWakeup now starts a one-shot timer that sends Wakeup. The timer restart logic is now all in _http2.py. I am not sure if the place where I update last_activity is the best choice but it seemed natural.
Making 58s idle PINGs the default breaks a couple of tests. I have not touched these yet.

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks awesome, thanks! 😃🍰
I've added a few comments below, let me know if anything is unclear. :)

Comment thread mitmproxy/options.py Resolved
Comment thread mitmproxy/options.py Resolved
Comment thread mitmproxy/proxy/commands.py Resolved
Comment thread mitmproxy/proxy/commands.py Resolved
Comment thread mitmproxy/proxy/layers/http/__init__.py Resolved
Comment thread mitmproxy/proxy/layers/http/_http2.py Resolved
Comment thread mitmproxy/proxy/server.py Resolved
<< OpenConnection(server)
>> reply(None, side_effect=make_h2)
<< RequestWakeup(1)
<< SendData(server, initial)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should emulate a Wakeup event here and make sure that it triggers a PING. :-)

def test_request_keepalive(monkeypatch, tctx):
    ...
    assert (playbook ...)
    t = time.time()
    monkeypatch.setattr(time, "time", lambda: t + 60)
    assert (playbook >> Wakeup() ...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll do this at the very end, when the rest is working. :)

@EndUser509

Copy link
Copy Markdown
Contributor Author

The update should implement the proper routing of the Wakeup event to the right client. I have used this in production for a week and observed no crashes, errors or other problems.
Quite some tests for HTTP/2 are broken due to the additional WakeupRequest command and I am not sure whether I should just disable PING for all test cases except for the ones explicitly testing the functionality. Or fix all broken tests.

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks awesome now! 💯
I only have some comments that should be very easy to implement.

Quite some tests for HTTP/2 are broken due to the additional WakeupRequest command and I am not sure whether I should just disable PING for all test cases except for the ones explicitly testing the functionality. Or fix all broken tests.

I think just disabling it for those tests is totally fine. 👍 I'm happy with either approach though, whatever you find is easier. 😃

Comment thread mitmproxy/proxy/layers/http/_http2.py Resolved
Comment thread mitmproxy/proxy/commands.py Resolved
Comment thread mitmproxy/proxy/server.py Resolved
Comment thread mitmproxy/proxy/server.py Resolved
Comment thread mitmproxy/proxy/server.py Resolved
EndUser509 and others added 4 commits April 10, 2022 09:50
Co-authored-by: Maximilian Hils <[email protected]>
Co-authored-by: Maximilian Hils <[email protected]>
Co-authored-by: Maximilian Hils <[email protected]>
Fixed almost all tests
@EndUser509

Copy link
Copy Markdown
Contributor Author

OK, next update. I had to add assert to wakeup in server.py to get rid of type missmatches involving Optional. Maybe you have a more elegant solution again.
Can you please look at the remaining errors?

  • The error about set[asyncio.Task] only affects 3.8 and the code is valid in 3.9 and newer (PEP 585)
  • The coverage errors are for the initializers of Wakeup and RequestWakeup, both are instantiated during the tests so I do not see what I can do here.
  • I am struggling to get the test case to run with the monkeypatched time. Any ideas?

@mhils

mhils commented Apr 13, 2022

Copy link
Copy Markdown
Member

OK, next update. I had to add assert to wakeup in server.py to get rid of type missmatches involving Optional. Maybe you have a more elegant solution again.

No, that's fine. :)

  • I am struggling to get the test case to run with the monkeypatched time. Any ideas?

Do you want to push what you have/what is not working so far? I'm busy with family stuff over easter, but happy to look at this (and your other questions) next week. :)

@EndUser509

Copy link
Copy Markdown
Contributor Author

No need to hurry. :) My sample test is already in the commit, the last one in test_http2.py. All other tests are fixed.
I don't understand the test harness too well, but I assume that the RequestWakeup instance that is expected first needs to be the same that is again referenced in the Wakeup coming out. But that does not work.

mhils added 2 commits April 22, 2022 13:34
This allows us to use it with `reply()` in tests,
which makes sure that the correct instance is reused.
 # Please enter the commit message for your changes. Lines starting
`typing.Set` for Python 3.8 compatibility and a few minor stylistic changes.
@mhils
mhils force-pushed the http2_keepalive branch from 48c5856 to bece08f Compare April 22, 2022 11:40

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to merge now, thank you very much again! 🍰 🚀 😃

@mhils
mhils merged commit 35703b0 into mitmproxy:main Apr 22, 2022
@mhils mhils mentioned this pull request Apr 23, 2022
12 of 14 tasks
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.

2 participants