Golang etcd watcher panic(Golang etcd观察者崩溃)

huangapple go评论69阅读模式
英文:

Golang etcd watcher panic

问题

package main

import (
"log"

"github.com/coreos/go-etcd/etcd"

)

func main() {
client := etcd.NewClient(
[]string{
"http://172.20.20.10:2379",
"http://172.20.20.11:2379",
"http://172.20.20.12:2379",
},
)
for {
watchChan := make(chan *etcd.Response)
go client.Watch("/config", 0, false, watchChan, nil)

	log.Println("等待更新...")
	r := <-watchChan

	log.Printf(">>> 收到更新的配置:%s: %s\n", r.Node.Key, r.Node.Value)
}

}

但是... 当某个节点(例如172.20.20.11)宕机时,会出现关于无效内存地址或空指针解引用的恐慌错误...

$ ./etcd-watcher
2015/11/09 18:46:19 等待更新...
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x10 pc=0x22fe]

goroutine 1 [running]:
main.main()
/Users/Stalker/Workspace/src/snippets/etcd-watcher.go:26 +0x2be

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/Users/Stalker/App/Go/1.5.1/src/runtime/asm_amd64.s:1696 +0x1

goroutine 19 [runnable]:
net/http.(*persistConn).writeLoop(0xc8200c6dc0)
/Users/Stalker/App/Go/1.5.1/src/net/http/transport.go:1009 +0x40c
created by net/http.(*Transport).dialConn
/Users/Stalker/App/Go/1.5.1/src/net/http/transport.go:686 +0xc9d

有人能解释一下发生了什么,并告诉我如何使这个简单的示例正常工作吗?非常感谢建议!
Alex

英文:

All!

I've the code below:

package main

import (
        &quot;log&quot;

        &quot;github.com/coreos/go-etcd/etcd&quot;
)

func main() {
        client := etcd.NewClient(
                []string{
                        &quot;http://172.20.20.10:2379&quot;,
                        &quot;http://172.20.20.11:2379&quot;,
                        &quot;http://172.20.20.12:2379&quot;,
                },
        )
        for {
                watchChan := make(chan *etcd.Response)
                go client.Watch(&quot;/config&quot;, 0, false, watchChan, nil)

                log.Println(&quot;Waiting for an update...&quot;)
                r := &lt;-watchChan

                log.Printf(&quot;&gt;&gt;&gt; got an updated config: %s: %s\n&quot;, r.Node.Key, r.Node.Value)
        }
}

BUT... When some node (for example 172.20.20.11) is going down the panic is complain regarding the invalid memory address or nil pointer dereference...

&gt; $ ./etcd-watcher
2015/11/09 18:46:19 Waiting for an update...
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x10 pc=0x22fe]

goroutine 1 [running]:
main.main()
    /Users/Stalker/Workspace/src/snippets/etcd-watcher.go:26 +0x2be

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
     /Users/Stalker/App/Go/1.5.1/src/runtime/asm_amd64.s:1696 +0x1

goroutine 19 [runnable]:
net/http.(*persistConn).writeLoop(0xc8200c6dc0)
    /Users/Stalker/App/Go/1.5.1/src/net/http/transport.go:1009 +0x40c
created by net/http.(*Transport).dialConn
    /Users/Stalker/App/Go/1.5.1/src/net/http/transport.go:686 +0xc9d

Could someone explain me what is going on and how to make this simple example to work properly??
Thanks a lot for advice!
Alex

答案1

得分: 4

这个库可以关闭watchChan通道。这将在你的r中返回一个空值,然后当你尝试记录r.Node时会引发panic错误。
当你得到一个指针返回时,你应该检查它是否不是nil。我还建议检查接收器通道是否已关闭,并相应地采取行动。

r, open := <-watchChan
if !open {
    // 通道已关闭
}
if r == nil {
   // watch通道返回了一个空值
}
英文:

The library can close the watchChanchannel. That will return a nil value in your r and then panic when you try to log r.Node.
When you get a pointer back, you should check it's not nil. I would also recommend checking if the receiver channel has been closed and act accordingly.

r, open := &lt;-watchChan
if !open {
    // channel is closed
}
if r == nil {
   // the watch channel return a nil value
}

huangapple
  • 本文由 发表于 2015年11月10日 01:39:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/33614899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定