Skip to content

Commit

Permalink
clair: add Shutdown struct
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Mar 10, 2021
1 parent 2d27ae5 commit 391c2f7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/clair/shutdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"fmt"
"net/http"
"sync"

"golang.org/x/sync/errgroup"
)

// Shutdown aggregates http.Sever Shutdown methods.
type Shutdown struct {
mu sync.Mutex
m map[*http.Server]struct{}
}

// Add registers a server.
func (s *Shutdown) Add(srv *http.Server) {
s.mu.Lock()
defer s.mu.Unlock()
if s.m == nil {
s.m = make(map[*http.Server]struct{})
}
s.m[srv] = struct{}{}
}

// Shutdown calls Shutdown on all added Servers. If a timeout is needed, it
// should be done via the passed Context.
func (s *Shutdown) Shutdown(ctx context.Context) error {
s.mu.Lock() // Leave locked forever
eg := &errgroup.Group{}
for srv := range s.m {
srv := srv
eg.Go(func() error {
if err := srv.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown %q: %w", srv.Addr, err)
}
return nil
})
delete(s.m, srv)
}
return eg.Wait()
}

0 comments on commit 391c2f7

Please sign in to comment.