2017-12-05 14 views
1

私は、Goで書かれたAppEngine(標準環境)アプリケーションでFirestoreを使用しようとしています。 I "Getting Started with Cloud Firestore"ガイド次てきたし、私の地元のdevのサーバー上でそれを実行するときに正常に動作し、簡単な例を実装するためにfirestore packageドキュメントを使用している。AppEngine Go標準環境でCloud Firestoreを使用すると、AppEnglineで実行中にrpcエラーが返される

しかし、私はアプリを展開し、展開されたバージョンの呼び出しを試してみてくださいこれが正常に動作のdevのサーバー上で、前述のようにそうそこ

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") 
} 

DocumentRef.Set()にこの問題を再現する私のコードでエラー

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

で失敗します返されたページにはテキストData stored in Firestore successfullyが含まれています。

デプロイされたコードを実行すると、代わりにFailed to create a firestore document: rpc error: code = Unavailable desc = all SubConns are in TransientFailureが得られます。なぜこのエラーが発生するのですか?どのように回避できますか?

+0

'firestoreClient'はhttpクライアントを使用しますか? urlfetch.Client(ctx)によって作成されたhttpクライアントを渡し/割り当てる必要がありますか? –

答えて

0

Firestoreクライアントライブラリの問題追跡ツールでこれについてissueを挙げましたが、状況は少し複雑です。

App Engineを使用する場合、Firestoreクライアントライブラリのネットワーク接続は、App Engine socket libraryを通過します。しかし、ソケットは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.

だから、これはFirestoreクライアントライブラリが失敗した理由です。小規模プロジェクトの場合、App Engineアプリの請求を有効にしても無料の範囲内に留まることができます。請求が有効になっている場合は、アプリの配備時にも有効です。

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.

ですから、ヨーロッパや他のいくつかの理由である場合:あなたは、欧州連合(EU)内に住んでいる場合は、あなたがGoogle policiesによる非商業的目的のために支払われたApp Engineアプリケーションを持つことが許されていません

有料App Engineアプリを使用できない場合、Firestoreクライアントライブラリを使用できなくなります。

代わりにFirestore REST APIを使用して、手動でFirestoreにHTTPリクエストを行います。これはもう少し作業ですが、小規模なプロジェクトでは機能します。

1

AppEngineでは、urlfetchサービスが提供するHttpクライアントを使用するクライアントを作成する必要があります。

firestore.NewClient()ファンクションは、WithHTTPCLient()ファンクションを使用して作成できるパラメータClientOptionsを受け入れます。

ここにはissuing HTTP requests from AppEngine Goに関する記事があります。

これは役に立ちます。

+0

ああ、私はそれを忘れていました。しかし、残念ながらそれは動作しません。 'firestore.NewClient()'は次のエラーを返します: 'サポートされていないHTTPクライアントが指定されました'。 firestore.NewClient()を呼び出すときにhttpクライアントが提供されていると、そのエラーを返すようです。 – nibarius

+0

urlfetchでサポートされていないソケットを使用している可能性があります。 –

+0

そうだそうです。ソケットはGAEの無料版では使用できません。 – nibarius

関連する問題