1
0
Fork 0
mirror of https://github.com/pomerium/pomerium.git synced 2025-05-25 06:57:11 +02:00
pomerium/internal/zero/controller/leaser.go
Joe Kralicky fe31799eb5
Fix many instances of contexts and loggers not being propagated ()
This also replaces instances where we manually write "return ctx.Err()"
with "return context.Cause(ctx)" which is functionally identical, but
will also correctly propagate cause errors if present.
2024-10-25 14:50:56 -04:00

60 lines
1.4 KiB
Go

package controller
import (
"context"
"sync/atomic"
"time"
"golang.org/x/sync/errgroup"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
)
type leaser struct {
client databroker.DataBrokerServiceClient
funcs []func(context.Context, databroker.DataBrokerServiceClient) error
}
// GetDataBrokerServiceClient implements the databroker.LeaseHandler interface.
func (c *leaser) GetDataBrokerServiceClient() databroker.DataBrokerServiceClient {
return c.client
}
// RunLeased implements the databroker.LeaseHandler interface.
func (c *leaser) RunLeased(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
for _, fn := range c.funcs {
fn := fn
eg.Go(func() error { return fn(ctx, c.client) })
}
err := eg.Wait()
return err
}
func WithLease(
funcs ...func(context.Context, databroker.DataBrokerServiceClient) error,
) func(context.Context, databroker.DataBrokerServiceClient) error {
return func(ctx context.Context, client databroker.DataBrokerServiceClient) error {
srv := &leaser{
client: client,
funcs: funcs,
}
leaser := databroker.NewLeaser("zero-ctrl", time.Second*30, srv)
return leaser.Run(ctx)
}
}
type LeaseStatus struct {
v atomic.Bool
}
func (w *LeaseStatus) HasLease() bool {
return w.v.Load()
}
func (w *LeaseStatus) MonitorLease(ctx context.Context, _ databroker.DataBrokerServiceClient) error {
w.v.Store(true)
<-ctx.Done()
w.v.Store(false)
return context.Cause(ctx)
}