Module Lifecycle

Quoting the documentation of the caddy.Module, we find this:

Modules will often implement additional interfaces including Provisioner, Validator, and CleanerUpper. If a module implements these interfaces, their methods are called during the module’s lifespan.

When a module is loaded by a host module, the following happens:

  1. ModuleInfo.New() is called to get a new instance of the module.
  2. The module’s configuration is unmarshaled into that instance.
  3. If the module is a Provisioner, the Provision() method is called.
  4. If the module is a Validator, the Validate() method is called.
  5. The module will probably be type-asserted from interface{} to some other, more useful interface expected by the host > module. For example, HTTP handler modules are type-asserted as caddyhttp.MiddlewareHandler values.
  6. When a module’s containing Context is canceled, if it is a CleanerUpper, its Cleanup() method is called.

To say it differently:

Lifecycle Diagram