英文:
Google App engine-Redislabs Go runtime production error - invalid memory address or nil pointer dereference
问题
我正在使用Redis Cloud服务(来自Redis Labs)在Google App Engine Go Runtime上,并且当我尝试获取一个不存在的键时,会出现上述错误。该代码在本地测试服务器上运行良好,但在生产环境中出现恐慌。
c, err := redis.Dial("tcp", "pub-redis-myredis:<myport>")
_, err = c.Do("AUTH", "password")
value, err := c.Do("GET", "foo4")
if value == nil {
log.Infof(contextOfAppEngineWhereServerIsRunning, "value not found in redislabs")
}
日志显示恐慌发生在_, err = c.Do("AUTH", "password")
这一行。
英文:
I'm using the Redis Cloud service (from Redis Labs) on Google App engine Go Runtime , and I get the above mentioned error when I try getting a key that doesn't exist. The code works fine on the local test server, but panics in production.
c, err := redis.Dial("tcp", "pub-redis-myredis:<myport>")
_, err = c.Do("AUTH", "password")
value, err := c.Do("GET", "foo4")
if value == nil {
log.Infof(contextOfAppEngineWhereServerIsRunning, "value not found in redislabs")
}
The log shows that the panic is in the line _, err = c.Do("AUTH", "password")
答案1
得分: 1
在AppEngine上,你的应用程序(webapp)在一个受限环境中运行,无法使用标准的net
包进行套接字操作(我猜redislabs使用了这个包)。
你需要使用appengine/socket
包来进行出站网络套接字操作。
这可能在本地环境下工作,因为限制只适用于生产环境。
始终检查返回的error
值。目前在你发布的代码中没有进行任何检查。如果你进行了检查,你会从错误中看到原因。
你会得到invalid memory address or nil pointer dereference
错误,是因为你的第一个调用失败了:
c, err := redis.Dial("tcp", "pub-redis-myredis:<myport>")
err
将是一个非nil
值,而c
是nil
,这意味着你无法调用它的Do()
方法。
英文:
On AppEngine your application (webapp) runs in a sandboxed environment where you can't use the standard net
package for sockets (which I assume redislabs use).
You have to use the appengine/socket
package for outbound network sockets.
It most likely works locally because the restriction only applies in production environment.
Always check returned error
values. Currently in the code you posted you check none. Should you have checked it, you would see the cause from the error.
You get invalid memory address or nil pointer dereference
error because your first call fails:
c, err := redis.Dial("tcp", "pub-redis-myredis:<myport>")
err
will be a non-nil
value and c
is nil
which means you can't call it's Do()
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论