Skip to content

Commit

Permalink
feat: Support unix sockets for urllib3
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Oct 26, 2018
1 parent 37f3142 commit b695ad7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/sentry/net/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

import socket
from socket import error as SocketError, timeout as SocketTimeout

from requests import Session as _Session
Expand Down Expand Up @@ -149,3 +150,26 @@ def __init__(self):
adapter = BlacklistAdapter()
self.mount('https://', adapter)
self.mount('http://', adapter)


class UnixHTTPConnection(HTTPConnection):
def __init__(self, host, **kwargs):
# We're using the `host` as the socket path, but
# urllib3 uses this host as the Host header by default.
# If we send along the socket path as a Host header, this is
# never what you want and would typically be malformed value.
# So we fake this by sending along `localhost` by default as
# other libraries do.
self.socket_path = host
super(UnixHTTPConnection, self).__init__(host='localhost', **kwargs)

def _new_conn(self):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
if self.timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
sock.settimeout(self.timeout)
sock.connect(self.socket_path)
return sock


class UnixHTTPConnectionPool(HTTPConnectionPool):
ConnectionCls = UnixHTTPConnection
5 changes: 5 additions & 0 deletions src/sentry/nodestore/riak/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from urllib3.connection import HTTPConnection
from urllib3.exceptions import HTTPError

from sentry.net.http import UnixHTTPConnectionPool


DEFAULT_NODES = ({'host': '127.0.0.1', 'port': 8098}, )


Expand Down Expand Up @@ -257,6 +260,8 @@ def create_pool(self, host):
addr = host.get('host', '127.0.0.1')
port = int(host.get('port', 8098))
secure = host.get('secure', False)
if host[:1] == '/':
connection_cls = UnixHTTPConnectionPool
if not secure:
connection_cls = HTTPConnectionPool
else:
Expand Down

0 comments on commit b695ad7

Please sign in to comment.