forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

jetstream: fix data race filtering events by did

Signed-off-by: Thomas Karpiniec <tkarpiniec@icloud.com>

Changed files
+15 -4
jetstream
+15 -4
jetstream/jetstream.go
···
// existing instances of the closure when j.WantedDids is mutated
return func(ctx context.Context, evt *models.Event) error {
+
j.mu.RLock()
// empty filter => all dids allowed
-
if len(j.wantedDids) == 0 {
-
return processFunc(ctx, evt)
+
matches := len(j.wantedDids) == 0
+
if !matches {
+
if _, ok := j.wantedDids[evt.Did]; ok {
+
matches = true
+
}
}
+
j.mu.RUnlock()
-
if _, ok := j.wantedDids[evt.Did]; ok {
+
if matches {
return processFunc(ctx, evt)
} else {
return nil
···
go func() {
if j.waitForDid {
-
for len(j.wantedDids) == 0 {
+
for {
+
j.mu.RLock()
+
hasDid := len(j.wantedDids) != 0
+
j.mu.RUnlock()
+
if hasDid {
+
break
+
}
time.Sleep(time.Second)
}
}