利用 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"]],["最后更新时间 (UTC):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:"]]