-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Adding premium plugin support for updating usage and monitoring quota #1330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af0b36f
Fixing API to match the example usage
mnorbury cd44d16
Add a quota monitoring function
mnorbury 2acf25b
The updater should use its own context as it should continue after th…
mnorbury 2fe091d
go mod tidy
mnorbury 0024d34
Add utility function to make all tables paid
mnorbury 27ff2e1
Add OnSyncFinish hook
mnorbury 06ff9a9
Allow resource channel to be cancelled
mnorbury 68aade0
Add `MakeTablePaid` helper function
mnorbury File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
| package premium | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
| ) | ||
|
|
||
| var ErrNoQuota = errors.New("no remaining quota for the month, please increase your usage limit if you want to continue syncing this plugin") | ||
|
|
||
| const DefaultQuotaCheckInterval = 30 * time.Second | ||
|
|
||
| type quotaChecker struct { | ||
| qm QuotaMonitor | ||
| duration time.Duration | ||
| } | ||
|
|
||
| type QuotaCheckOption func(*quotaChecker) | ||
|
|
||
| // WithQuotaCheckPeriod the time interval between quota checks | ||
| func WithQuotaCheckPeriod(duration time.Duration) QuotaCheckOption { | ||
| return func(m *quotaChecker) { | ||
| m.duration = duration | ||
| } | ||
| } | ||
|
|
||
| // WithCancelOnQuotaExceeded monitors the quota usage at intervals defined by duration and cancels the context if the quota is exceeded | ||
| func WithCancelOnQuotaExceeded(ctx context.Context, qm QuotaMonitor, ops ...QuotaCheckOption) (context.Context, func(), error) { | ||
| m := quotaChecker{ | ||
| qm: qm, | ||
| duration: DefaultQuotaCheckInterval, | ||
| } | ||
| for _, op := range ops { | ||
| op(&m) | ||
| } | ||
|
|
||
| if err := m.checkInitialQuota(ctx); err != nil { | ||
| return ctx, nil, err | ||
| } | ||
|
|
||
| ctx, cancel := m.startQuotaMonitor(ctx) | ||
|
|
||
| return ctx, cancel, nil | ||
| } | ||
|
|
||
| func (qc quotaChecker) checkInitialQuota(ctx context.Context) error { | ||
| hasQuota, err := qc.qm.HasQuota(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !hasQuota { | ||
| return ErrNoQuota | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (qc quotaChecker) startQuotaMonitor(ctx context.Context) (context.Context, func()) { | ||
| newCtx, cancel := context.WithCancel(ctx) | ||
| go func() { | ||
| defer cancel() | ||
| ticker := time.NewTicker(qc.duration) | ||
| for { | ||
| select { | ||
| case <-newCtx.Done(): | ||
| return | ||
| case <-ticker.C: | ||
| hasQuota, err := qc.qm.HasQuota(newCtx) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add retries to the |
||
| if err != nil { | ||
| continue | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should we do with an error determining the quota - since returning an error here would cancel the sync. |
||
| } | ||
| if !hasQuota { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| }() | ||
| return newCtx, cancel | ||
| } | ||
This file contains hidden or 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,52 @@ | ||
| package premium | ||
|
|
||
| import ( | ||
| "context" | ||
| "github.com/stretchr/testify/require" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func newFakeQuotaMonitor(hasQuota ...bool) *fakeQuotaMonitor { | ||
| return &fakeQuotaMonitor{hasQuota: hasQuota} | ||
| } | ||
|
|
||
| type fakeQuotaMonitor struct { | ||
| hasQuota []bool | ||
| calls int | ||
| } | ||
|
|
||
| func (f *fakeQuotaMonitor) HasQuota(_ context.Context) (bool, error) { | ||
| hasQuota := f.hasQuota[f.calls] | ||
| if f.calls < len(f.hasQuota)-1 { | ||
| f.calls++ | ||
| } | ||
| return hasQuota, nil | ||
| } | ||
|
|
||
| func TestWithCancelOnQuotaExceeded_NoInitialQuota(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| _, _, err := WithCancelOnQuotaExceeded(ctx, newFakeQuotaMonitor(false)) | ||
|
|
||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestWithCancelOnQuotaExceeded_NoQuota(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| ctx, _, err := WithCancelOnQuotaExceeded(ctx, newFakeQuotaMonitor(true, false), WithQuotaCheckPeriod(1*time.Millisecond)) | ||
| require.NoError(t, err) | ||
|
|
||
| <-ctx.Done() | ||
| } | ||
|
|
||
| func TestWithCancelOnQuotaExceeded_HasQuotaCanceled(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| ctx, cancel, err := WithCancelOnQuotaExceeded(ctx, newFakeQuotaMonitor(true, true, true), WithQuotaCheckPeriod(1*time.Millisecond)) | ||
| require.NoError(t, err) | ||
| cancel() | ||
|
|
||
| <-ctx.Done() | ||
| } |
This file contains hidden or 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,27 @@ | ||
| package premium | ||
|
|
||
| import "github.com/cloudquery/plugin-sdk/v4/schema" | ||
|
|
||
| // ContainsPaidTables returns true if any of the tables are paid | ||
| func ContainsPaidTables(tables schema.Tables) bool { | ||
| for _, t := range tables { | ||
| if t.IsPaid { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // MakeAllTablesPaid sets all tables to paid | ||
| func MakeAllTablesPaid(tables schema.Tables) schema.Tables { | ||
| for _, table := range tables { | ||
| MakeTablePaid(table) | ||
| } | ||
| return tables | ||
| } | ||
|
|
||
| // MakeTablePaid sets the table to paid | ||
| func MakeTablePaid(table *schema.Table) *schema.Table { | ||
| table.IsPaid = true | ||
| return table | ||
| } |
This file contains hidden or 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,39 @@ | ||
| package premium | ||
|
|
||
| import ( | ||
| "github.com/cloudquery/plugin-sdk/v4/schema" | ||
| "github.com/stretchr/testify/assert" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestContainsPaidTables(t *testing.T) { | ||
| noPaidTables := schema.Tables{ | ||
| &schema.Table{Name: "table1", IsPaid: false}, | ||
| &schema.Table{Name: "table2", IsPaid: false}, | ||
| &schema.Table{Name: "table3", IsPaid: false}, | ||
| } | ||
|
|
||
| paidTables := schema.Tables{ | ||
| &schema.Table{Name: "table1", IsPaid: false}, | ||
| &schema.Table{Name: "table2", IsPaid: true}, | ||
| &schema.Table{Name: "table3", IsPaid: false}, | ||
| } | ||
|
|
||
| assert.False(t, ContainsPaidTables(noPaidTables), "no paid tables") | ||
| assert.True(t, ContainsPaidTables(paidTables), "paid tables") | ||
| } | ||
|
|
||
| func TestMakeAllTablesPaid(t *testing.T) { | ||
| noPaidTables := schema.Tables{ | ||
| &schema.Table{Name: "table1", IsPaid: false}, | ||
| &schema.Table{Name: "table2", IsPaid: false}, | ||
| &schema.Table{Name: "table3", IsPaid: false}, | ||
| } | ||
|
|
||
| paidTables := MakeAllTablesPaid(noPaidTables) | ||
|
|
||
| assert.Equal(t, 3, len(paidTables)) | ||
| for _, table := range paidTables { | ||
| assert.True(t, table.IsPaid) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically a cancelable context is created by the caller, but in this case I didn't want to make the caller (plugin developer) have to write this code.