summaryrefslogtreecommitdiff
path: root/vendor/github.com/sjtug/cerberus/core/instance.go
blob: fa4962fbf6d61203b769807105018fdf2c47de5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package core

import (
	"go.uber.org/zap"
)

// Instance is the shared core of the cerberus module.
// There's only one instance of this struct in the entire Caddy runtime.
type Instance struct {
	*InstanceState
	Config
}

// UpdateWithConfig updates the instance with a new config.
// If the config is incompatible with the current config, its internal state will be reset.
// User can pass in an optional logger to log basic metrics about the initialized state.
func (i *Instance) UpdateWithConfig(c Config, logger *zap.Logger) error {
	logger.Info("updating cerberus instance config")
	if i.StateCompatible(&c) {
		// We only need to update the config.
		i.Config = c
	} else {
		// We need to reset the state.
		logger.Info("existing cerberus instance with incompatible config found, resetting state")
		state, pendingElems, blocklistElems, approvalElems, err := NewInstanceState(c)
		if err != nil {
			return err
		}
		i.Config = c
		i.Close() // Close the old state
		i.InstanceState = state
		logger.Info("cerberus state initialized",
			zap.Int64("pending_elems", pendingElems),
			zap.Int64("blocklist_elems", blocklistElems),
			zap.Int64("approval_elems", approvalElems),
		)
	}
	return nil
}