您的應用程式可使用 Capabilities API 來偵測特定 API 功能的運作中斷和排定停機時間。您可以使用這個 API 來偵測特定功能無法使用的時間,然後略過該功能,藉此縮短應用程式中的停機時間。如要查看 capability 套件內容,請參閱capability 套件參考資料。。
舉例來說,如果您使用 Datastore API,您可以利用 Capabilities API 來偵測 Datastore API 無法使用的時間,並且向使用者報告錯誤:
funchandler(whttp.ResponseWriter,r*http.Request){ctx:=appengine.NewContext(r)// Check if the Datastore API is availableif!capability.Enabled(ctx,"datastore_v3","*"){http.Error(w,"This service is currently unavailable.",503)return}// do Datastore lookup ...}
funccheckDatastoreMode(whttp.ResponseWriter,r*http.Request){ctx:=appengine.NewContext(r)// Check if the Datastore service is in read-only mode.if!capability.Enabled(ctx,"datastore_v3","write"){// Datastore is in read-only mode.}}
在 Go 1.11 中使用 Capabilities API
如果提供的 API 和功能可用,capability.Enabled 函式會傳回 true。您必須傳遞功能名稱 (例如 "write") 或萬用字元 "*",才能查詢 API 的所有功能。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThe Capabilities API allows applications to detect outages and scheduled downtime for specific API capabilities, helping to reduce downtime by identifying when a capability is unavailable and bypassing it.\u003c/p\u003e\n"],["\u003cp\u003eThis API is designed for first-generation runtimes and remains usable when upgrading to corresponding second-generation runtimes, although those upgrading to Go 1.12+ should consult the migration guide.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ecapability.Enabled\u003c/code\u003e function verifies the availability of a given API and capability, and requires a capability name like \u003ccode\u003e"write"\u003c/code\u003e or the wildcard \u003ccode\u003e"*"\u003c/code\u003e for querying all API capabilities.\u003c/p\u003e\n"],["\u003cp\u003eDatastore writes are the only exception to the usual behavior, with the API returning \u003ccode\u003eDISABLED\u003c/code\u003e when Datastore is in read-only mode, whereas other statuses return \u003ccode\u003eENABLED\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eSupported capabilities include the availability of the blobstore, Datastore reads and writes, Mail service, Memcache service, Task Queue service, and URL Fetch service.\u003c/p\u003e\n"]]],[],null,["# Capabilities API for legacy bundled services\n\nWith the Capabilities API, your application can detect outages and scheduled\ndowntime for specific [API capabilities](#Supported_capabilities). You can use\nthis API to reduce downtime in your application by detecting when a capability\nis unavailable and then bypassing it. To view the contents of\nthe `capability` package, see the\n[`capability` package reference](/appengine/docs/legacy/standard/go111/reference/latest/capability).\n.\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\n| Every status request to this API always returns `ENABLED` except\n| for the \"Datastore writes\" capability, which returns `DISABLED` if\n| Datastore is in read-only mode for your app.\n\nFor example, if you use the Datastore API, you can use the Capabilities API to\ndetect when the Datastore API is unavailable and report an error to the user: \n\n func handler(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \t// Check if the Datastore API is available\n \tif !capability.Enabled(ctx, \"datastore_v3\", \"*\") {\n \t\thttp.Error(w, \"This service is currently unavailable.\", 503)\n \t\treturn\n \t}\n \t// do Datastore lookup ...\n }\n\nYou can separately query for the availability of Datastore reads and writes. The\nfollowing sample shows how to detect the availability of Datastore writes and,\nduring downtime, provide a message to users: \n\n func checkDatastoreMode(w http.ResponseWriter, r *http.Request) {\n \tctx := appengine.NewContext(r)\n \t// Check if the Datastore service is in read-only mode.\n \tif !capability.Enabled(ctx, \"datastore_v3\", \"write\") {\n \t\t// Datastore is in read-only mode.\n \t}\n\n }\n\nUsing the Capabilities API in Go 1.11\n-------------------------------------\n\nThe [`capability.Enabled`](/appengine/docs/legacy/standard/go111/reference/latest/capability#google_golang_org_appengine_capability_Enabled)\nfunction returns true if the provided API and capability are available. You must\npass either a capability name (such as `\"write\"`) or the wildcard `\"*\"` to query\nall capabilities of the API.\n\nSupported capabilities\n----------------------\n\nThe API currently supports the following capabilities:"]]