【发布时间】:2016-10-08 02:18:38
【问题描述】:
我第一次使用谷歌云环境,特别是谷歌应用引擎和数据存储,当我在本地运行时一切正常。我通过按照文档设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 对数据存储进行身份验证。但是一旦我部署到应用程序引擎,请求总是超时,似乎 GetAll 方法永远不会返回。以下是我的应用程序的代码:
package app
import (
"fmt"
"net/http"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/cloud/datastore"
)
type User struct {
FirstName string
LastName string
Email string
Created time.Time
id int64
}
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
var err error
var dbClient *datastore.Client
var ctx context.Context
ctx = appengine.NewContext(r)
dbClient, err = datastore.NewClient(ctx, "app-id")//this has the real app id, not sure if this is meant to be secret
if err != nil {
fmt.Fprintf(w, "Could not create datastore client: %+v", err)
return
}
defer dbClient.Close()
var users []*User
query := datastore.NewQuery("User").Filter("Email=", "jcarm010@fiu.edu")
keys, err := dbClient.GetAll(ctx, query, &users)
if err != nil {
fmt.Fprintf(w, "Could not query users: %+v", err)
return
}
for i, key := range keys {
users[i].id = key.ID()
fmt.Fprintf(w, "%+v\n",users[i])
}
fmt.Fprintf(w, "done!!!")
}
App Engine 日志中的错误有以下两行:
This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.
Process terminated because the request deadline was exceeded. (Error code 123)
顺便说一句,这在我的本地快速完成,并且我的数据存储中只有一条记录。关于为什么会发生这种情况或如何调试它的任何猜测? 谢谢
【问题讨论】:
-
代码中没有任何内容会导致此超时...在我的本地,它会在不到 1 秒的时间内返回。删除 GetAll 调用并重新部署会使超时消失。如果我使用 put 而不是 GetAll,我会遇到同样的错误...
-
Google 的数据存储区与本地存储区相比有多少行?
-
我没有本地数据存储,只有与谷歌数据存储通信的本地开发者网络服务器,它只包含 1 行。
标签: google-app-engine go google-cloud-platform google-cloud-datastore