-
Notifications
You must be signed in to change notification settings - Fork 1
Add Support for Querying Against Read Replicas #70
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
Open
smst-jeff
wants to merge
9
commits into
main
Choose a base branch
from
jeff/read-replicas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+450
−27
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6158cee
close rows resource
smst-jeff 8a54a74
clean up comments
smst-jeff 7369d68
add support for querying against read replicas
smst-jeff f967930
lint
smst-jeff 3670fb0
lint
smst-jeff 2902288
Update read_replicas_test.go
smst-jeff 770eab9
Update main_test.go
smst-jeff 10d4e70
Update main_test.go
smst-jeff 247b790
lint
smst-jeff 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package sequel | ||
|
|
||
| import ( | ||
| "errors" | ||
| "sync" | ||
|
|
||
| "github.com/go-sqlx/sqlx" | ||
| ) | ||
|
|
||
| var ErrNoReadReplicaConnection = errors.New("no read replica connections") | ||
|
|
||
| type readReplica struct { | ||
| db *sqlx.DB | ||
|
|
||
| next *readReplica | ||
| } | ||
|
|
||
| // readReplicas contains a set of DB connections. It is intended to give fair round robin access | ||
| // through a circular singularly linked list. | ||
| // | ||
| // Replicas are appended after the current one. The intended use is to build the replica set before | ||
| // querying, but all operations are concurrent-safe. | ||
| type readReplicas struct { | ||
| m sync.Mutex | ||
|
|
||
| current *readReplica | ||
| } | ||
|
|
||
| // add adds a DB to the collection of read replicas | ||
| func (rr *readReplicas) add(db *sqlx.DB) { | ||
| rr.m.Lock() | ||
| defer rr.m.Unlock() | ||
|
|
||
| r := readReplica{ | ||
| db: db, | ||
| } | ||
|
|
||
| // Empty ring, add new DB | ||
| if rr.current == nil { | ||
| r.next = &r | ||
|
|
||
| rr.current = &r | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Insert new db after current | ||
| n := rr.current.next | ||
| r.next = n | ||
| rr.current.next = &r | ||
| } | ||
|
|
||
| // next returns the current DB. The current pointer is advanced. | ||
| func (rr *readReplicas) next() (*sqlx.DB, error) { | ||
| rr.m.Lock() | ||
| defer rr.m.Unlock() | ||
|
|
||
| if rr.current == nil { | ||
| return nil, ErrNoReadReplicaConnection | ||
| } | ||
|
|
||
| c := rr.current | ||
|
|
||
| rr.current = rr.current.next | ||
|
|
||
| return c.db, nil | ||
| } | ||
|
|
||
| // Close closes all read replica connections | ||
| func (rr *readReplicas) Close() { | ||
| rr.m.Lock() | ||
| defer rr.m.Unlock() | ||
|
|
||
| // If this instance has no replicas, current would be nil | ||
| if rr.current == nil { | ||
| return | ||
| } | ||
|
|
||
| first := rr.current | ||
| for c := first; ; c = c.next { | ||
| c.db.Close() | ||
|
|
||
| if c.next == first { | ||
| break | ||
| } | ||
| } | ||
| } | ||
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,145 @@ | ||
| package sequel | ||
|
|
||
| import ( | ||
| "errors" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/go-sqlx/sqlx" | ||
| ) | ||
|
|
||
| func Test_readReplicas_add(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| rrs []*sqlx.DB | ||
| }{ | ||
| { | ||
| name: "add single", | ||
| rrs: []*sqlx.DB{sqlx.NewDb(nil, "fakeDriver")}, | ||
| }, | ||
| { | ||
| name: "add multiple", | ||
| rrs: []*sqlx.DB{sqlx.NewDb(nil, "fakeDriver"), sqlx.NewDb(nil, "fakeDriver"), sqlx.NewDb(nil, "fakeDriver")}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Create readReplicas | ||
| rr := &readReplicas{} | ||
|
|
||
| // Add connections to read replicas | ||
| for _, c := range tt.rrs { | ||
| rr.add(c) | ||
| } | ||
|
|
||
| // Confirm they are all added | ||
| if !rrContains(t, rr, tt.rrs) { | ||
| t.Fatal("readReplicas does not contain all added conns") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_readReplicas_next(t *testing.T) { | ||
| newRRs := func(rrs ...*sqlx.DB) *readReplicas { | ||
| rr := &readReplicas{} | ||
| for _, c := range rrs { | ||
| rr.add(c) | ||
| } | ||
|
|
||
| return rr | ||
| } | ||
|
|
||
| c1, c2, c3 := sqlx.NewDb(nil, "fakeDriver"), sqlx.NewDb(nil, "fakeDriver"), sqlx.NewDb(nil, "fakeDriver") | ||
|
|
||
| emptyRR := newRRs() | ||
| singleRR := newRRs(c1) | ||
| threeRR := newRRs(c1, c2, c3) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| rr *readReplicas | ||
| want *sqlx.DB | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "empty readReplicas", | ||
| rr: emptyRR, | ||
| want: nil, | ||
| wantErr: ErrNoReadReplicaConnection, | ||
| }, | ||
| { | ||
| name: "one read replica", | ||
| rr: singleRR, | ||
| want: c1, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "one read replica (2 calls)", | ||
| rr: singleRR, | ||
| want: c1, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "one read replica (3 calls)", | ||
| rr: singleRR, | ||
| want: c1, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "three read replicas", | ||
| rr: threeRR, | ||
| want: c1, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "three read replicas (2 calls)", | ||
| rr: threeRR, | ||
| want: c3, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "three read replicas (3 calls)", | ||
| rr: threeRR, | ||
| want: c2, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "three read replicas (4 calls)", | ||
| rr: threeRR, | ||
| want: c1, | ||
| wantErr: nil, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := tt.rr.next() | ||
| if !errors.Is(err, tt.wantErr) { | ||
| t.Fatalf("got err %v, expecting %v", err, tt.wantErr) | ||
| } | ||
|
|
||
| if got != tt.want { | ||
| t.Fatalf("got rr %p, want %p", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // rrContains is a test helper to see if a [readReplicas] contains all the connections passed in | ||
| func rrContains(t *testing.T, rr *readReplicas, conns []*sqlx.DB) bool { | ||
| t.Helper() | ||
|
|
||
| var findCnt int | ||
| head := rr.current | ||
| for c := head.next; ; c = c.next { | ||
| if slices.Contains(conns, c.db) { | ||
| findCnt++ | ||
| } | ||
|
|
||
| if c == head { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return findCnt == len(conns) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.