def getConnection(self, micro_status_entry):
        '''Return a deferred which will fire (if connection attempt is
        successful) with a Connection Protocol made to *relay*.

        There are three general cases to handle for incoming connection
        requests:

            1. We already have an open TLS connection to the requested relay.
               In this case, immediately callback the deferred with the
               open connection.
            2. We're currently trying to connect to this relay. In this case,
               add the request to a pending request list for this relay.
               When the connection is made successfully, callback all
               pending request deferreds with the Connection, or errback
               all pending request deferreds on failure.
            3. We have no open or pending connections to this relay (i.e.
               this is the first connection request to this relay). In this
               case, create a new list of pending requests for this connection
               and add the current request. Create an SSL endpoint and add an
               appropriate callback and errback. If the request is successful,
               callback all pending requests with the open connection when
               it opens; errback all pending requests on failure.

        :param stem.descriptor.server_descriptor.RelayDescriptor relay:
            relay to make a TLS connection to
        :returns: **twisted.internet.defer.Deferred** which, on success, will
            callback with an oppy.connection.connection.Connection Protocol
            object

        XXX raises:
        '''
        from twisted.internet import reactor

        m = micro_status_entry

        d = defer.Deferred()

        if m.fingerprint in self._connection_dict:
            d.callback(self._connection_dict[m.fingerprint])
        elif m.fingerprint in self._pending_request_dict:
            self._pending_request_dict[m.fingerprint].append(d)
        else:
            try:
                f = endpoints.connectProtocol(endpoints.SSL4ClientEndpoint(
                    reactor, m.address, m.or_port, TLSClientContextFactory()),
                                              ConnectionBuildTask(self, m))
                f.addErrback(self._initialConnectionFailed, m.fingerprint)
                self._pending_request_dict[m.fingerprint] = [d]
            except Exception as e:
                self._initialConnectionFailed(e, m.fingerprint)

        return d

    # called when the initial connection fails
    # TODO: track these and timeout if we get too many failures
dataset/ETHPy150Open nskinkel/oppy/oppy/connection/connectionmanager.py/ConnectionManager.getConnection/original