Looking for "design input" on a worker (my first) I'm writing.

The worker receive a message regularly from a client/device. I would like to detect (in the worker) when the client ceases to send the message (or it's significantly overdue). So, it's like a snitch or a watchdog. There will be multiple clients, reporting at different times and periods. I receive a message and it tells me when to expect the next one. What would be a design to detect that a client is overdue? So far, my only thought is: - when receiving a message, store in KV the time the next one is expected. - trigger a worker via cron/timer at a period close to the shortest period of clients - and check if any times in the KV store are earlier than now Any better way to do this?
8 Replies
zegevlier
zegevlier•11mo ago
I think the more "idiomatic" way would be to use DOs and alarms. You would have one DO per client, then each message from the client triggers the DO to set an alarm at the time it would be overdue, overwriting any existing alarms. Then, if the alarm triggers you know the client is overdue. This would scale up infinitely (there is only so much you can do each cron trigger), and it would react quicker to a client not responding. Plus, KV is not strongly consistent while DO storage (which includes alarms) is
mackenzie
mackenzie•11mo ago
thanks. I wanted to look at DO, but haven't got that far yet - and owndered if there was an "alarm" concept anywhere - so that sounds positive! would I need one DO per client, or one DO could serve them all, and set the alarm at the earliest pending time each time? (I suspect I'm thinking wrong about DOs.....as they will have specific data, right?)
zegevlier
zegevlier•11mo ago
Just a quick overview of DOs: Every Durable Object is a JS class. Each DO class you have also has a DO binding. On this binding, you can get DO "actors" by ID. Each actor has its own storage and thread and may be located in a different part of the word. All the actors execute the same code, that of the DO class. I would suggest having each client request a DO with a different ID, since then the code would be a lot simpler to manage.
mackenzie
mackenzie•11mo ago
Understood - I have a unique client / device ID I could use for that if this project was ever used "for real" (pet project ATM) it would be thousands (or Millions) of clients - but that doesn't sound like an issue? Thanks very much for your input! Off to complete my reading on DOs, and see how to convert my existing worker to a DO....
zegevlier
zegevlier•11mo ago
Nope, that would work fine Best of luck!
mackenzie
mackenzie•11mo ago
I'll have to move up to a paid plan to use DO - but I guess that's the idea! 🙂 I don't see DO-help, or DO-discussions channels - so I assume we ask in general Workers channels.
zegevlier
zegevlier•11mo ago
There is #durable-objects! 😄
mackenzie
mackenzie•11mo ago
Duh. Hadn’t seen it….thanks! Just FYI, I’m writing this in rust. Easy so far with workers-rs, so let’s see in DO!
Want results from more Discord servers?
Add your server