add lock to getunifi

This commit is contained in:
davidnewhall2 2019-12-11 01:14:09 -08:00
parent ecd1c64004
commit 8aa071935e
5 changed files with 25 additions and 14 deletions

12
Gopkg.lock generated
View File

@ -27,7 +27,7 @@
[[projects]] [[projects]]
branch = "master" branch = "master"
digest = "1:50708c8fc92aec981df5c446581cf9f90ba9e2a5692118e0ce75d4534aaa14a2" digest = "1:00e5ad58045d6d2a6c9e65d1809ff2594bc396e911712ae892a93976fdece115"
name = "github.com/influxdata/influxdb1-client" name = "github.com/influxdata/influxdb1-client"
packages = [ packages = [
"models", "models",
@ -35,7 +35,7 @@
"v2", "v2",
] ]
pruneopts = "UT" pruneopts = "UT"
revision = "fc22c7df067eefd070157f157893fbce961d6359" revision = "8bf82d3c094dc06be9da8e5bf9d3589b6ea032ae"
[[projects]] [[projects]]
digest = "1:ff5ebae34cfbf047d505ee150de27e60570e8c394b3b8fdbb720ff6ac71985fc" digest = "1:ff5ebae34cfbf047d505ee150de27e60570e8c394b3b8fdbb720ff6ac71985fc"
@ -103,15 +103,15 @@
name = "golang.org/x/sys" name = "golang.org/x/sys"
packages = ["windows"] packages = ["windows"]
pruneopts = "UT" pruneopts = "UT"
revision = "eeba5f6aabab6d6594a9191d6bfeaca5fa6a8248" revision = "ac6580df4449443a05718fd7858c1f91ad5f8d20"
[[projects]] [[projects]]
digest = "1:87738e338f505d3e3be1f80d36b53f3c4e73be9b7ad4ccae46abbe9ef04f3f71" digest = "1:2883cea734f2766f41ff9c9d4aefccccc53e3d44f5c8b08893b9c218cf666722"
name = "golift.io/unifi" name = "golift.io/unifi"
packages = ["."] packages = ["."]
pruneopts = "UT" pruneopts = "UT"
revision = "ba857a3a04311fed362cb43fa7bf4066bc3a7e55" revision = "a607fe940c6a563c6994f2c945394b19d2183b1c"
version = "v4.1.5" version = "v4.1.6"
[[projects]] [[projects]]
digest = "1:b75b3deb2bce8bc079e16bb2aecfe01eb80098f5650f9e93e5643ca8b7b73737" digest = "1:b75b3deb2bce8bc079e16bb2aecfe01eb80098f5650f9e93e5643ca8b7b73737"

View File

@ -18,6 +18,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -54,6 +55,7 @@ type UnifiPoller struct {
Flag *Flag Flag *Flag
Config *Config Config *Config
LastCheck time.Time LastCheck time.Time
sync.Mutex // locks the Unifi struct member when re-authing to unifi.
} }
// Flag represents the CLI args available and their settings. // Flag represents the CLI args available and their settings.

View File

@ -35,9 +35,9 @@ func (u *UnifiPoller) DumpJSONPayload() (err error) {
case err != nil: case err != nil:
return err return err
case StringInSlice(u.Flag.DumpJSON, []string{"d", "device", "devices"}): case StringInSlice(u.Flag.DumpJSON, []string{"d", "device", "devices"}):
return u.dumpSitesJSON(unifi.DevicePath, "Devices", sites) return u.dumpSitesJSON(unifi.APIDevicePath, "Devices", sites)
case StringInSlice(u.Flag.DumpJSON, []string{"client", "clients", "c"}): case StringInSlice(u.Flag.DumpJSON, []string{"client", "clients", "c"}):
return u.dumpSitesJSON(unifi.ClientPath, "Clients", sites) return u.dumpSitesJSON(unifi.APIClientPath, "Clients", sites)
case strings.HasPrefix(u.Flag.DumpJSON, "other "): case strings.HasPrefix(u.Flag.DumpJSON, "other "):
apiPath := strings.SplitN(u.Flag.DumpJSON, " ", 2)[1] apiPath := strings.SplitN(u.Flag.DumpJSON, " ", 2)[1]
_, _ = fmt.Fprintf(os.Stderr, "[INFO] Dumping Path '%s':\n", apiPath) _, _ = fmt.Fprintf(os.Stderr, "[INFO] Dumping Path '%s':\n", apiPath)

View File

@ -28,7 +28,10 @@ func New() *UnifiPoller {
SaveSites: true, SaveSites: true,
HTTPListen: defaultHTTPListen, HTTPListen: defaultHTTPListen,
Namespace: appName, Namespace: appName,
}, Flag: &Flag{ConfigFile: DefaultConfFile}, },
Flag: &Flag{
ConfigFile: DefaultConfFile,
},
} }
} }
@ -125,6 +128,7 @@ func (u *UnifiPoller) PollController() {
for u.LastCheck = range ticker.C { for u.LastCheck = range ticker.C {
if err := u.CollectAndProcess(); err != nil { if err := u.CollectAndProcess(); err != nil {
u.LogErrorf("%v", err) u.LogErrorf("%v", err)
u.Unifi.CloseIdleConnections()
u.Unifi = nil // trigger re-auth in unifi.go. u.Unifi = nil // trigger re-auth in unifi.go.
} }
} }

View File

@ -11,6 +11,11 @@ import (
// GetUnifi returns a UniFi controller interface. // GetUnifi returns a UniFi controller interface.
func (u *UnifiPoller) GetUnifi() (err error) { func (u *UnifiPoller) GetUnifi() (err error) {
u.Lock()
defer u.Unlock()
if u.Unifi != nil {
u.Unifi.CloseIdleConnections()
}
// Create an authenticated session to the Unifi Controller. // Create an authenticated session to the Unifi Controller.
u.Unifi, err = unifi.NewUnifi(&unifi.Config{ u.Unifi, err = unifi.NewUnifi(&unifi.Config{
User: u.Config.UnifiUser, User: u.Config.UnifiUser,