3
votes

I built a small go app, for use in AppEngine. I run the go locally through go run .., and I am trying to use the datastore.

I run the datastore emulator locally through gcloud beta emulators datastore start and by exporting DATASTORE_EMULATOR_HOST the go app can locally connect to it.

So I built my app using cloud.google.com/go/datastore, but when I deployed this to AppEngine any go code that connects to the datastore seems to make the whole app fail with a timeout.

Whilst trying to debug this I ran into code using google.golang.org/appengine/datastore and writing some test code I got things to work on AppEngine.

However this datastore does not seem to be able to connect to the datastore emulator.

  1. Has anyone run into the timeout issue when using cloud.google.com/go/datastore? I seem to read on different answers that people say to use this package and not the golang.org package for some reason.
  2. Can I use cloud.google.com/go/datastore on AppEngine? I would prefer to use this package, since it works with the datastore emulator.

I am using the following code, in short:

import "cloud.google.com/go/datastore"
...
ctx := appengine.NewContext(r)
...
client, err := datastore.NewClient(dsCtx, projectID)
...
key := datastore.IDKey(TestKind, testID, nil)
err = client.Get(ctx, key, &data)

This is the code that times out on AppEngine, but works fine locally.

The code that likewise works on AppEngine is this:

import "google.golang.org/appengine/datastore"
...
ctx := appengine.NewContext(r)
key := datastore.NewKey(ctx, TestKind, "", testID, nil)
err := datastore.Get(ctx, key, &data)

And this will fail locally with the following:

Metadata fetch failed: Get http://metadata/computeMetadata/v1/instance/attributes/gae_project: dial tcp: lookup metadata: no such host

Any help would be much appreciated.

1

1 Answers

4
votes

From the Connecting to Cloud Datastore with App Engine section in the App Engine Cloud Datastore Overview:

You cannot use the Cloud Datastore client library with Go applications in the App Engine standard environment.

Basically, cloud.google.com/go/datastore is for using Cloud Datastore outside of the App Engine standard environment. This includes non-App Engine environments, as well as the App Engine flexible environment.

google.golang.org/appengine/datastore is for using it within the App Engine standard environment.

For local testing of the App Engine standard environment, look into using dev_appserver.py which provides integration with the Cloud Datastore emulator via the --support_datastore_emulator flag.