-
Notifications
You must be signed in to change notification settings - Fork 7
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
Make server logic vector clock-aware #153
Merged
richardhuaaa
merged 3 commits into
main
from
09-09-make_server_logic_vector_clock-aware
Sep 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -8,7 +8,11 @@ import ( | |
"go.uber.org/zap" | ||
) | ||
|
||
type PollableDBQuery[ValueType any] func(ctx context.Context, lastSeenID int64, numRows int32) (results []ValueType, lastID int64, err error) | ||
type PollableDBQuery[ValueType any, CursorType any] func( | ||
ctx context.Context, | ||
lastSeen CursorType, | ||
numRows int32, | ||
) (results []ValueType, nextCursor CursorType, err error) | ||
Comment on lines
+11
to
+15
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. Main change is here - |
||
|
||
// Poll whenever notified, or at an interval if not notified | ||
type PollingOptions struct { | ||
|
@@ -17,33 +21,33 @@ type PollingOptions struct { | |
NumRows int32 | ||
} | ||
|
||
type DBSubscription[ValueType any] struct { | ||
ctx context.Context | ||
log *zap.Logger | ||
lastSeenID int64 | ||
options PollingOptions | ||
query PollableDBQuery[ValueType] | ||
updates chan<- []ValueType | ||
type DBSubscription[ValueType any, CursorType any] struct { | ||
ctx context.Context | ||
log *zap.Logger | ||
lastSeen CursorType | ||
options PollingOptions | ||
query PollableDBQuery[ValueType, CursorType] | ||
updates chan<- []ValueType | ||
} | ||
|
||
func NewDBSubscription[ValueType any]( | ||
func NewDBSubscription[ValueType any, CursorType any]( | ||
ctx context.Context, | ||
log *zap.Logger, | ||
query PollableDBQuery[ValueType], | ||
lastSeenID int64, | ||
query PollableDBQuery[ValueType, CursorType], | ||
lastSeen CursorType, | ||
options PollingOptions, | ||
) *DBSubscription[ValueType] { | ||
return &DBSubscription[ValueType]{ | ||
ctx: ctx, | ||
log: log, | ||
lastSeenID: lastSeenID, | ||
options: options, | ||
query: query, | ||
updates: nil, | ||
) *DBSubscription[ValueType, CursorType] { | ||
return &DBSubscription[ValueType, CursorType]{ | ||
ctx: ctx, | ||
log: log, | ||
lastSeen: lastSeen, | ||
options: options, | ||
query: query, | ||
updates: nil, | ||
} | ||
} | ||
|
||
func (s *DBSubscription[ValueType]) Start() (<-chan []ValueType, error) { | ||
func (s *DBSubscription[ValueType, CursorType]) Start() (<-chan []ValueType, error) { | ||
if s.updates != nil { | ||
return nil, fmt.Errorf("Already started") | ||
} | ||
|
@@ -75,25 +79,24 @@ func (s *DBSubscription[ValueType]) Start() (<-chan []ValueType, error) { | |
return updates, nil | ||
} | ||
|
||
func (s *DBSubscription[ValueType]) poll() { | ||
func (s *DBSubscription[ValueType, CursorType]) poll() { | ||
// Repeatedly query page by page until no more results | ||
for { | ||
results, lastID, err := s.query(s.ctx, s.lastSeenID, s.options.NumRows) | ||
results, lastID, err := s.query(s.ctx, s.lastSeen, s.options.NumRows) | ||
if s.ctx.Err() != nil { | ||
break | ||
} else if err != nil { | ||
s.log.Error( | ||
"Error querying for DB subscription", | ||
zap.Error(err), | ||
zap.Int64("lastSeenID", s.lastSeenID), | ||
zap.Any("lastSeen", s.lastSeen), | ||
zap.Int32("numRows", s.options.NumRows), | ||
) | ||
// Did not update lastSeenID; will retry on next poll | ||
// Did not update lastSeen; will retry on next poll | ||
break | ||
} else if len(results) == 0 { | ||
break | ||
} | ||
s.lastSeenID = lastID | ||
s.lastSeen = lastID | ||
s.updates <- results | ||
if int32(len(results)) < s.options.NumRows { | ||
break | ||
|
Oops, something went wrong.
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.
I added this mainly to stop malicious actors from flooding our DB with large requests using huge vector clock queries. We could theoretically make this bigger
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.
In practice, this should always be < the number of nodes in the registry. Maybe that can be the upper bound?