Google App Engine-Redislabs Go运行时生产错误-无效的内存地址或空指针解引用

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

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(&quot;tcp&quot;, &quot;pub-redis-myredis:&lt;myport&gt;&quot;)
_, err = c.Do(&quot;AUTH&quot;, &quot;password&quot;) 
value, err := c.Do(&quot;GET&quot;, &quot;foo4&quot;)

if value == nil {
    log.Infof(contextOfAppEngineWhereServerIsRunning, &quot;value not found in redislabs&quot;)
    
} 

The log shows that the panic is in the line _, err = c.Do(&quot;AUTH&quot;, &quot;password&quot;)

答案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值,而cnil,这意味着你无法调用它的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(&quot;tcp&quot;, &quot;pub-redis-myredis:&lt;myport&gt;&quot;)

err will be a non-nil value and c is nil which means you can't call it's Do() method.

huangapple
  • 本文由 发表于 2015年4月24日 19:51:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/29846815.html
匿名

发表评论

匿名网友

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

确定