views:

11

answers:

1

Say a server stores objects with a reference count. Clients, when they connect (over sockets), send messages to increment and decrement the counts on those objects. The only behavior pattern that's guaranteed is that if a client works with a particular object, it will increment it, some time will pass, and it will decrement it (clients never do either more than once and never decrement without incrementing first). However, if a client sends an increment message, and then crashes before sending the decrement message, the server somehow needs to know to decrement the reference count itself.

The naive solution is to store a list of clients 'attached' to an object, and put the clients in the list to increment and take them out to decrement. Then when a client disconnects the server can iterate all of the lists and search for the disconnected client. But this uses O(N) memory where N is the number of clients. Is there a better way?

(If you're wondering what the reference count is for, the clients and the server are both connected to a second server, and the reference count of the objects stored in the first server affect what requests the second server will answer for clients.)

Update: Clients may wait seconds, minutes, or days before finally decrementing the count. Any sensible timeout would have to be too long to be useful.

A: 

You could require clients to periodically "refresh" their reference.

Anon.