Closed mihaitodor closed 6 months ago
Update: I also investigated switching to the new github.com/nats-io/nats.go/jetstream
API and I can still reproduce the issue with the following code:
package main
import (
"context"
"log"
"time"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)
func main() {
conn, err := nats.Connect("localhost:4222")
if err != nil {
log.Fatal(err)
}
js, err := jetstream.New(conn)
if err != nil {
log.Fatal(err)
}
kv, err := js.KeyValue(context.Background(), "foobar")
if err != nil {
log.Fatal(err)
}
// watcher, err := kv.Watch(ctx, "001.>")
watcher, err := kv.Watch(context.Background(), "\"001.>\"")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := watcher.Stop(); err != nil {
log.Fatal(err)
}
}()
var keys []any
ctx, done := context.WithTimeout(context.Background(), 3*time.Second)
defer done()
loop:
for {
select {
case entry := <-watcher.Updates():
if entry == nil {
break loop
}
keys = append(keys, entry.Key())
case <-ctx.Done():
log.Fatal("timeout")
}
}
log.Printf("%v", keys)
}
I can, however, now pass a context to kv.Watch()
which makes it easier to abort after a while, but I still think the kv.Watch()
call should reject invalid keys.
Also, the nats: jetstream not enabled
error is still returned for kv.Watch(context.Background(), "")
.
Thanks for the detailed report!
We will take a look soon.
@mihaitodor there were 2 issues:
Watch("\"001.>\"")
was a server issue, fixed here: https://github.com/nats-io/nats-server/pull/5318Watch("")
returned wrong error due to lacking client-side validation: #1613 Both will now return an error since both are essentially invalid arguments: nats: invalid key: keys cannot be empty and must be a valid NATS subject
.
That's awesome, thank you very much for the quick fixes!
Both PRs are merged and will be included in the next releases of nats-server and nats.go.
Observed behavior
The
kv.Watch("\"001.>\"")
call blocks forever if there's a key starting with001.
in Nats KV.Additionally, if you also try
kv.Watch("")
, you get a rather cryptic error:nats: jetstream not enabled
.Expected behavior
kv.Watch()
should reject invalid keys or, at the very least, it shouldn't block forever.Also,
kv.Watch("")
should return a more informative error message, maybe saying that thekeys
parameter cannot be set to an empty string.Server and client version
nats:2.10.12
Docker imagegithub.com/nats-io/nats.go
v1.34.1
client librariesv0.1.4
v1.22.1
(darwin/arm64
)Host environment
OSX Sonoma 14.4.1 on Macbook Pro M3 Max
Steps to reproduce
Run the following shell commands:
Create a
go.mod
file with the following contents:And then write the following code in a file called
main.go
:If you execute
go mod tidy && go run main.go
, you'll see a message containingtimeout
. If you uncomment line 27 and comment out line 28, you should see[001.123]
instead.Additionally, if you try
kv.Watch("")
, you'll get a rather cryptic error:nats: jetstream not enabled
.(cc @codegangsta)