I am trying to implement a service which will hold some value of an user and on dropping the TCP connection the data will be removed from the database.
I thought about using TTL on values, but it doesn't work because if the user tries to log in before the value is dropped then it causes a problem. And if the user stays idle for a long time while keeping the TCP connection the value will get automatically dropped due to TTL which will also cause a problem.
#invoking a function while dropping a TCP connection of a client
6 messages · Page 1 of 1 (latest)
hey! Couldn't you create a timestamp when the tcp connection is made and associate it to the user "session"
and manage it that way?
If the user reconnects the new timestamp will be different than the one you have associated to the data or on the
client "context". Would that make sense?
I implemented this somehow using custom wrapper type around tcpstream... https://github.com/tokio-rs/axum/discussions/3581
GitHub
Summary I am trying to implement a service which will hold some value of an user and on dropping the TCP connection the data will be removed from the database. I thought about using TTL on values, ...
Did it work? It makes sense to me, while MyIo is alive you can considerer the connection alive even if it's idle. It should only be dropped when the user actually disconnects, sounds like the behavior you want
tying the cleanup to a TTL is very tricky, for example I'm a lazy person and I could be your user. what if your ttl is 2 minutes but i take 3 minutes? The server would think I'm "gone" when I'm not, it's a cat and mouse game between timeouts and real user behavior.
using the lifetime of the actual TCP connection is more reliable because it's tied to the real network state
yeah it was kinda easy to implement, you just have to make a wrapper type of tcp stream