Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Use Twisted-15.2.1, Use Agent.usingEndpointFactory rather than implem…
Browse files Browse the repository at this point in the history
…ent our own Agent
  • Loading branch information
NegativeMjark committed Jun 1, 2015
1 parent afbd3b2 commit 90abdaf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 51 deletions.
75 changes: 25 additions & 50 deletions synapse/http/matrixfederationclient.py
Expand Up @@ -16,7 +16,7 @@

from twisted.internet import defer, reactor, protocol
from twisted.internet.error import DNSLookupError
from twisted.web.client import readBody, _AgentBase, _URI, HTTPConnectionPool
from twisted.web.client import readBody, HTTPConnectionPool, Agent
from twisted.web.http_headers import Headers
from twisted.web._newclient import ResponseDone

Expand Down Expand Up @@ -53,41 +53,17 @@
)


class MatrixFederationHttpAgent(_AgentBase):

def __init__(self, reactor, pool=None):
_AgentBase.__init__(self, reactor, pool)

def request(self, destination, endpoint, method, path, params, query,
headers, body_producer):

outgoing_requests_counter.inc(method)

host = b""
port = 0
fragment = b""

parsed_URI = _URI(b"http", destination, host, port, path, params,
query, fragment)

# Set the connection pool key to be the destination.
key = destination

d = self._requestWithEndpoint(key, endpoint, method, parsed_URI,
headers, body_producer,
parsed_URI.originForm)

def _cb(response):
incoming_responses_counter.inc(method, response.code)
return response

def _eb(failure):
incoming_responses_counter.inc(method, "ERR")
return failure
class MatrixFederationEndpointFactory(object):
def __init__(self, hs):
self.tls_context_factory = hs.tls_context_factory

d.addCallbacks(_cb, _eb)
def endpointForURI(self, uri):
destination = uri.netloc

return d
return matrix_federation_endpoint(
reactor, destination, timeout=10,
ssl_context_factory=self.tls_context_factory
)


class MatrixFederationHttpClient(object):
Expand All @@ -105,10 +81,17 @@ def __init__(self, hs):
self.server_name = hs.hostname
pool = HTTPConnectionPool(reactor)
pool.maxPersistentPerHost = 10
self.agent = MatrixFederationHttpAgent(reactor, pool=pool)
self.agent = Agent.usingEndpointFactory(
reactor, MatrixFederationEndpointFactory(hs), pool=pool
)
self.clock = hs.get_clock()
self.version_string = hs.version_string

def _create_url(self, destination, path_bytes, param_bytes, query_bytes):
return urlparse.urlunparse(
("matrix", destination, path_bytes, param_bytes, query_bytes, "")
)

@defer.inlineCallbacks
def _create_request(self, destination, method, path_bytes,
body_callback, headers_dict={}, param_bytes=b"",
Expand All @@ -119,8 +102,8 @@ def _create_request(self, destination, method, path_bytes,
headers_dict[b"User-Agent"] = [self.version_string]
headers_dict[b"Host"] = [destination]

url_bytes = urlparse.urlunparse(
("", "", path_bytes, param_bytes, query_bytes, "",)
url_bytes = self._create_url(
destination, path_bytes, param_bytes, query_bytes
)

logger.info("Sending request to %s: %s %s",
Expand All @@ -139,22 +122,20 @@ def _create_request(self, destination, method, path_bytes,
# (once we have reliable transactions in place)
retries_left = 5

endpoint = self._getEndpoint(reactor, destination)
http_url_bytes = urlparse.urlunparse(
("", "", path_bytes, param_bytes, query_bytes, "")
)

while True:
producer = None
if body_callback:
producer = body_callback(method, url_bytes, headers_dict)
producer = body_callback(method, http_url_bytes, headers_dict)

try:
request_deferred = preserve_context_over_fn(
self.agent.request,
destination,
endpoint,
method,
path_bytes,
param_bytes,
query_bytes,
url_bytes,
Headers(headers_dict),
producer
)
Expand Down Expand Up @@ -442,12 +423,6 @@ def body_callback(method, url_bytes, headers_dict):

defer.returnValue((length, headers))

def _getEndpoint(self, reactor, destination):
return matrix_federation_endpoint(
reactor, destination, timeout=10,
ssl_context_factory=self.hs.tls_context_factory
)


class _ReadBodyToFileProtocol(protocol.Protocol):
def __init__(self, stream, deferred, max_size):
Expand Down
2 changes: 1 addition & 1 deletion synapse/python_dependencies.py
Expand Up @@ -19,7 +19,7 @@

REQUIREMENTS = {
"syutil>=0.0.6": ["syutil>=0.0.6"],
"Twisted==14.0.2": ["twisted==14.0.2"],
"Twisted==15.2.1": ["twisted==15.2.1"],
"service_identity>=1.0.0": ["service_identity>=1.0.0"],
"pyopenssl>=0.14": ["OpenSSL>=0.14"],
"pyyaml": ["yaml"],
Expand Down

0 comments on commit 90abdaf

Please sign in to comment.