Persist information between caddy reloads


We have a caddy server running in our production environment. We want to construct a data structure (in memory) based on the requests that came in for a host. Is there any way to persist that data between caddy reloads? We used like to use the request history for some period of time.


Would this be through a custom Caddy plugin? Like, are you asking for help using Caddy’s Go APIs to persist state through reloads?

I was thinking more along the lines of using Caddy’s APIs to persist data, if any.
Also, is there such an example in the caddy source code that does something similar outside of reading a configuration file?

Can you be more specific (about “outside of reading a configuration file”)? I want to drill down into this because we have just implemented in Caddy 2 the ability to persist state between config changes / reloads.

For Caddy 1, you can create a package-level (“global”) variable that lives for the lifetime of the application. You can do that in Caddy 2 as well, but we also have a way to carry through state without needing global variables.

We were hoping to collect some statistics from the requests that came in. I did not want to add those details in the Caddyfile because I din’t want to clutter the file with information that is not related to configuring different directives. Maybe writing it to a different file that can be read when caddy starts up might not be a bad idea.

Is there a graceful way to restart caddy outside of killing the current process? Currently we do ExecReload=/bin/kill -USR1 $MAINPID for a reload.

We are currently running a slightly newer version than 0.11.0, specific commit id being: d3b731e9255b72d4571a5aac125634cf1b6031dc.

That’s certainly an option.

That doesn’t kill the process; it performs a graceful in-process reload of the config. That is the correct way to do it.

I just confirmed that the process ID does not change and read up a little more about ExecReload. The kill $MAINPID threw me off a little bit.

Thanks a lot for all the help. I have enough information to get started on solving my problem, now.
I will definitely check out Caddy 2 soon. Sounds like there has been quite a few new features.

1 Like