英文:
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 (
        "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("Waiting for an update...")
                r := <-watchChan
                log.Printf(">>> got an updated config: %s: %s\n", 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...
> $ ./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 := <-watchChan
if !open {
    // channel is closed
}
if r == nil {
   // the watch channel return a nil value
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论