Skip to content
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

Lazy connections #2

Merged
merged 2 commits into from Aug 5, 2016
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Now lazy-loading s3 connection and s3 buckets.

Making these newtork calls proactively in our constructors is not a good
practice. Constructors should only configure the object for use and NOT
actually instantiate non-config object properties.

Perfect case for lazy-loading is testing. Unless the object is heavily
mocked these network calls would 1) require a network connection for
testing even if the object is only instanted in the test and not used
and 2) slow down tests due to network calls.
  • Loading branch information
Scott Griffin
Scott Griffin committed May 13, 2015
commit 60be4fcfa8007f65fbed83e2bf80d6dd7f79ab8f
@@ -1,7 +1,6 @@
"""
" Copyright: Loggly
" Author: Scott Griffin
" Last Updated: 02/22/2013
"
"""
try: import simplejson as json
@@ -83,12 +82,25 @@ class S3Producer( BaseProducer ):

def __init__(self, **kwargs):
super( S3Producer, self ).__init__(**kwargs)
self.s3conn = boto.connect_s3( self.config.s3_key, self.config.s3_secret )

self.bucket = self.s3conn.lookup( self.config.s3_bucket )
self._s3conn = None
self._bucket = None

@property
def s3conn(self):
if self._s3conn is None:
self._s3conn = boto.connect_s3( self.config.s3_key, self.config.s3_secret )
return self._s3conn

@property
def bucket(self):
if self._bucket is None:
self._bucket = self.s3conn.lookup( self.config.s3_bucket )

if not self.bucket:
self.bucket = self.s3conn.create_bucket( self.config.s3_bucket )
if not self._bucket:
self._bucket = self.s3conn.create_bucket( self.config.s3_bucket )

return self._bucket


def send( self, msg, **kwargs ):
"""
@@ -1,7 +1,6 @@
"""
" Copyright: Loggly
" Author: Scott Griffin
" Last Updated: 02/22/2013
"
" This class provides the ability to register a function as
" a consumer to an S3 routing_key. This class also handles tracking
@@ -74,8 +73,8 @@ class S3Consumer(object):
def __init__(self, routing_key, func, name=None, config='config.py'):

self.config = config_loader( config )
self.s3conn = boto.connect_s3( self.config.s3_key, self.config.s3_secret )
self.bucket = self.s3conn.get_bucket( self.config.s3_bucket )
self._s3conn = None
self._bucket = None
self.routing_key = routing_key.upper()
self.callback = func

@@ -90,6 +89,18 @@ def __init__(self, routing_key, func, name=None, config='config.py'):
location=self.config.s3_cursor['location']
)


@property
def s3conn(self):
if self._s3conn is None:
self._s3conn = boto.connect_s3( self.config.s3_key, self.config.s3_secret )
return self._s3conn

@property
def bucket(self):
if self._bucket is None:
self._bucket = self.s3conn.get_bucket( self.config.s3_bucket )
return self._bucket

def _gen_name(self, func):
""" Generates a cursor name so that the cursor can be re-attached to """
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.