Skip to content

Make HTTP requests concurrently#232

Merged
kolanos merged 4 commits intoiopipe:masterfrom
kolanos:issue/222
May 21, 2018
Merged

Make HTTP requests concurrently#232
kolanos merged 4 commits intoiopipe:masterfrom
kolanos:issue/222

Conversation

@kolanos
Copy link
Copy Markdown
Contributor

@kolanos kolanos commented May 18, 2018

Closes #222

Signed-off-by: Michael Lavers kolanos@gmail.com

Closes iopipe#222

Signed-off-by: Michael Lavers <kolanos@gmail.com>
@kolanos kolanos requested a review from pselle May 18, 2018 23:07
Copy link
Copy Markdown
Contributor

@pselle pselle left a comment

Choose a reason for hiding this comment

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

This is cool! Have you run any manual tests to see the difference between the agent before and after this change re: performance?

self.handler.stream.close()
plugin['uploads'].append(self.signed_request['jwtAccess'])

def __del__(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The del isn't needed anymore because futures wait will close things if this unloads?

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.

Yes. The GC will also probably call __del__ before the thread is done with the stream, so this avoids a race condition as well.

Comment thread iopipe/iopipe.py

self.run_hooks('post:setup')

def __del__(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, so this (versus the comment in plugins) is where unloading will happen?

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.

So ThreadPoolExecutor maintains a thread pool, this ensures that the pool is shutdown gracefully, which includes waiting for any futures that might still be running. This should be rare as wait_for_futures will wait for futures after each invocation. The circumstance where __del__ would be engaged is when AWS Lambda is terminating a process.

Comment thread iopipe/signer.py
},
headers={
'Authorization': report.report['client_id']
'Authorization': token,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is easier to read, dig it

Comment thread setup.py
packages=find_packages(exclude=('tests', 'tests.*',)),
extras_require={
'dev': ['flake8', 'requests'],
'dev': ['jmespath>=0.7.1,<1.0.0', 'requests'],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You removed flake8 as a dev requirement? Is this why there're some flake8 changes? (there are a few diffs that are reorganization)

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.

It should be in tests_require, will add it back in there. The dev dependencies are meant for someone running IOpipe locally, which would not require flake8.

stream = iopipe.plugins[0].handler.stream

assert hasattr(stream, 'file')
assert stream.file.closed
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a way to test that the streams are closed at the end of the invocation with this new functionality?

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.

Because they're now closed in another thread, it is difficult to test. Will look into it.

Comment thread tests/test_system.py Outdated
@@ -1,5 +1,6 @@
import socket
import sys
import time
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unused import?

kolanos added 2 commits May 21, 2018 10:27
Signed-off-by: Michael Lavers <kolanos@gmail.com>
…letion

Signed-off-by: Michael Lavers <kolanos@gmail.com>
@kolanos
Copy link
Copy Markdown
Contributor Author

kolanos commented May 21, 2018

@pselle Regarding performance, making HTTP requests concurrently now means that the HTTP requests will now be as fast as the slowest request made. So if the agent is only sending a report, then there won't be much (if any) of a performance gain. But if the agent is sending a report, profile and logs -- then it will perform all three concurrently and will only block until the slowest request is complete. Since we're looking at adding a kitchen sink, this will greatly speed things up.

@pselle
Copy link
Copy Markdown
Contributor

pselle commented May 21, 2018

@kolanos Thanks, I understand how concurrency will benefit, I was asking if you'd done any testing with regards to "how much"

@kolanos kolanos self-assigned this May 21, 2018
@kolanos
Copy link
Copy Markdown
Contributor Author

kolanos commented May 21, 2018

@pselle I have benchmarked ThreadPoolExecutor relative to blocking I/O requests. Because these are network requests, there is no deterministic "it will be X% faster". But if you have three requests that take 10ms each, then the blocking version will take ~30ms and the thread pool version will take ~10ms. Also, with signed requests, we can initiate this request earlier in the invocation lifecycle without blocking, and as such it will be ready when the invocation needs it.

@kolanos kolanos requested review from ewindisch and pselle May 21, 2018 21:23
Signed-off-by: Michael Lavers <kolanos@gmail.com>
@kolanos kolanos merged commit 3842fdf into iopipe:master May 21, 2018
@kolanos kolanos deleted the issue/222 branch May 21, 2018 22:56
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