Skip to content

Add Async IO support #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dszakallas
Copy link

@dszakallas dszakallas commented Nov 10, 2017

I added a proof of concept implementation for this lib whihc uses async http:
https://aiohttp.readthedocs.io/en/stable/index.html
I don't know yet how to include this code in a way that does not break interpreters below 3.5. I want to avoid putting it into a string and execing.

I am not very familiar with Python yet, so I appricieate any help on this. Thanks!

@dszakallas dszakallas force-pushed the async-support branch 2 times, most recently from 568df92 to 7b075cb Compare November 10, 2017 19:16

class SumoLogic(object):

def __init__(self, accessId, accessKey, endpoint=None, cookieFile='cookies.txt'):
Copy link
Author

@dszakallas dszakallas Nov 10, 2017

Choose a reason for hiding this comment

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

there are a couple of rough edges here:

  • cookies have to be added back if they are really needed
  • remove explicitly setting the read timeout to infinity. I don't why but while fetching results for some search jobs I frequently got TimeOutErrors. I think this should be configurable at least.

self.endpoint = str(response.url).replace('/collectors', '') # dirty hack to sanitise URI and retain domain

async def delete(self, method, params=None):
await self._guard_endpoint()
Copy link
Author

Choose a reason for hiding this comment

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

I did it this way because I felt async constructors are unidiomatic. Are they?

@@ -0,0 +1,162 @@
from copy import copy
Copy link
Author

Choose a reason for hiding this comment

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

I don't like it that the whole file is basically copied, but I couldn't find a better way. Is there?

response = await self.session.get('https://api.sumologic.com/api/v1/collectors') # Dummy call to get endpoint
self.endpoint = str(response.url).replace('/collectors', '') # dirty hack to sanitise URI and retain domain
async def _set_endpoint(self):
if self._endpoint is None:
Copy link
Author

Choose a reason for hiding this comment

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

double checked locking to avoid sending the request more than once


async def search_job(self, query, fromTime=None, toTime=None, timeZone='UTC'):
params = {'query': query, 'from': fromTime, 'to': toTime, 'timeZone': timeZone}
r = await self.post('/search/jobs', params)
return json.loads(await r.text())
async with r:
Copy link
Author

Choose a reason for hiding this comment

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

prevents leaking response objects

return json.loads(await r.text()), r.headers['etag']
async with r:
r = await self.get('/collectors/' + str(collector_id) + '/sources/' + str(source_id))
return json.loads(await r.text()), r.headers['etag']

async def create_source(self, collector_id, source):
return await self.post('/collectors/' + str(collector_id) + '/sources', source)
Copy link
Author

Choose a reason for hiding this comment

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

i cannot prevent leaking here without changing the API, as this returns the request object itself.

SumoLogic REST API endpoint changes based on the geo location of the client.
For example, If the client geolocation is Australia then the REST end point is
https://api.au.sumologic.com/api/v1
async def __aenter__(self):
Copy link
Author

Choose a reason for hiding this comment

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

for safely closing the connection

Copy link
Author

Choose a reason for hiding this comment

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

also it maybe wort exposing a close method.

async def search_metrics(self, query, fromTime=None, toTime=None, requestedDataPoints=600, maxDataPoints=800):
'''Perform a single Sumo metrics query'''

def millisectimestamp(ts):
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the common functions should be extracted to a util namespace to reduce code duplications.

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