This commit adds the support to do the following: * Mark connections for non-reporting workers as closed. This is the controller counterpart to the worker functionality (see #1330). This is written as a scheduled job that does the work DB-side in a single atomic query. * Works to reconcile states if such a broken controller-worker connection resumes and a worker reports a connection as connected that should be disconnected. In this case, the controller will send an update request, and the worker will honor it and terminate the connection. * Further refinement of the grace period setting has been added here. We have converged on the current server "liveness" setting as our default here, which is half of the previous 30s (15 seconds, in other words). Additionally, this is now configurable on the controller and worker side, with the caveat that it's currently impossible to do so in config as the setting has been untagged in HCL. This is exposed so that we can run some sophisticated testing scenarios where we skew the grace period to either the controller or worker to ensure the aforementioned reconciliation works. * Some repository functions have been added to support the new functionality, in addition to some test code to the worker to allow querying of session state while testing. * Finally, we've added some add timestamp subtraction functions as well, basically serving as the opposite of the addition functions.
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package servers
|
|
|
|
import "time"
|
|
|
|
// getOpts - iterate the inbound Options and return a struct
|
|
func getOpts(opt ...Option) options {
|
|
opts := getDefaultOptions()
|
|
for _, o := range opt {
|
|
o(&opts)
|
|
}
|
|
return opts
|
|
}
|
|
|
|
// Option - how Options are passed as arguments
|
|
type Option func(*options)
|
|
|
|
// options = how options are represented
|
|
type options struct {
|
|
withLimit int
|
|
withLiveness time.Duration
|
|
withUpdateTags bool
|
|
}
|
|
|
|
func getDefaultOptions() options {
|
|
return options{
|
|
withLimit: 0,
|
|
withLiveness: 0,
|
|
}
|
|
}
|
|
|
|
// WithLimit provides an option to provide a limit. Intentionally allowing
|
|
// negative integers. If WithLimit < 0, then unlimited results are returned.
|
|
// If WithLimit == 0, then default limits are used for results.
|
|
func WithLimit(limit int) Option {
|
|
return func(o *options) {
|
|
o.withLimit = limit
|
|
}
|
|
}
|
|
|
|
// WithLiveness indicates how far back we want to search for server entries.
|
|
// Use 0 for the default liveness (15 seconds). A liveness value of -1 removes
|
|
// the liveliness condition.
|
|
func WithLiveness(liveness time.Duration) Option {
|
|
return func(o *options) {
|
|
o.withLiveness = liveness
|
|
}
|
|
}
|
|
|
|
// WithUpdateTags indicates that we should perform tag updates in the DB.
|
|
// This would happen on first sync from a worker after startup or (eventually,
|
|
// perhaps), after a SIGHUP.
|
|
func WithUpdateTags(updateTags bool) Option {
|
|
return func(o *options) {
|
|
o.withUpdateTags = updateTags
|
|
}
|
|
}
|