Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zero: group funcs that need run within a lease (#4862)
- Loading branch information
Showing
6 changed files
with
135 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Package leaser groups all Zero services that should run within a lease. | ||
package leaser | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/pomerium/pomerium/pkg/grpc/databroker" | ||
) | ||
|
||
type service struct { | ||
client databroker.DataBrokerServiceClient | ||
funcs []func(ctx context.Context) error | ||
} | ||
|
||
// GetDataBrokerServiceClient implements the databroker.LeaseHandler interface. | ||
func (c *service) GetDataBrokerServiceClient() databroker.DataBrokerServiceClient { | ||
return c.client | ||
} | ||
|
||
// RunLeased implements the databroker.LeaseHandler interface. | ||
func (c *service) RunLeased(ctx context.Context) error { | ||
eg, ctx := errgroup.WithContext(ctx) | ||
for _, fn := range c.funcs { | ||
fn := fn | ||
eg.Go(func() error { | ||
return fn(ctx) | ||
}) | ||
} | ||
return eg.Wait() | ||
} | ||
|
||
// Run runs services within a lease | ||
func Run( | ||
ctx context.Context, | ||
client databroker.DataBrokerServiceClient, | ||
funcs ...func(ctx context.Context) error, | ||
) error { | ||
srv := &service{ | ||
client: client, | ||
funcs: funcs, | ||
} | ||
leaser := databroker.NewLeaser("zero-ctrl", time.Second*30, srv) | ||
return RunWithRestart(ctx, func(ctx context.Context) error { | ||
return leaser.Run(ctx) | ||
}, srv.databrokerChangeMonitor) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package leaser | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pomerium/pomerium/pkg/grpc/databroker" | ||
) | ||
|
||
const typeStr = "pomerium.io/zero/leaser" | ||
|
||
// databrokerChangeMonitor runs infinite sync loop to see if there is any change in databroker | ||
// it doesn't really syncs anything, just checks if the underlying databroker has changed | ||
func (c *service) databrokerChangeMonitor(ctx context.Context) error { | ||
_, recordVersion, serverVersion, err := databroker.InitialSync(ctx, c.GetDataBrokerServiceClient(), &databroker.SyncLatestRequest{ | ||
Type: typeStr, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error during initial sync: %w", err) | ||
} | ||
|
||
stream, err := c.GetDataBrokerServiceClient().Sync(ctx, &databroker.SyncRequest{ | ||
Type: typeStr, | ||
ServerVersion: serverVersion, | ||
RecordVersion: recordVersion, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error calling sync: %w", err) | ||
} | ||
|
||
for { | ||
_, err := stream.Recv() | ||
if err != nil { | ||
return fmt.Errorf("error receiving record: %w", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters