Skip to content

Commit

Permalink
remove tracking stopped containers as dying
Browse files Browse the repository at this point in the history
Do not mark containers as dying after they have been stopped.
The mechanic causes that when a single container is restarted it is not
registered again. The containers is marked as dying until
ttl-dying-cleanup expired. If the container is restarted before the TTL
expires it ignored and not registered again.
Containers are also only unmarked as dying when bridge.add() is called,
if this did not happen despite the TTL expires and the container was
restarted it was also not registered again.
I guess the dying state was introduced to prevent that a container is
reregistered while it is being shutdown.
Because Sync() was changed to only consider containers in running state
it can not happen anymore.

The ttl-dying-cleanup config option is removed.
  • Loading branch information
fho committed Oct 22, 2024
1 parent 103eadb commit 17c55df
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
41 changes: 10 additions & 31 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strconv"
"strings"
"sync"
"time"

dockerapi "github.com/fsouza/go-dockerclient"
)
Expand All @@ -20,12 +19,11 @@ var serviceIDPattern = regexp.MustCompile(`^(.+?):([a-zA-Z0-9][a-zA-Z0-9_.-]+):[

type Bridge struct {
sync.Mutex
registry RegistryAdapter
docker *dockerapi.Client
services map[string][]*Service
deadContainers map[string]*DeadContainer
dyingContainers map[string]time.Time
config Config
registry RegistryAdapter
docker *dockerapi.Client
services map[string][]*Service
deadContainers map[string]*DeadContainer
config Config
}

func New(docker *dockerapi.Client, adapterUri string, config Config) (*Bridge, error) {
Expand All @@ -40,12 +38,11 @@ func New(docker *dockerapi.Client, adapterUri string, config Config) (*Bridge, e

log.Println("Using", uri.Scheme, "adapter:", uri)
return &Bridge{
docker: docker,
config: config,
registry: factory.New(uri),
services: make(map[string][]*Service),
deadContainers: make(map[string]*DeadContainer),
dyingContainers: make(map[string]time.Time),
docker: docker,
config: config,
registry: factory.New(uri),
services: make(map[string][]*Service),
deadContainers: make(map[string]*DeadContainer),
}, nil
}

Expand Down Expand Up @@ -179,11 +176,6 @@ func (b *Bridge) Sync(quiet bool) {
}

func (b *Bridge) add(containerId string, quiet bool) {
if _, ok := b.dyingContainers[containerId]; ok {
log.Println("container, ", containerId[:12], ", is dying, ignoring")
return
}

if d := b.deadContainers[containerId]; d != nil {
b.services[containerId] = d.Services
delete(b.deadContainers, containerId)
Expand Down Expand Up @@ -384,7 +376,6 @@ func (b *Bridge) remove(containerId string, deregister bool) {
b.deadContainers[containerId] = &DeadContainer{b.config.RefreshTtl, b.services[containerId]}
}
delete(b.services, containerId)
b.markContainerAsDying(containerId)
}

// bit set on ExitCode if it represents an exit via a signal
Expand Down Expand Up @@ -418,18 +409,6 @@ func (b *Bridge) shouldRemove(containerId string) bool {
return false
}

func (b *Bridge) markContainerAsDying(containerId string) {
// cleanup after CleanupDyingTtl
for containerId, t := range b.dyingContainers {
if time.Since(t) >= time.Millisecond*time.Duration(b.config.CleanupDyingTtl) {
delete(b.dyingContainers, containerId)
}
}

// mark container as "dying"
b.dyingContainers[containerId] = time.Now()
}

var Hostname string

func init() {
Expand Down
1 change: 0 additions & 1 deletion bridge/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Config struct {
RefreshInterval int
DeregisterCheck string
Cleanup bool
CleanupDyingTtl int
}

type Service struct {
Expand Down
2 changes: 0 additions & 2 deletions registrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var (
retryAttempts = flag.Int("retry-attempts", 0, "Max retry attempts to establish a connection with the backend. Use -1 for infinite retries")
retryInterval = flag.Int("retry-interval", 2000, "Interval (in millisecond) between retry-attempts.")
cleanup = flag.Bool("cleanup", false, "Remove dangling services")
cleanupDyingTtl = flag.Int("ttl-dying-cleanup", 60000, "TTL (in millisecond) for cleaning dying containers cache")
)

func assert(err error) {
Expand Down Expand Up @@ -110,7 +109,6 @@ func main() {
RefreshInterval: *refreshInterval,
DeregisterCheck: *deregister,
Cleanup: *cleanup,
CleanupDyingTtl: *cleanupDyingTtl,
})

assert(err)
Expand Down

0 comments on commit 17c55df

Please sign in to comment.