Skip to content
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

Added UpdatesOnly to kwWatchOptions #818

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/kv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,8 @@ kvStore_WatchMulti(kvWatcher **new_watcher, kvStore *kv, const char **keys, int
{
if (opts->MetaOnly)
so.Config.HeadersOnly = true;
if (opts->UpdatesOnly)
so.Config.DeliverPolicy = js_DeliverNew;
if (opts->IgnoreDeletes)
w->ignoreDel = true;
}
Expand All @@ -1148,10 +1150,17 @@ kvStore_WatchMulti(kvWatcher **new_watcher, kvStore *kv, const char **keys, int
natsSubscription *sub = w->sub;

natsSub_Lock(sub);
if ((sub->jsi != NULL) && (sub->jsi->pending == 0))
if ((opts == NULL) || !opts->UpdatesOnly)
{
if ((sub->jsi != NULL) && (sub->jsi->pending == 0))
{
w->initDone = true;
w->retMarker = true;
}
}
else
{
w->initDone = true;
w->retMarker = true;
}
natsSub_Unlock(sub);
}
Expand Down
1 change: 1 addition & 0 deletions src/nats.h
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,7 @@ typedef struct kvWatchOptions
bool IncludeHistory;
bool MetaOnly;
int64_t Timeout; ///< How long to wait (in milliseconds) for some operations to complete.
bool UpdatesOnly; ///< Only receive updates, no initial snapshot.

} kvWatchOptions;

Expand Down
11 changes: 11 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32121,6 +32121,17 @@ void test_KeyValueWatch(void)
natsThread_Join(t);
natsThread_Destroy(t);
kvWatcher_Destroy(w);
w = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: As best practice, I would set w to NULL since you are going to recreate below. But since if it is fails to be created we fail (testCond() would exit), then there is no risk of "double free" when calling kvWatcher_Destroy() again...


// Now try with UpdatesOnly and make sure we don't get the initial values.
test("Create watcher with UpdatesOnly: ");
kvWatchOptions o = {.UpdatesOnly = true};
s = kvStore_Watch(&w, kv, "t.*", &o);
testCond(s == NATS_OK);

IFOK(s, kvStore_PutString(NULL, kv, "t.age", "57"));
testCond(_expectUpdate(w, "t.age", "57", 11));
kvWatcher_Destroy(w);
kvStore_Destroy(kv);

JS_TEARDOWN;
Expand Down