rabbitmq / rabbitmq-objc-client

RabbitMQ client for Objective-C and Swift
https://rabbitmq.com
Other
242 stars 84 forks source link

Need a way to create non-auto recovery RMQConnection #174

Open tanphamanhh opened 4 years ago

tanphamanhh commented 4 years ago

As title was told. I don't find any way to create a RMQ Connection without auto recovery. I think it is necessary option when create a connection (like Java Library)

mattfiocca commented 1 year ago

I have the same requirement; our use case is such that we are using access tokens as passwords that are proxied through an HTTP backend for authentication. Auto-recovery fails for us because the tokens could be expired at the point of retrying the connection.

I have gotten around this though, by attaching a publisher (swift Combine) to our delegate class that can be subscribed to for disconnect events. From there we can manually tear the connection down and rebuild tokens as needed.

Maybe this can help others:

@objc class ConnectionDelegate: NSObject, RMQConnectionDelegate {

    var disconnectCalled = PassthroughSubject<Bool,Never>()

    ...

    func connection(_ connection: RMQConnection!, disconnectedWithError error: Error!) {
        disconnectCalled.send(true)
    }

    ...
}

let connection_delegate = ConnectionDelegate()
let disconnect_subscription = connection_delegate!.disconnectCalled.sink { [weak self] _ in
    guard let self_ = self else { return }

    // teardown, refresh tokens, reconnect, etc...
    self_.doTearDownAndReconnect()
}

...

let connection = RMQConnection(
    transport: transport,
    config: config,
    handshakeTimeout: 10,
    channelAllocator: allocator!,
    frameHandler: allocator!,
    delegate: connection_delegate!,
    command: commandQueue!,
    waiterFactory: RMQSemaphoreWaiterFactory(),
    heartbeatSender: heartbeatSender!)

connection?.start()
michaelklishin commented 1 year ago

Other clients have relevant features such as:

michaelklishin commented 1 year ago

While disabling recovery would be one reasonable option, both features above can be just as relevant, in particular with modern RabbitMQ versions.

mattfiocca commented 1 year ago

@michaelklishin, means to inject updated secrets prior to reconnect attempts would be marvelous..