-
Notifications
You must be signed in to change notification settings - Fork 23
Add NewSyncedDatabaseConnector
#64
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79d26c3
chore: update header
levydsa 0beaff9
feat: use libsql_open_sync_with_config with explicit offline param
levydsa e6c7599
feat: add test workflow
levydsa e1893c9
feat: add sqld to the CI environment
levydsa 148b4cb
chore(ci): bump go version
levydsa 550f5ea
feat(ci): add libsql-server version as workflow_dispatch input
levydsa 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test: | ||
strategy: | ||
matrix: | ||
go-version: [ '1.20' ] | ||
os: [ubuntu-latest, macos-latest] | ||
libsql-server-release: [libsql-server-v0.24.32] | ||
levydsa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
|
||
- name: Install sqld | ||
run: | | ||
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/tursodatabase/libsql/releases/download/${{ matrix.libsql-server-release }}/libsql-server-installer.sh | sh | ||
echo "$HOME/.sqld/bin" >> $GITHUB_PATH | ||
sqld --version | ||
|
||
- name: Start sqld server | ||
run: | | ||
sqld & | ||
while ! curl -s http://localhost:8080/health > /dev/null; do | ||
echo "Waiting for sqld..." | ||
sleep 1 | ||
done | ||
echo "sqld is ready!" | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
env: | ||
LIBSQL_PRIMARY_URL: "http://localhost:8080" | ||
LIBSQL_AUTH_TOKEN: "" | ||
run: go test -v ./... |
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 | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -54,7 +54,7 @@ type Option interface { | |||||||||||||||||||||||||||||||||||||
type option func(*config) error | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
type Replicated struct { | ||||||||||||||||||||||||||||||||||||||
FrameNo int | ||||||||||||||||||||||||||||||||||||||
FrameNo int | ||||||||||||||||||||||||||||||||||||||
FramesSynced int | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
@@ -135,7 +135,37 @@ func NewEmbeddedReplicaConnector(dbPath string, primaryUrl string, opts ...Optio | |||||||||||||||||||||||||||||||||||||
if config.syncInterval != nil { | ||||||||||||||||||||||||||||||||||||||
syncInterval = *config.syncInterval | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
return openEmbeddedReplicaConnector(dbPath, primaryUrl, authToken, readYourWrites, encryptionKey, syncInterval) | ||||||||||||||||||||||||||||||||||||||
return openSyncConnector(dbPath, primaryUrl, authToken, readYourWrites, encryptionKey, syncInterval, false) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
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. [nitpick] Add a Go doc comment for
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||
func NewSyncedDatabaseConnector(dbPath string, primaryUrl string, opts ...Option) (*Connector, error) { | ||||||||||||||||||||||||||||||||||||||
var config config | ||||||||||||||||||||||||||||||||||||||
errs := make([]error, 0, len(opts)) | ||||||||||||||||||||||||||||||||||||||
for _, opt := range opts { | ||||||||||||||||||||||||||||||||||||||
if err := opt.apply(&config); err != nil { | ||||||||||||||||||||||||||||||||||||||
errs = append(errs, err) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
if len(errs) > 0 { | ||||||||||||||||||||||||||||||||||||||
return nil, errors.Join(errs...) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
authToken := "" | ||||||||||||||||||||||||||||||||||||||
if config.authToken != nil { | ||||||||||||||||||||||||||||||||||||||
authToken = *config.authToken | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
readYourWrites := true | ||||||||||||||||||||||||||||||||||||||
if config.readYourWrites != nil { | ||||||||||||||||||||||||||||||||||||||
readYourWrites = *config.readYourWrites | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
encryptionKey := "" | ||||||||||||||||||||||||||||||||||||||
if config.encryptionKey != nil { | ||||||||||||||||||||||||||||||||||||||
encryptionKey = *config.encryptionKey | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
syncInterval := time.Duration(0) | ||||||||||||||||||||||||||||||||||||||
if config.syncInterval != nil { | ||||||||||||||||||||||||||||||||||||||
syncInterval = *config.syncInterval | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
return openSyncConnector(dbPath, primaryUrl, authToken, readYourWrites, encryptionKey, syncInterval, true) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
type driver struct{} | ||||||||||||||||||||||||||||||||||||||
|
@@ -173,7 +203,7 @@ func (d driver) OpenConnector(dbAddress string) (sqldriver.Connector, error) { | |||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func libsqlSync(nativeDbPtr C.libsql_database_t) (Replicated, error) { | ||||||||||||||||||||||||||||||||||||||
var errMsg *C.char | ||||||||||||||||||||||||||||||||||||||
var rep C.replicated; | ||||||||||||||||||||||||||||||||||||||
var rep C.replicated | ||||||||||||||||||||||||||||||||||||||
statusCode := C.libsql_sync2(nativeDbPtr, &rep, &errMsg) | ||||||||||||||||||||||||||||||||||||||
if statusCode != 0 { | ||||||||||||||||||||||||||||||||||||||
return Replicated{0, 0}, libsqlError("failed to sync database ", statusCode, errMsg) | ||||||||||||||||||||||||||||||||||||||
|
@@ -198,10 +228,10 @@ func openRemoteConnector(primaryUrl, authToken string) (*Connector, error) { | |||||||||||||||||||||||||||||||||||||
return &Connector{nativeDbPtr: nativeDbPtr}, nil | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func openEmbeddedReplicaConnector(dbPath, primaryUrl, authToken string, readYourWrites bool, encryptionKey string, syncInterval time.Duration) (*Connector, error) { | ||||||||||||||||||||||||||||||||||||||
func openSyncConnector(dbPath, primaryUrl, authToken string, readYourWrites bool, encryptionKey string, syncInterval time.Duration, offline bool) (*Connector, error) { | ||||||||||||||||||||||||||||||||||||||
var closeCh chan struct{} | ||||||||||||||||||||||||||||||||||||||
var closeAckCh chan struct{} | ||||||||||||||||||||||||||||||||||||||
nativeDbPtr, err := libsqlOpenWithSync(dbPath, primaryUrl, authToken, readYourWrites, encryptionKey) | ||||||||||||||||||||||||||||||||||||||
nativeDbPtr, err := libsqlOpenWithSync(dbPath, primaryUrl, authToken, readYourWrites, encryptionKey, offline) | ||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
@@ -309,7 +339,7 @@ func libsqlOpenRemote(url, authToken string) (C.libsql_database_t, error) { | |||||||||||||||||||||||||||||||||||||
return db, nil | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
func libsqlOpenWithSync(dbPath, primaryUrl, authToken string, readYourWrites bool, encryptionKey string) (C.libsql_database_t, error) { | ||||||||||||||||||||||||||||||||||||||
func libsqlOpenWithSync(dbPath, primaryUrl, authToken string, readYourWrites bool, encryptionKey string, offline bool) (C.libsql_database_t, error) { | ||||||||||||||||||||||||||||||||||||||
dbPathNativeString := C.CString(dbPath) | ||||||||||||||||||||||||||||||||||||||
defer C.free(unsafe.Pointer(dbPathNativeString)) | ||||||||||||||||||||||||||||||||||||||
primaryUrlNativeString := C.CString(primaryUrl) | ||||||||||||||||||||||||||||||||||||||
|
@@ -321,15 +351,30 @@ func libsqlOpenWithSync(dbPath, primaryUrl, authToken string, readYourWrites boo | |||||||||||||||||||||||||||||||||||||
if readYourWrites { | ||||||||||||||||||||||||||||||||||||||
readYourWritesNative = 1 | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
var offlineNative C.char = 0 | ||||||||||||||||||||||||||||||||||||||
if offline { | ||||||||||||||||||||||||||||||||||||||
offlineNative = 1 | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
var encrytionKeyNativeString *C.char | ||||||||||||||||||||||||||||||||||||||
if encryptionKey != "" { | ||||||||||||||||||||||||||||||||||||||
encrytionKeyNativeString = C.CString(encryptionKey) | ||||||||||||||||||||||||||||||||||||||
defer C.free(unsafe.Pointer(encrytionKeyNativeString)) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
config := C.libsql_config{ | ||||||||||||||||||||||||||||||||||||||
db_path: dbPathNativeString, | ||||||||||||||||||||||||||||||||||||||
auth_token: authTokenNativeString, | ||||||||||||||||||||||||||||||||||||||
primary_url: primaryUrlNativeString, | ||||||||||||||||||||||||||||||||||||||
read_your_writes: readYourWritesNative, | ||||||||||||||||||||||||||||||||||||||
encryption_key: encrytionKeyNativeString, | ||||||||||||||||||||||||||||||||||||||
offline: offlineNative, | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
var db C.libsql_database_t | ||||||||||||||||||||||||||||||||||||||
var errMsg *C.char | ||||||||||||||||||||||||||||||||||||||
statusCode := C.libsql_open_sync(dbPathNativeString, primaryUrlNativeString, authTokenNativeString, readYourWritesNative, encrytionKeyNativeString, &db, &errMsg) | ||||||||||||||||||||||||||||||||||||||
statusCode := C.libsql_open_sync_with_config(config, &db, &errMsg) | ||||||||||||||||||||||||||||||||||||||
if statusCode != 0 { | ||||||||||||||||||||||||||||||||||||||
return nil, libsqlError(fmt.Sprintf("failed to open database %s %s", dbPath, primaryUrl), statusCode, errMsg) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
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.