在AppEngline上运行时,将Cloud Firestore与AppEngine Go标准环境配合使用会返回rpc错误

I'm trying to use Firestore in my AppEngine (standard environment) app written in Go. I've been following the "Getting Started with Cloud Firestore" guide and have been using the firestore package documentation to implement a simple example that works fine when running it on my local dev server.

However when I deploy the app and try the deployed version the call to DocumentRef.Set() fails with the error

rpc error: code = Unavailable desc = all SubConns are in TransientFailure

This is my code that reproduces the issue:

func init() {
    http.HandleFunc("/test", testHandler)
}

type testData struct {
    TestData string `firestore:"myKey,omitempty"`
}

func testHandler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)

    var firestoreClient *firestore.Client
    var firebaseApp *firebase.App
    var err error

    conf := &firebase.Config{ProjectID: "my-project"}
    firebaseApp, err = firebase.NewApp(ctx, conf)

    if err != nil {
        fmt.Fprintf(w, "Failed to create a new firestore app: %v", err)
        return
    }

    firestoreClient, err = firebaseApp.Firestore(ctx)
    if err != nil {
        fmt.Fprintf(w, "Failed to create a new firestore client: %v", err)
        return
    }

    data := testData{"my value"}
    _, err = firestoreClient.Collection("testCollection").Doc("testDoc").Set(ctx, data)
    if err != nil {
        fmt.Fprintf(w, "Failed to create a firestore document: %v", err)
        return
    }
    firestoreClient.Close()
    fmt.Fprint(w, "Data stored in Firestore successfully")
}

As mentioned before, on the dev server this works fine. So there the returned page contains the text Data stored in Firestore successfully.

When running the deployed code I get Failed to create a firestore document: rpc error: code = Unavailable desc = all SubConns are in TransientFailure instead. Why do I get this error and how can I avoid it?

I've raised an issue about this in the Firestore client library issue tracker and it seems like the situation is a bit complex.

When using App Engine the Firestore client library's network connections goes trough the App Engine socket library. However sockets is only available for paid App Engine apps:

Sockets are only available for paid apps, and traffic from sockets is billed as outgoing bandwidth. Sockets are also limited by daily and per minute (burst) quotas.

So this is the reason why the Firestore client library fails. For small scale projects it's possible to enable billing of your App Engine app and still stay within the free range. If billing is enabled it should work when the app is deployed as well.

However if you are living within the European Union you are not allowed to have a paid App Engine app for non commercial purposes due to Google policies:

If you are located in the European Union and the sole purpose for which you want to use Google Cloud Platform services has no potential economic benefit you should not use the service. If you have already started using Google Cloud Platform, you should discontinue using the service. See Create, modify, or close your billing account to learn how to disable billing on your projects.

So if you are in Europe or for some other reason are unable to use have a paid App Engine app you will not be able to use the Firestore client library.

One alternative in this case is to use the Firestore REST API instead and manually make HTTP requests to Firestore. It's a bit more work, but for smaller scale projects it works.

On AppEngine you need to create a client that uses Http client provided by urlfetch service.

The firestore.NewClient() function accepts ClientOptions parameters that you can create using WithHTTPCLient() function.

Here is an article on issuing HTTP requests from AppEngine Go.

That should help.