oauth: automatically clean up expired oauth state in db
This commit is contained in:
@@ -48,13 +48,17 @@ func runApp(ctx context.Context) error {
|
||||
return fmt.Errorf("failed to initialize database connection pool: %w", err)
|
||||
}
|
||||
|
||||
// start http server
|
||||
// start background processes
|
||||
|
||||
auth, err := authentication.New(ctx, connPool)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to construct authenticator: %w", err)
|
||||
}
|
||||
|
||||
authErrCh := runAuthProcesses(ctx, auth)
|
||||
|
||||
// start http server
|
||||
|
||||
srvErrCh := runServer(ctx, connPool, auth)
|
||||
|
||||
// wait for interrupt signal or unrecoverable failure, then shutdown
|
||||
@@ -62,13 +66,24 @@ func runApp(ctx context.Context) error {
|
||||
osSignalCh := make(chan os.Signal, 1)
|
||||
signal.Notify(osSignalCh, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
var serverAlreadyShutdown bool
|
||||
var (
|
||||
alreadyShutdown struct {
|
||||
server bool
|
||||
authProcesses bool
|
||||
}
|
||||
)
|
||||
select {
|
||||
case s := <-osSignalCh:
|
||||
fmt.Println("application received shutdown signal:", s)
|
||||
fmt.Println("shutting down")
|
||||
case err := <-authErrCh:
|
||||
alreadyShutdown.authProcesses = true
|
||||
fmt.Println("auth processes shutdown unexpectedly")
|
||||
if err != nil {
|
||||
fmt.Println("auth processes encountered error:", err)
|
||||
}
|
||||
case err := <-srvErrCh:
|
||||
serverAlreadyShutdown = true
|
||||
alreadyShutdown.server = true
|
||||
fmt.Println("server shutdown unexpectedly")
|
||||
if err != nil {
|
||||
fmt.Println("server encountered error:", err)
|
||||
@@ -81,7 +96,14 @@ func runApp(ctx context.Context) error {
|
||||
|
||||
var errs []error
|
||||
|
||||
if !serverAlreadyShutdown {
|
||||
if !alreadyShutdown.authProcesses {
|
||||
if err := <-authErrCh; err != nil {
|
||||
errs = append(errs, fmt.Errorf("auth processes experienced an error: %w", err))
|
||||
}
|
||||
fmt.Println("auth processes shut down")
|
||||
}
|
||||
|
||||
if !alreadyShutdown.server {
|
||||
if err := <-srvErrCh; err != nil {
|
||||
errs = append(errs, fmt.Errorf("server experienced an error: %w", err))
|
||||
}
|
||||
@@ -91,6 +113,19 @@ func runApp(ctx context.Context) error {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func runAuthProcesses(ctx context.Context, auth *authentication.Authenticator) <-chan error {
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
defer close(errCh)
|
||||
|
||||
if err := auth.RunBackgroundCleanup(ctx); err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
return errCh
|
||||
}
|
||||
|
||||
func runServer(ctx context.Context, connPool *pgxpool.Pool, auth *authentication.Authenticator) <-chan error {
|
||||
srv := &http.Server{
|
||||
Addr: ":8082", // local
|
||||
|
||||
Reference in New Issue
Block a user