Обнаружение данных go race при настройке обработчиков для потоковой передачи nats

Для потоковой подписки nats обработчик должен выглядеть следующим образом

handler := func(ms *stan.Msg){}

Я создал следующую функцию, пытаясь взять их обработчик и затем вызвать свою собственную, чтобы обработать возвращенное сообщение.

func (t *T) Subscribe(sub Subscription, h ...message.Handler) (stan.Subscription, error) {
    process := func(ms *stan.Msg) {         
        for i := range h {
            h[i](ms)
        }
    }

    return t.subscribe(sub, process)
}

С помощью go test -race я получаю предупреждение о состоянии гонки данных вокруг области, где я зацикливаюсь / вызываю свои обработчики. Я ищу несколько предложений о том, как я могу улучшить это, чтобы убрать предупреждение о гонке.

== выход обнаружения гонки

==================
WARNING: DATA RACE
Read at 0x00c4201f8040 by goroutine 82:
  github.com/MyTest/nats/v3_test.TestDurability.func2()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:399 +0x4d
  github.com/MyTest/nats/v3.(*T).Subscribe.func1()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:222 +0x2a0
  github.com/nats-io/go-nats-streaming.(*conn).processMsg()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/stan.go:464 +0x41c
  github.com/nats-io/go-nats-streaming.(*conn).(github.com/nats-io/go-nats-streaming.processMsg)-fm()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x4b
  github.com/nats-io/go-nats.(*Conn).waitForMsgs()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:1564 +0x1eb

Previous write at 0x00c4201f8040 by goroutine 81:
  github.com/MyTest/nats/v3_test.TestDurability.func1()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:383 +0xe7
  github.com/MyTest/nats/v3.(*T).Subscribe.func1()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:222 +0x2a0
  github.com/nats-io/go-nats-streaming.(*conn).processMsg()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/stan.go:464 +0x41c
  github.com/nats-io/go-nats-streaming.(*conn).(github.com/nats-io/go-nats-streaming.processMsg)-fm()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x4b
  github.com/nats-io/go-nats.(*Conn).waitForMsgs()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:1564 +0x1eb

Goroutine 82 (running) created at:
  github.com/nats-io/go-nats.(*Conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2238 +0x3f8
  github.com/nats-io/go-nats.(*Conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2155 +0x7e
  github.com/nats-io/go-nats-streaming.(*conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x59e
  github.com/nats-io/go-nats-streaming.(*conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:185 +0xb0
  github.com/MyTest/nats/v3.(*T).subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:279 +0x1be
  github.com/MyTest/nats/v3.(*T).Subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:226 +0x1f7
  github.com/MyTest/nats/v3_test.TestDurability()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:398 +0xa1a
  testing.tRunner()
      /usr/lib64/golang/src/testing/testing.go:746 +0x16c

Goroutine 81 (finished) created at:
  github.com/nats-io/go-nats.(*Conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2238 +0x3f8
  github.com/nats-io/go-nats.(*Conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2155 +0x7e
  github.com/nats-io/go-nats-streaming.(*conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x59e
  github.com/nats-io/go-nats-streaming.(*conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:185 +0xb0
  github.com/MyTest/nats/v3.(*T).subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:279 +0x1be
  github.com/MyTest/nats/v3.(*T).Subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:226 +0x1f7
  github.com/MyTest/nats/v3_test.TestDurability()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:382 +0x606
  testing.tRunner()
      /usr/lib64/golang/src/testing/testing.go:746 +0x16c
==================
==================
WARNING: DATA RACE
Read at 0x00c4201f0000 by goroutine 82:
  runtime.growslice()
      /usr/lib64/golang/src/runtime/slice.go:82 +0x0
  github.com/MyTest/nats/v3_test.TestDurability.func2()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:399 +0x166
  github.com/MyTest/nats/v3.(*T).Subscribe.func1()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:222 +0x2a0
  github.com/nats-io/go-nats-streaming.(*conn).processMsg()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/stan.go:464 +0x41c
  github.com/nats-io/go-nats-streaming.(*conn).(github.com/nats-io/go-nats-streaming.processMsg)-fm()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x4b
  github.com/nats-io/go-nats.(*Conn).waitForMsgs()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:1564 +0x1eb

Previous write at 0x00c4201f0000 by goroutine 81:
  github.com/MyTest/nats/v3_test.TestDurability.func1()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:383 +0xb2
  github.com/MyTest/nats/v3.(*T).Subscribe.func1()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:222 +0x2a0
  github.com/nats-io/go-nats-streaming.(*conn).processMsg()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/stan.go:464 +0x41c
  github.com/nats-io/go-nats-streaming.(*conn).(github.com/nats-io/go-nats-streaming.processMsg)-fm()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x4b
  github.com/nats-io/go-nats.(*Conn).waitForMsgs()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:1564 +0x1eb

Goroutine 82 (running) created at:
  github.com/nats-io/go-nats.(*Conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2238 +0x3f8
  github.com/nats-io/go-nats.(*Conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2155 +0x7e
  github.com/nats-io/go-nats-streaming.(*conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x59e
  github.com/nats-io/go-nats-streaming.(*conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:185 +0xb0
  github.com/MyTest/nats/v3.(*T).subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:279 +0x1be
  github.com/MyTest/nats/v3.(*T).Subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:226 +0x1f7
  github.com/MyTest/nats/v3_test.TestDurability()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:398 +0xa1a
  testing.tRunner()
      /usr/lib64/golang/src/testing/testing.go:746 +0x16c

Goroutine 81 (finished) created at:
  github.com/nats-io/go-nats.(*Conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2238 +0x3f8
  github.com/nats-io/go-nats.(*Conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats/nats.go:2155 +0x7e
  github.com/nats-io/go-nats-streaming.(*conn).subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:217 +0x59e
  github.com/nats-io/go-nats-streaming.(*conn).Subscribe()
      /home/chadit/Projects/src/github.com/nats-io/go-nats-streaming/sub.go:185 +0xb0
  github.com/MyTest/nats/v3.(*T).subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:279 +0x1be
  github.com/MyTest/nats/v3.(*T).Subscribe()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats.go:226 +0x1f7
  github.com/MyTest/nats/v3_test.TestDurability()
      /home/chadit/Projects/src/github.com/MyTest/nats/v3/nats_test.go:382 +0x606
  testing.tRunner()
      /usr/lib64/golang/src/testing/testing.go:746 +0x16c
==================
--- FAIL: TestDurability (1.01s)
    testing.go:699: race detected during execution of test
FAIL
exit status 1

ссылка на проект https://github.com/chadit/MessageQueueExamples/tree/master/nats-streaming/nats

0 ответов

Другие вопросы по тегам