Refactor sign-in page and IAM service lifecycle
Redesign the sign-in page to show email/password form inline with OIDC provider buttons (with vendor icons) instead of separate pages. Extract OIDCProvider type to its own file. Replace errgroup with sync.WaitGroup + WithCancelCause for graceful shutdown in IAM services. Refactor garbage collectors to use functional options and time.Ticker instead of time.After to avoid repeated allocations. Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -34,20 +34,36 @@ type (
|
||||
interval time.Duration
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
GarbageCollectorOption func(*GarbageCollector)
|
||||
)
|
||||
|
||||
func NewGarbageCollector(
|
||||
pg *pg.Client,
|
||||
interval time.Duration,
|
||||
logger *log.Logger,
|
||||
) *GarbageCollector {
|
||||
return &GarbageCollector{
|
||||
pg: pg,
|
||||
interval: interval,
|
||||
logger: logger.Named("saml.garbage_collector").With(log.Duration("interval", interval)),
|
||||
func WithGarbageCollectionInterval(interval time.Duration) GarbageCollectorOption {
|
||||
return func(gc *GarbageCollector) {
|
||||
gc.interval = interval
|
||||
}
|
||||
}
|
||||
|
||||
func NewGarbageCollector(
|
||||
pgClient *pg.Client,
|
||||
logger *log.Logger,
|
||||
opts ...GarbageCollectorOption,
|
||||
) *GarbageCollector {
|
||||
gc := &GarbageCollector{
|
||||
pg: pgClient,
|
||||
interval: DefaultGarbageCollectionInterval,
|
||||
logger: logger.Named("saml.garbage_collector"),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(gc)
|
||||
}
|
||||
|
||||
gc.logger = gc.logger.With(log.Duration("interval", gc.interval))
|
||||
|
||||
return gc
|
||||
}
|
||||
|
||||
func (gc *GarbageCollector) Run(ctx context.Context) error {
|
||||
gc.logger.InfoCtx(ctx, "saml garbage collector starting")
|
||||
|
||||
@@ -55,12 +71,15 @@ func (gc *GarbageCollector) Run(ctx context.Context) error {
|
||||
gc.logger.ErrorCtx(ctx, "cannot run initial cleanup", log.Error(err))
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(gc.interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
gc.logger.InfoCtx(ctx, "saml garbage collector shutting down")
|
||||
return ctx.Err()
|
||||
case <-time.After(gc.interval):
|
||||
case <-ticker.C:
|
||||
if err := gc.cleanup(ctx); err != nil {
|
||||
gc.logger.ErrorCtx(ctx, "cannot run periodic cleanup", log.Error(err))
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/crewjam/saml"
|
||||
@@ -70,29 +71,25 @@ func NewService(
|
||||
}
|
||||
|
||||
func (s *Service) Run(ctx context.Context) error {
|
||||
gc := NewGarbageCollector(s.pg, DefaultGarbageCollectionInterval, s.logger)
|
||||
wg := sync.WaitGroup{}
|
||||
ctx, cancel := context.WithCancelCause(ctx)
|
||||
defer cancel(context.Canceled)
|
||||
|
||||
gcCtx, stopGC := context.WithCancel(ctx)
|
||||
defer stopGC()
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
errCh <- gc.Run(gcCtx)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
stopGC()
|
||||
<-errCh
|
||||
return ctx.Err()
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
s.logger.ErrorCtx(ctx, "saml garbage collector failed", log.Error(err))
|
||||
return err
|
||||
gcCtx, stopGC := context.WithCancel(context.WithoutCancel(ctx))
|
||||
gc := NewGarbageCollector(s.pg, s.logger)
|
||||
wg.Go(func() {
|
||||
if err := gc.Run(gcCtx); err != nil {
|
||||
cancel(fmt.Errorf("saml garbage collector crashed: %w", err))
|
||||
}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
<-ctx.Done()
|
||||
|
||||
stopGC()
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return context.Cause(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) GenerateSpMetadata() ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user