cloudflare / pingora

A library for building fast, reliable and evolvable network services.
Apache License 2.0
21.01k stars 1.16k forks source link

[Question or Feature] how to track client connect/disconnect esp. for websocket #337

Closed futurist closed 1 month ago

futurist commented 1 month ago

What is the problem your feature solves, or the need it fulfills?

We have many websocket servers, using Pingora as gateway, want to release some resource when idle(no client connected)

Describe the solution you'd like

type Connections = DashSet<String>;
type Clients = DashMap<String, Connections>;

#[derive(Clone)]
pub struct ConnectionManager {
    clients: Arc<Clients>,
}

I have this ConnectionManager to manage all Clients and Connections. For example, client "abc" maybe have 1,2,3 connections.

The ConnectionManager will track connection come and go, when a client's connection is 0, will release all resources related to that client.

The question is: ProxyHTTP trait method connected_to_upstream maybe used for track connect, but I don't find method for disconnect, can add a method for this or there's already one?

eaufavor commented 1 month ago

See https://github.com/cloudflare/pingora/issues/245#issuecomment-2129977269. Disconnect is tracked via the drop of the Tracer object.

Also note that connected_to_upstream will happen for both new connection and reused one. The Tracer object is designed to track both connect and disconnect at connection level.