From 692792f8be6b79fc3b22f62e05205e34b466ae4b Mon Sep 17 00:00:00 2001 From: Adrian Serrano Date: Mon, 23 Nov 2020 13:28:19 +0100 Subject: [PATCH] Thread-safe singleton --- .../module/system/socket/socket_linux.go | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/x-pack/auditbeat/module/system/socket/socket_linux.go b/x-pack/auditbeat/module/system/socket/socket_linux.go index 00879ce6152..36ae6276e14 100644 --- a/x-pack/auditbeat/module/system/socket/socket_linux.go +++ b/x-pack/auditbeat/module/system/socket/socket_linux.go @@ -88,29 +88,36 @@ func init() { } } -var currentInstance *MetricSet +var ( + // Singleton to instantiate one socket dataset at a time. + instance *MetricSet + instanceMutex sync.Mutex +) // New constructs a new MetricSet. func New(base mb.BaseMetricSet) (mb.MetricSet, error) { + instanceMutex.Lock() + defer instanceMutex.Unlock() + config := defaultConfig if err := base.Module().UnpackConfig(&config); err != nil { return nil, errors.Wrapf(err, "failed to unpack the %s config", fullName) } - if currentInstance != nil { + if instance != nil { // Do not instantiate a new dataset if the config hasn't changed. // This is necessary when run under config reloader even though the // reloader itself already checks the config for changes, because // the first time it runs it will allocate two consecutive instances // (one for checking the config, one for running). This saves // running the guesses twice on startup. - if config.Equals(currentInstance.config) { - return currentInstance, nil + if config.Equals(instance.config) { + return instance, nil } - currentInstance.terminated.Wait() + instance.terminated.Wait() } var err error - currentInstance, err = newSocketMetricset(config, base) - return currentInstance, err + instance, err = newSocketMetricset(config, base) + return instance, err } func newSocketMetricset(config Config, base mb.BaseMetricSet) (*MetricSet, error) {