dr6673999 2017-12-29 12:13
浏览 59

为nats-streaming设置处理程序时进行比赛数据检测

For a nats-streaming subscription the handler needs to look like this

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

I created the following function in an attempt to take the their handler and then call my own to process the message returned.

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)
}

With go test -race I get a data warning race condition around the area where I loop over/call my handlers. I am Looking for some suggestions on how I can improve this to remove that race warning.

== race detection output

==================
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

link to project https://github.com/chadit/MessageQueueExamples/tree/master/nats-streaming/nats

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计