Commit b385d131 authored by Olivier Bourdon's avatar Olivier Bourdon

Fix for issue #19: Potential thread safety issue during initialisation of plugin

parent c6ca4344
......@@ -28,6 +28,7 @@ import (
"os"
"path"
"strings"
"sync"
"syscall"
"time"
......@@ -96,6 +97,11 @@ var (
// Function to check properness of configuration parameter
// and set plugin attribute accordingly
func (p *dfCollector) setProcPath(cfg interface{}) error {
p.initializedMutex.Lock()
defer p.initializedMutex.Unlock()
if p.initialized {
return nil
}
procPath, err := config.GetConfigItem(cfg, "proc_path")
if err == nil && len(procPath.(string)) > 0 {
procPathStats, err := os.Stat(procPath.(string))
......@@ -107,6 +113,7 @@ func (p *dfCollector) setProcPath(cfg interface{}) error {
}
p.proc_path = procPath.(string)
}
p.initialized = true
return nil
}
......@@ -229,10 +236,12 @@ func (p *dfCollector) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
// NewDfCollector creates new instance of plugin and returns pointer to initialized object.
func NewDfCollector() *dfCollector {
logger := log.New()
imutex := new(sync.Mutex)
return &dfCollector{
stats: &dfStats{},
logger: logger,
proc_path: procPath,
stats: &dfStats{},
logger: logger,
initializedMutex: imutex,
proc_path: procPath,
}
}
......@@ -249,9 +258,11 @@ func Meta() *plugin.PluginMeta {
}
type dfCollector struct {
stats collector
logger *log.Logger
proc_path string
initialized bool
initializedMutex *sync.Mutex
stats collector
logger *log.Logger
proc_path string
}
type dfMetric struct {
......
......@@ -21,8 +21,11 @@ package df
import (
"strings"
"sync"
"testing"
log "github.com/Sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
......@@ -76,7 +79,10 @@ func (dfp *DfPluginSuite) SetupSuite() {
func (dfp *DfPluginSuite) TestGetMetricTypes() {
Convey("Given df plugin is initialized", dfp.T(), func() {
dfPlg := dfCollector{
stats: dfp.mockCollector,
stats: dfp.mockCollector,
logger: log.New(),
initializedMutex: new(sync.Mutex),
proc_path: "/proc",
}
Convey("When list of available metrics is requested", func() {
......@@ -117,7 +123,10 @@ func (dfp *DfPluginSuite) TestGetMetricTypes() {
func (dfp *DfPluginSuite) TestCollectMetrics() {
Convey("Given df plugin is initialized", dfp.T(), func() {
dfPlg := dfCollector{
stats: dfp.mockCollector,
stats: dfp.mockCollector,
logger: log.New(),
initializedMutex: new(sync.Mutex),
proc_path: "/proc",
}
Convey("When values for given metrics are requested", func() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment