From 413cb32b6c0b37a5721e08ed87ac7f0cb425917c Mon Sep 17 00:00:00 2001 From: Laurie Clark-Michalek Date: Wed, 21 Mar 2018 14:08:39 +0000 Subject: [PATCH] Use static initialisation for invoke instances, instead of init funcs The order of init function execution is dependant on the order that the source files are passed to the compiler. This causes issues when building under other build systems, such as bazel or buck, as they are not guarenteed to maintain the same file order as the default go tool. --- cpu/cpu.go | 3 +-- disk/disk.go | 6 +----- docker/docker.go | 6 +----- host/host.go | 6 +----- load/load.go | 6 +----- mem/mem.go | 6 +----- net/net.go | 6 +----- process/process.go | 6 +----- 8 files changed, 8 insertions(+), 37 deletions(-) diff --git a/cpu/cpu.go b/cpu/cpu.go index 049869e..ceaf77f 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -54,10 +54,9 @@ type lastPercent struct { } var lastCPUPercent lastPercent -var invoke common.Invoker +var invoke common.Invoker = common.Invoke{} func init() { - invoke = common.Invoke{} lastCPUPercent.Lock() lastCPUPercent.lastCPUTimes, _ = Times(false) lastCPUPercent.lastPerCPUTimes, _ = Times(true) diff --git a/disk/disk.go b/disk/disk.go index a2c4720..1c31047 100644 --- a/disk/disk.go +++ b/disk/disk.go @@ -6,11 +6,7 @@ import ( "github.com/shirou/gopsutil/internal/common" ) -var invoke common.Invoker - -func init() { - invoke = common.Invoke{} -} +var invoke common.Invoker = common.Invoke{} type UsageStat struct { Path string `json:"path"` diff --git a/docker/docker.go b/docker/docker.go index 83716fd..76791c2 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -10,11 +10,7 @@ import ( var ErrDockerNotAvailable = errors.New("docker not available") var ErrCgroupNotAvailable = errors.New("cgroup not available") -var invoke common.Invoker - -func init() { - invoke = common.Invoke{} -} +var invoke common.Invoker = common.Invoke{} type CgroupMemStat struct { ContainerID string `json:"containerID"` diff --git a/host/host.go b/host/host.go index a256a6c..1e9e9bb 100644 --- a/host/host.go +++ b/host/host.go @@ -6,11 +6,7 @@ import ( "github.com/shirou/gopsutil/internal/common" ) -var invoke common.Invoker - -func init() { - invoke = common.Invoke{} -} +var invoke common.Invoker = common.Invoke{} // A HostInfoStat describes the host status. // This is not in the psutil but it useful. diff --git a/load/load.go b/load/load.go index dfe32a1..9085889 100644 --- a/load/load.go +++ b/load/load.go @@ -6,11 +6,7 @@ import ( "github.com/shirou/gopsutil/internal/common" ) -var invoke common.Invoker - -func init() { - invoke = common.Invoke{} -} +var invoke common.Invoker = common.Invoke{} type AvgStat struct { Load1 float64 `json:"load1"` diff --git a/mem/mem.go b/mem/mem.go index 87dfb53..bdf15ba 100644 --- a/mem/mem.go +++ b/mem/mem.go @@ -6,11 +6,7 @@ import ( "github.com/shirou/gopsutil/internal/common" ) -var invoke common.Invoker - -func init() { - invoke = common.Invoke{} -} +var invoke common.Invoker = common.Invoke{} // Memory usage statistics. Total, Available and Used contain numbers of bytes // for human consumption. diff --git a/net/net.go b/net/net.go index 428e68e..c31f512 100644 --- a/net/net.go +++ b/net/net.go @@ -12,11 +12,7 @@ import ( "github.com/shirou/gopsutil/internal/common" ) -var invoke common.Invoker - -func init() { - invoke = common.Invoke{} -} +var invoke common.Invoker = common.Invoke{} type IOCountersStat struct { Name string `json:"name"` // interface name diff --git a/process/process.go b/process/process.go index e20742a..29d114e 100644 --- a/process/process.go +++ b/process/process.go @@ -11,11 +11,7 @@ import ( "github.com/shirou/gopsutil/mem" ) -var invoke common.Invoker - -func init() { - invoke = common.Invoke{} -} +var invoke common.Invoker = common.Invoke{} type Process struct { Pid int32 `json:"pid"`