Skip to content

Commit

Permalink
Address PR review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Kostadinov <dimitar.kostadinov@sap.com>
  • Loading branch information
dimitar-kostadinov committed Mar 6, 2024
1 parent 1e833ce commit 21d367d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
9 changes: 4 additions & 5 deletions registry/handlers/app.go
Expand Up @@ -412,12 +412,11 @@ func (app *App) RegisterHealthChecks(healthRegistries ...*health.Registry) {
}

// OnExit close the underlying registry
func (app *App) OnExit() {
if app.isCache {
if regProxy, ok := app.registry.(proxy.Closable); ok {
regProxy.Close()
}
func (app *App) OnExit() error {
if r, ok := app.registry.(proxy.Closer); ok {
return r.Close()
}
return nil
}

// register a handler with the application, by route name. The handler will be
Expand Down
8 changes: 4 additions & 4 deletions registry/proxy/proxyregistry.go
Expand Up @@ -211,13 +211,13 @@ func (pr *proxyingRegistry) BlobStatter() distribution.BlobStatter {
return pr.embedded.BlobStatter()
}

type Closable interface {
type Closer interface {
// Close release all resources used by this object
Close()
Close() error
}

func (pr *proxyingRegistry) Close() {
pr.scheduler.Stop()
func (pr *proxyingRegistry) Close() error {
return pr.scheduler.Stop()
}

// authChallenger encapsulates a request to the upstream to establish credential challenges
Expand Down
8 changes: 5 additions & 3 deletions registry/proxy/scheduler/scheduler.go
Expand Up @@ -206,12 +206,13 @@ func (ttles *TTLExpirationScheduler) startTimer(entry *schedulerEntry, ttl time.
}

// Stop stops the scheduler.
func (ttles *TTLExpirationScheduler) Stop() {
func (ttles *TTLExpirationScheduler) Stop() error {
ttles.Lock()
defer ttles.Unlock()

if err := ttles.writeState(); err != nil {
dcontext.GetLogger(ttles.ctx).Errorf("Error writing scheduler state: %s", err)
err := ttles.writeState()
if err != nil {
err = fmt.Errorf("error writing scheduler state: %w", err)
}

for _, entry := range ttles.entries {
Expand All @@ -221,6 +222,7 @@ func (ttles *TTLExpirationScheduler) Stop() {
close(ttles.doneChan)
ttles.saveTimer.Stop()
ttles.stopped = true
return err
}

func (ttles *TTLExpirationScheduler) writeState() error {
Expand Down
12 changes: 10 additions & 2 deletions registry/proxy/scheduler/scheduler_test.go
Expand Up @@ -136,7 +136,12 @@ func TestRestoreOld(t *testing.T) {
if err != nil {
t.Fatalf("Error starting ttlExpirationScheduler: %s", err)
}
defer s.Stop()
defer func(s *TTLExpirationScheduler) {
err := s.Stop()
if err != nil {
t.Fatalf("Error stopping ttlExpirationScheduler: %s", err)
}
}(s)

wg.Wait()
mu.Lock()
Expand Down Expand Up @@ -177,7 +182,10 @@ func TestStopRestore(t *testing.T) {

// Start and stop before all operations complete
// state will be written to fs
s.Stop()
err = s.Stop()
if err != nil {
t.Fatalf(err.Error())
}
time.Sleep(10 * time.Millisecond)

// v2 will restore state from fs
Expand Down
9 changes: 7 additions & 2 deletions registry/registry.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -221,7 +222,7 @@ func setDirectoryURL(directoryurl string) *acme.Client {
}

// ListenAndServe runs the registry's HTTP server.
func (registry *Registry) ListenAndServe() error {
func (registry *Registry) ListenAndServe() (err error) {
config := registry.config

ln, err := listener.NewListener(config.HTTP.Net, config.HTTP.Addr)
Expand Down Expand Up @@ -316,7 +317,11 @@ func (registry *Registry) ListenAndServe() error {
go func() {
serveErr <- registry.server.Serve(ln)
}()
defer registry.app.OnExit()
defer func(app *handlers.App) {
if exitErr := app.OnExit(); exitErr != nil {
err = errors.Join(err, exitErr)
}
}(registry.app)

select {
case err := <-serveErr:
Expand Down

0 comments on commit 21d367d

Please sign in to comment.