- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
全部!
我有下面的代码:
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)
}
}
但是......当某个节点(例如 172.20.20.11)正在关闭时, panic 是提示无效的内存地址或零指针取消引用......
> $ ./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
有人可以向我解释发生了什么以及如何让这个简单的例子正常工作吗?非常感谢您的建议!亚历克斯
最佳答案
库可以关闭 watchChan
channel 。这将在您的 r
中返回一个 nil 值,然后当您尝试记录 r.Node
时出现 panic 。当你得到一个指针时,你应该检查它不是 nil
。我还建议检查接收方 channel 是否已关闭并采取相应措施。
r, open := <-watchChan
if !open {
// channel is closed
}
if r == nil {
// the watch channel return a nil value
}
关于Golang etcd 观察者 panic ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614899/
AngularFire2 文档演示了以下将观察者绑定(bind)到您的 Firebase 可观察对象的模式: this.item = af.database.object('/item'); {{ (
我正在使用观察者模式和 BlockingQueue 添加一些实例。现在在另一种方法中,我正在使用队列,但似乎 take() 一直在等待,即使我这样做: /** {@inheritDoc} */ @Ov
我遇到问题 - 创建具有不同属性的订单。在我的工作案例中,用户有大约 10 个不同的属性,每个属性有 3-4 个变体。 我正在使用 Angular js,并编写了两个函数: 二传手 $scope
我尝试将额外的产品添加到购物车。我为此创建了一个观察者。 getEvent()->getControllerAction()->getFullActionName() == 'checkout_car
我尝试将额外的产品添加到购物车。我为此创建了一个观察者。 getEvent()->getControllerAction()->getFullActionName() == 'checkout_car
假设我想创建一组基于类型的观察者。也就是说,当他们收到事件通知时,他们会被告知其中一个参数的类型,然后根据是否可以对该类型进行操作来决定是否采取行动。 有没有简单的方法可以做到这一点?我认为这对于泛型
快速提问。 当 setChanged() 和 notifyObservers() 被调用并且 Observer.update() 运行时,它是否在一个新的 Thread,还是在同一个Thread中?
我必须为可观察对象和观察者创建代码。正如您所知,每个观察者都有自己的“更新”方法。 在我的可观察代码中,我使用了“notifyObservers”方法。它的参数是一个字符串数组,其中包含有关我的项目的
是否有一种设计模式可以形成“复合”观察者/可观察对象? 我的意思是我有一个可观察的 A 通知它的听众一些变化。 每个监听器也是一个可观察对象,并通知自己的监听器(在它执行的某些操作中,哪个操作是由第一
我对以下代码为何不起作用感到有点困惑: MutableLiveData mutableTest = new MutableLiveData<>(); MediatorLiveData mediator
场景:我有两个名为 FirstFragment 的 fragment 和 UnitFragment .我来自 FirstFragment至UnitFragment选择一个单位返回FirstFragme
我想了解如何 平面 map 作品。我知道这是处理 Observable> 的一种方式。 无论如何,我正在测试它的行为并陷入困境: let plusDom = document.querySelecto
我有 public class mammal implements Imammle { public String getName() { return "Mammal";
如果我使用 KVO 来观察我的播放器项目,如下所示: AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
我想在正在使用的变量上使用观察者,这是我的代码: const get = Ember.get; uploader:Ember.inject.service('uploader'), progressC
我一直在使用Laravel Observers每当我的模型被删除/更新/保存时处理我的应用程序逻辑。我注意到在某些情况下这些观察者不会被触发。例如,如果我使用 Model::where(active,
我有一个工作人员服务来发送一些简单的通知。但现在,我想在发送之前检查数据库中的一些数据。 但是,我不能在工作线程中使用观察者,它必须在主线程中。我有一个错误类型 java.lang.IllegalSt
我有 2 个字段:美元和日元。当我更改美元字段时,我想在日元字段中显示转换后的数字,反之亦然。 我的问题是这样的: 如果我有 2 个带有观察者的字段,例如: addTextChangedListene
在 View Controller 中,我创建了一个 subview 数组,可以随时从父 View 中删除,因此它们的生命周期比 View Controller 的生命周期短。 创建它们时,我几乎这样
如何在另一个线程上运行 RxJava,因为主线程上的工作太多。 我在一个方法内运行观察者,下面是代码 fragment : public void updatePie() { RxJavaPlugin
我是一名优秀的程序员,十分优秀!