Skip to content

Re-using connection for long lived commands like subscriptions #173

@StefanPrintezis

Description

@StefanPrintezis

In the pubsub docs example when subscribing you're supposed to use the .withConnection.

try await valkeyClient.withConnection { connection in
    try await connection.subscribe(to: ["channel1", "channel2"]) { subscription in
        for try await item in subscription {
            // a subscription item includes the channel the message was published on
            // as well as the message
            print(item.channel)
            print(item.message)
        }
    }
}

What would your advice be when you want to subscribe, but don't need to claim the connection in it's closure? Effectively re-using a connection for long lived subscriptions?

Right now i've implemented my own "connection manager" & keep withConnections closures open during a service run() so my thousands of subscriptions can run on just several connections, but it feels like i'm circumventing the design somehow or i'm missing something here, so some direction on this part would be greatly appreciated!

Also before my implementation ("connection manager") I depleted my connections pretty quickly in my tests but did not have an easy way of telling the connection pool could no longer provide an available connection. Should I see a log statement if the command times out? I probably don't see it because other timeouts trigger sooner I think

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions