Skip to content

Commit

Permalink
Remove default arguments that resulted in heavyweight objects being c…
Browse files Browse the repository at this point in the history
…reated (#106)
  • Loading branch information
graebm authored Nov 26, 2019
1 parent 712c617 commit 8a35dc7
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion awscrt/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class HttpClientConnection(HttpConnectionBase):
def new(cls,
host_name,
port,
bootstrap,
socket_options=None,
tls_connection_options=None,
bootstrap=None,
proxy_options=None):
"""
Initiates a new connection to host_name and port using socket_options and tls_connection_options if supplied.
Expand Down
7 changes: 2 additions & 5 deletions awscrt/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,12 @@ def __init__(self, event_loop_group, max_hosts=16):
class ClientBootstrap(NativeResource):
__slots__ = ()

def __init__(self, event_loop_group, host_resolver=None):
def __init__(self, event_loop_group, host_resolver):
assert isinstance(event_loop_group, EventLoopGroup)
assert isinstance(host_resolver, HostResolverBase) or host_resolver is None
assert isinstance(host_resolver, HostResolverBase)

super(ClientBootstrap, self).__init__()

if host_resolver is None:
host_resolver = DefaultHostResolver(event_loop_group)

self._binding = _awscrt.client_bootstrap_new(event_loop_group, host_resolver)


Expand Down
13 changes: 10 additions & 3 deletions elasticurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ def print_header_list(headers):
# you only want one of these.
event_loop_group = io.EventLoopGroup(1)

host_resolver = io.DefaultHostResolver(event_loop_group)

# client bootstrap knows how to connect all the pieces. In this case it also has the default dns resolver
# baked in.
client_bootstrap = io.ClientBootstrap(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

url = urlparse(args.url)
port = 443
Expand Down Expand Up @@ -149,8 +151,13 @@ def on_incoming_body(http_stream, body_data):
socket_options.connect_timeout_ms = args.connect_timeout

hostname = url.hostname
connect_future = http.HttpClientConnection.new(hostname, port, socket_options,
tls_connection_options, client_bootstrap)
connect_future = http.HttpClientConnection.new(
host_name=hostname,
port=port,
socket_options=socket_options,
tls_connection_options=tls_connection_options,
bootstrap=client_bootstrap)

connection = connect_future.result(10)
connection.shutdown_future.add_done_callback(on_connection_shutdown)

Expand Down
3 changes: 2 additions & 1 deletion mqtt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def on_receive_message(topic, message):
# Run
args = parser.parse_args()
event_loop_group = io.EventLoopGroup(1)
client_bootstrap = io.ClientBootstrap(event_loop_group)
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

tls_options = None
if args.cert or args.key or args.root_ca:
Expand Down
3 changes: 2 additions & 1 deletion test/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def test_default_provider(self):
scoped_env = ScopedEnvironmentVariable('AWS_SHARED_CREDENTIALS_FILE', 'test/resources/credentials_test')

event_loop_group = awscrt.io.EventLoopGroup()
bootstrap = awscrt.io.ClientBootstrap(event_loop_group)
host_resolver = awscrt.io.DefaultHostResolver(event_loop_group)
bootstrap = awscrt.io.ClientBootstrap(event_loop_group, host_resolver)
provider = awscrt.auth.AwsCredentialsProvider.new_default_chain(bootstrap)

future = provider.get_credentials()
Expand Down
10 changes: 7 additions & 3 deletions test/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from __future__ import absolute_import
import awscrt.exceptions
from awscrt.http import HttpClientConnection, HttpClientStream, HttpHeaders, HttpProxyOptions, HttpRequest
from awscrt.io import TlsContextOptions, ClientTlsContext, TlsConnectionOptions
from awscrt.io import ClientBootstrap, ClientTlsContext, DefaultHostResolver, EventLoopGroup, TlsConnectionOptions, TlsContextOptions
from concurrent.futures import Future
from io import open # Python2's built-in open() doesn't return a stream
import os
Expand Down Expand Up @@ -107,8 +107,12 @@ def _new_client_connection(self, secure, proxy_options=None):
else:
tls_conn_opt = None

connection_future = HttpClientConnection.new(self.hostname,
self.port,
event_loop_group = EventLoopGroup()
host_resolver = DefaultHostResolver(event_loop_group)
bootstrap = ClientBootstrap(event_loop_group, host_resolver)
connection_future = HttpClientConnection.new(host_name=self.hostname,
port=self.port,
bootstrap=bootstrap,
tls_connection_options=tls_conn_opt,
proxy_options=proxy_options)
return connection_future.result(self.timeout)
Expand Down
4 changes: 0 additions & 4 deletions test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ def test_init(self):


class ClientBootstrapTest(NativeResourceTest):
def test_init_defaults(self):
event_loop_group = EventLoopGroup()
bootstrap = ClientBootstrap(event_loop_group)

def test_init(self):
event_loop_group = EventLoopGroup()
host_resolver = DefaultHostResolver(event_loop_group)
Expand Down
11 changes: 9 additions & 2 deletions test/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@

class MqttClientTest(NativeResourceTest):
def test_lifetime(self):
client = Client(ClientBootstrap(EventLoopGroup()))
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap)


class Config:
Expand Down Expand Up @@ -86,10 +89,14 @@ class MqttConnectionTest(NativeResourceTest):

def _test_connection(self):
config = Config.get()
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)

tls_opts = TlsContextOptions.create_client_with_mtls(config.cert, config.key)
tls = ClientTlsContext(tls_opts)
client = Client(ClientBootstrap(EventLoopGroup()), tls)

client = Client(bootstrap, tls)
connection = Connection(
client=client,
client_id=create_client_id(),
Expand Down

0 comments on commit 8a35dc7

Please sign in to comment.