private async ValueTask DisposeConnection(PulsarUrl serviceUrl) { if (_connections.TryRemove(serviceUrl, out Connection connection)) { await connection.DisposeAsync().ConfigureAwait(false); DotPulsarEventSource.Log.ConnectionDisposed(); } }
private async ValueTask <Connection> GetConnection(PulsarUrl url, CancellationToken cancellationToken) { using (await _lock.Lock(cancellationToken).ConfigureAwait(false)) { if (_connections.TryGetValue(url, out Connection connection)) { return(connection); } return(await EstablishNewConnection(url, cancellationToken).ConfigureAwait(false)); } }
public async ValueTask <IConnection> FindConnectionForTopic(string topic, CancellationToken cancellationToken) { var lookup = new CommandLookupTopic { Topic = topic, Authoritative = false, AdvertisedListenerName = _listenerName }; var physicalUrl = _serviceUrl; while (true) { var connection = await GetConnection(physicalUrl, cancellationToken).ConfigureAwait(false); var response = await connection.Send(lookup, cancellationToken).ConfigureAwait(false); response.Expect(BaseCommand.Type.LookupResponse); if (response.LookupTopicResponse.Response == CommandLookupTopicResponse.LookupType.Failed) { response.LookupTopicResponse.Throw(); } lookup.Authoritative = response.LookupTopicResponse.Authoritative; var lookupResponseServiceUrl = new Uri(GetBrokerServiceUrl(response.LookupTopicResponse)); if (response.LookupTopicResponse.Response == CommandLookupTopicResponse.LookupType.Redirect || !response.LookupTopicResponse.Authoritative) { physicalUrl = lookupResponseServiceUrl; continue; } if (response.LookupTopicResponse.ProxyThroughServiceUrl) { var url = new PulsarUrl(physicalUrl, lookupResponseServiceUrl); return(await GetConnection(url, cancellationToken).ConfigureAwait(false)); } // LookupType is 'Connect', ServiceUrl is local and response is authoritative. Assume the Pulsar server is a standalone docker. return(lookupResponseServiceUrl.IsLoopback ? connection : await GetConnection(lookupResponseServiceUrl, cancellationToken).ConfigureAwait(false)); } }
private async Task <Connection> EstablishNewConnection(PulsarUrl url, CancellationToken cancellationToken) { var stream = await _connector.Connect(url.Physical).ConfigureAwait(false); var connection = new Connection(new PulsarStream(stream)); DotPulsarEventSource.Log.ConnectionCreated(); _connections[url] = connection; _ = connection.ProcessIncommingFrames(cancellationToken).ContinueWith(t => DisposeConnection(url)); var commandConnect = _commandConnect; if (url.ProxyThroughServiceUrl) { commandConnect = WithProxyToBroker(_commandConnect, url.Logical); } var response = await connection.Send(commandConnect, cancellationToken).ConfigureAwait(false); response.Expect(BaseCommand.Type.Connected); return(connection); }