Closed Gr33nbl00d closed 2 weeks ago
See https://caddyserver.com/docs/extending-caddy#module-lifecycle, you can use a UsagePool
to track number of references to a resource.
Thanks for the fast reply. I have already seen this. However i thought my use case is a bit different than what UsagePool is usually used for. Currently each module has a dedicated working directory which contains a statefull disk persisted leveldb. Which can be accessed by one module instance at a time only. The only option i see is that i would extract the update timer and cleanup and a lot of other things like thread synchronization to a global object which might could be hold by UsagePool. Puh thats a big change but i guess it is the only option. It also has some advantages so to reuse a revocation list in multiple servers. Not sure my idea really can work. I have a look tomorrow.
One option is you could have a caddy.App
module which acts as your holder of global references (you can invoke an app from a handler/matcher/whatever module even if it's not configured, getting a singleton per config). Otherwise yeah a global object is an ok approach.
Ok yeah i was thinking about such an idea already. So it is in general possible to access an caddy.App module from somewhere "below the module tree?" How would you do this? I am asking because i currently have the question if it would be possible to detect from which "server" the revocation config is. If there is a way i could access the name for identification.
I guess i will go for the global var solution for the moment simply because of time restrictions. But i will investigate the "caddy.App" solution at some point because it might could help with some other issues too.
You can call https://pkg.go.dev/github.com/caddyserver/caddy/v2#Context.App from your module's Provision to get a reference to a known app (and it provisions and caches the app if it was not already invoked or configured).
I have a problem in my revocation module. I need to ensure that one working directory is only used by one server and one module instance at a time.
Because of this i have added a check during provision.
The problem is that i did not know that during admin reload a module can be multiple times loaded. It actually provisions all modules and after this does a cleanup of the old modules.
How can i deal with exlcusive resources like ports/files/directories inside a module in a safe way?
I would need to know when the old module instance is stopped/cleaned up in order to start using the resource in the new module instance `
https://github.com/Gr33nbl00d/caddy-revocation-validator/issues/17