Narrower locks for faster response times

This commit is contained in:
Christopher LaPointe
2023-05-30 22:04:20 -04:00
parent f9f5e66553
commit e6c0aedf9a

View File

@@ -216,9 +216,6 @@ func (s *Core) pollThread(rate time.Duration) {
// stopping idle containers // stopping idle containers
// Will normally happen in the background with the pollThread // Will normally happen in the background with the pollThread
func (s *Core) Poll() { func (s *Core) Poll() {
s.mux.Lock()
defer s.mux.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
@@ -240,6 +237,10 @@ func (s *Core) checkForNewContainersSync(ctx context.Context) {
} }
} }
// Lock
s.mux.Lock()
defer s.mux.Unlock()
// check for containers we think are running, but aren't (destroyed, error'd, stop'd via another process, etc) // check for containers we think are running, but aren't (destroyed, error'd, stop'd via another process, etc)
for cid, cts := range s.active { for cid, cts := range s.active {
if _, ok := runningContainers[cid]; !ok && !cts.pinned { if _, ok := runningContainers[cid]; !ok && !cts.pinned {
@@ -259,6 +260,9 @@ func (s *Core) checkForNewContainersSync(ctx context.Context) {
} }
func (s *Core) watchForInactivitySync(ctx context.Context) { func (s *Core) watchForInactivitySync(ctx context.Context) {
s.mux.Lock()
defer s.mux.Unlock()
for cid, cts := range s.active { for cid, cts := range s.active {
shouldStop, err := s.checkContainerForInactivity(ctx, cid, cts) shouldStop, err := s.checkContainerForInactivity(ctx, cid, cts)
if err != nil { if err != nil {