doudao9896 2017-12-05 21:35
浏览 63
已采纳

在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?

  • 写回答

2条回答 默认 最新

  • dounan4479 2018-01-02 00:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)