fix: address code review comments (#3942)

* fix: address code review comments in https://github.com/project-zot/zot/pull/3885#pullrequestreview-4045836197

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>

* fix: data race in GetPort()

See https://github.com/project-zot/zot/actions/runs/24045271222/job/70126983674?pr=3942

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>

* fix(test): reuse ReadLogFileAndSearchString for auto-port log; throttle poll loop

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>

---------

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
This commit is contained in:
Andrei Aaron
2026-04-08 00:10:54 +03:00
committed by GitHub
parent 78c6e915dd
commit c6289ec5ba
6 changed files with 102 additions and 68 deletions
+14 -16
View File
@@ -57,8 +57,8 @@ type Controller struct {
LDAPClient *LDAPClient
taskScheduler *scheduler.Scheduler
Healthz *common.Healthz
// runtime params
chosenPort int // kernel-chosen port
// runtime params (atomic: Run may set the port concurrently with GetPort readers, e.g. tests)
chosenPort atomic.Int64
// TLS certificate management
TlsWatcher atomic.Pointer[TlsConfigWatcher]
}
@@ -127,7 +127,7 @@ func NewController(appConfig *config.Config) *Controller {
}
func (c *Controller) GetPort() int {
return c.chosenPort
return int(c.chosenPort.Load())
}
func (c *Controller) Run() error {
@@ -186,23 +186,21 @@ func (c *Controller) Run() error {
return err
}
// Always derive the bound port from the listener so GetPort matches the actual socket (numeric
// config, kernel-chosen :0, or service names like "http" resolved by net.Listen).
chosenAddr, ok := listener.Addr().(*net.TCPAddr)
if !ok {
c.Log.Error().Str("port", port).Msg("invalid addr type")
return errors.ErrBadType
}
c.chosenPort.Store(int64(chosenAddr.Port))
if port == "0" || port == "" {
chosenAddr, ok := listener.Addr().(*net.TCPAddr)
if !ok {
c.Log.Error().Str("port", port).Msg("invalid addr type")
return errors.ErrBadType
}
c.chosenPort = chosenAddr.Port
c.Log.Info().Int("port", chosenAddr.Port).IPAddr("address", chosenAddr.IP).Msg(
"port is unspecified, listening on kernel chosen port",
)
} else {
chosenPort, _ := strconv.ParseInt(port, 10, 32)
c.chosenPort = int(chosenPort)
}
tlsConfig := c.Config.CopyTLSConfig()