-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add remote and local listener support #83
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 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0fc8dd5
Add remote and local listener support
hfhbd 3f3776c
Update PostgresNativeDriver.kt
hfhbd 5c944a2
Use Ktor instead implementing cancellation support with self-piping
hfhbd 3045701
Merge remote-tracking branch 'origin/hfhbd/remote-listener' into hfhb…
hfhbd a740cdc
Merge branch 'main' into hfhbd/remote-listener
hfhbd 3d3f752
Fix canceling
hfhbd 53be557
Add license
hfhbd 104033d
Make ScopedListenerSupport internal, we could make it public later if…
hfhbd 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
71 changes: 71 additions & 0 deletions
71
...ht-driver/src/commonMain/kotlin/app/softwork/sqldelight/postgresdriver/ListenerSupport.kt
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,71 @@ | ||
package app.softwork.sqldelight.postgresdriver | ||
|
||
import kotlinx.cinterop.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import libpq.* | ||
import platform.posix.* | ||
import kotlin.coroutines.* | ||
import kotlin.time.* | ||
|
||
public sealed interface ListenerSupport { | ||
public sealed interface ScopedListenerSupport : ListenerSupport { | ||
public val notificationScope: CoroutineScope | ||
} | ||
|
||
public companion object { | ||
public fun Local(notificationScope: CoroutineScope): Local { | ||
val notifications = MutableSharedFlow<String>() | ||
return Local(notificationScope, notifications) { notifications.emit(it) } | ||
} | ||
} | ||
|
||
public class Local( | ||
override val notificationScope: CoroutineScope, | ||
public val notifications: Flow<String>, | ||
public val notify: suspend (String) -> Unit | ||
) : ScopedListenerSupport | ||
|
||
public class Remote( | ||
override val notificationScope: CoroutineScope, | ||
public val notificationName: (String) -> String = { it } | ||
) : ScopedListenerSupport { | ||
internal fun remoteListener(conn: CPointer<PGconn>): Flow<String> = channelFlow { | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
val selectorContext = newSingleThreadContext("WorkerSelectorManager") | ||
val job = Job() | ||
val coroutineContext = selectorContext + job | ||
|
||
try { | ||
val socket = PQsocket(conn) | ||
check(socket >= 0) { | ||
"Error while connecting to the PostgreSql socket" | ||
} | ||
val pollfd: pollfd = memScoped { alloc() } | ||
pollfd.fd = socket | ||
val timeoutMs = 500 | ||
|
||
while (true) { | ||
withContext(coroutineContext) { | ||
@OptIn(UnsafeNumber::class) | ||
poll(pollfd.ptr, 1, timeoutMs) | ||
} | ||
PQconsumeInput(conn) | ||
var notification: PGnotify? = null | ||
while (PQnotifies(conn)?.pointed?.also { notification = it } != null) { | ||
notification?.let { | ||
val tableName = it.relname!!.toKString() | ||
hfhbd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PQfreemem(it.ptr) | ||
send(tableName) | ||
} | ||
} | ||
} | ||
} finally { | ||
job.cancel() | ||
selectorContext.close() | ||
} | ||
}.shareIn(notificationScope, SharingStarted.WhileSubscribed(replayExpiration = Duration.ZERO)) | ||
} | ||
|
||
public object None : ListenerSupport | ||
} |
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
12 changes: 10 additions & 2 deletions
12
testing/src/commonMain/sqldelight/app/softwork/sqldelight/postgresdriver/Users.sq
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 |
---|---|---|
@@ -1,9 +1,17 @@ | ||
insertAndGet: | ||
INSERT INTO users(email, username, bio, image) | ||
VALUES (?, ?, ?, ?) | ||
INSERT INTO users(email, username, bio, image, fooID) | ||
VALUES (?, ?, ?, ?, ?) | ||
RETURNING id; | ||
|
||
selectByUsername: | ||
SELECT email, username, bio, image | ||
FROM users | ||
WHERE username = :username; | ||
|
||
selectByFoo: | ||
SELECT * FROM users WHERE fooID = :fooID; | ||
|
||
updateWhereFoo: | ||
UPDATE users | ||
SET email = :newEmail | ||
WHERE fooID = :fooID; |
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.