Fix mem bloat (#32)

* Create only one dockerClient

* remove now obsolete import
This commit is contained in:
d3lity
2023-07-31 22:15:33 +03:00
committed by GitHub
parent 763606ebfa
commit 30ea96a062
2 changed files with 5 additions and 12 deletions

View File

@@ -11,8 +11,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/google/gopacket"
_ "github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
@@ -28,6 +27,8 @@ type LazyGroup struct {
sleepMethod string // whether to stop or pause the container
}
var err error
func (lg LazyGroup) MainLoop() {
// rxPacketCount is continiously updated by the getRxPackets goroutine
var rxPacketCount int
@@ -72,9 +73,6 @@ func (lg LazyGroup) MainLoop() {
}
func (lg LazyGroup) getContainers() []types.Container {
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
check(err)
dockerClient.NegotiateAPIVersion(context.Background())
filter := filters.NewArgs(filters.Arg("label", "lazytainer.group="+lg.groupName))
containers, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: filter})
check(err)
@@ -84,9 +82,6 @@ func (lg LazyGroup) getContainers() []types.Container {
func (lg LazyGroup) stopContainers() {
debugLogger.Println("stopping container(s)")
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
check(err)
dockerClient.NegotiateAPIVersion(context.Background())
for _, c := range lg.getContainers() {
if lg.sleepMethod == "stop" || lg.sleepMethod == "" {
if err := dockerClient.ContainerStop(context.Background(), c.ID, container.StopOptions{}); err != nil {
@@ -106,9 +101,6 @@ func (lg LazyGroup) stopContainers() {
func (lg LazyGroup) startContainers() {
debugLogger.Println("starting container(s)")
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
check(err)
dockerClient.NegotiateAPIVersion(context.Background())
for _, c := range lg.getContainers() {
if lg.sleepMethod == "stop" || lg.sleepMethod == "" {
if err := dockerClient.ContainerStart(context.Background(), c.ID, types.ContainerStartOptions{}); err != nil {

View File

@@ -16,6 +16,7 @@ import (
// log := logger()
var infoLogger *log.Logger
var debugLogger *log.Logger
var dockerClient *client.Client
func main() {
flags := log.LstdFlags | log.Lshortfile
@@ -45,7 +46,7 @@ func configureFromLabels() map[string]LazyGroup {
container_id, err := os.Hostname()
check(err)
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
dockerClient, err = client.NewClientWithOpts(client.FromEnv)
check(err)
//negotiate API version to prevent "client version is too new" error