Skip to content

Commit 9a07490

Browse files
Matt Whelankolanos
authored andcommitted
Greedily load the handler module. (#1)
It turns out that at cold start time, the CPU isn't so throttled. So that's the time to do big jobs like instrumenting crazy dependencies. So we should try to load all our user's deps up front, before their first invocation. Plus, the lazy-load strategy would have them paying for DB connection setup and stuff on first invoke, which is bad.
1 parent 9bd0144 commit 9a07490

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

python/newrelic_lambda_wrapper.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
newrelic.agent.initialize()
2020

21-
wrapped_handler = None
22-
2321

2422
class IOpipeNoOp(object):
2523
def __call__(self, *args, **kwargs):
@@ -33,12 +31,6 @@ def __getattr__(self, name):
3331
return IOpipeNoOp()
3432

3533

36-
@newrelic.agent.lambda_handler()
37-
def handler(event, context):
38-
context.iopipe = IOpipeNoOp()
39-
return get_wrapped_handler()(event, context)
40-
41-
4234
def get_handler():
4335
if (
4436
"NEW_RELIC_LAMBDA_HANDLER" not in os.environ
@@ -89,10 +81,12 @@ def get_handler():
8981
return handler
9082

9183

92-
def get_wrapped_handler():
93-
global wrapped_handler
84+
# Greedily load the handler during cold start, so we don't pay for it on first invoke
85+
wrapped_handler = get_handler()
86+
9487

95-
if not wrapped_handler:
96-
wrapped_handler = get_handler()
88+
@newrelic.agent.lambda_handler()
89+
def handler(event, context):
90+
context.iopipe = IOpipeNoOp()
91+
return wrapped_handler(event, context)
9792

98-
return wrapped_handler

0 commit comments

Comments
 (0)