77 "regexp"
88 "slices"
99 "strings"
10+ "sync"
1011 "time"
1112
1213 "github.com/codeshelldev/gotl/pkg/jsonutils"
@@ -16,8 +17,8 @@ import (
1617 "github.com/moby/moby/client"
1718)
1819
20+ var mu sync.RWMutex
1921var containerHosts = map [string ][]string {}
20-
2122var containers []container.Summary
2223
2324func GetDiffDiscovery () Diff [string ] {
@@ -119,11 +120,17 @@ func getContainerDiff() (Diff[string], error) {
119120 return Diff [string ]{}, err
120121 }
121122
123+ mu .RLock ()
122124 containerDiff := diffContainers (containers , newContainers )
125+ mu .RUnlock ()
123126
127+ mu .Lock ()
124128 containers = newContainers
129+ mu .Unlock ()
125130
131+ mu .RLock ()
126132 logger .Info ("Found " , len (containers ), " enabled containers" )
133+ mu .RUnlock ()
127134
128135 if len (containerDiff .Added ) > 0 {
129136 logger .Debug ("Found " , len (containerDiff .Added ), " added containers" )
@@ -139,7 +146,9 @@ func getContainerDiff() (Diff[string], error) {
139146 hostSlices := slices .Collect (seq )
140147 hosts := slices .Concat (hostSlices ... )
141148
149+ mu .RLock ()
142150 old , exists := containerHosts [container .ID ]
151+ mu .RUnlock ()
143152 if exists {
144153 diff := GetDiff (old , hosts )
145154
@@ -154,18 +163,24 @@ func getContainerDiff() (Diff[string], error) {
154163 logger .Dev ("!> With " , strings .Join (hosts , "," ))
155164 }
156165
166+ mu .Lock ()
157167 containerHosts [container .ID ] = hosts
168+ mu .Unlock ()
158169 }
159170
160171 for _ , removed := range containerDiff .Removed {
172+ mu .RLock ()
161173 host , exists := containerHosts [removed .ID ]
174+ mu .RUnlock ()
162175
163176 if exists {
164177 globalDiff .Removed = append (globalDiff .Removed , host ... )
165178
166179 logger .Info ("Removed " , removed .Names [0 ])
167180
181+ mu .Lock ()
168182 delete (containerHosts , removed .ID )
183+ mu .Unlock ()
169184 }
170185 }
171186
0 commit comments