在Google云端硬盘文档上创建多个权限时出现Java异常
作者:互联网
在我们的应用程序中,我们需要使用Google Drive api向多个用户共享多个文件.
我们使用Google Drive api的java客户端库提供的批处理.
这已经在生产中运行,但我们从Google Drive api中得到了很多不明确的例外情况:
Internal Error. User message: "An internal error has occurred which prevented the sharing of these item(s): "
我们处理异常并以指数退避重试,但这些错误会导致此应用程序的流量和可用性出现大的延迟.
这些异常发生的原因是什么?怎么避免那些?
如果我们知道出现这些异常时会出现什么问题会非常有帮助,所以我们可以避免它.
一些额外的信息:
每个批处理对不同文件包含100个权限操作.
每分钟调用一次批处理操作.
代码:
String fileId = "1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ";
JsonBatchCallback<Permission> callback = new JsonBatchCallback<Permission>()
{
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders)
throws IOException {
System.err.println(e.getMessage());
}
@Override
public void onSuccess(Permission permission, HttpHeaders responseHeaders) throws IOException {
System.out.println("Permission ID: " + permission.getId());
}
};
BatchRequest batch = driveService.batch();
for(String email : emails) {
Permission userPermission = new Permission().setType("user").setRole("reader").setEmailAddress(email);
driveService.permissions().create(fileId, userPermission).setSendNotificationEmail(false).setFields("id").queue(batch, callback);
}
batch.execute();
变量电子邮件包含100个电子邮件字符串.
解决方法:
{
"code" : 500,
"errors" : [ {
"domain" : "global",
"message" : "Internal Error. User message: "An internal error has occurred which prevented the sharing of these item(s): fileame"",
"reason" : "internalError"
} ],
"message" : "Internal Error. User message: "An internal error has occurred which prevented the sharing of these item(s): filename""
}
基本上是防洪.通常的建议是Implementing exponential backoff
Exponential backoff is a standard error handling strategy for network
applications in which the client periodically retries a failed request
over an increasing amount of time. If a high volume of requests or
heavy network traffic causes the server to return errors, exponential
backoff may be a good strategy for handling those errors. Conversely,
it is not a relevant strategy for dealing with errors unrelated to
rate-limiting, network volume or response times, such as invalid
authorization credentials or file not found errors.Used properly, exponential backoff increases the efficiency of
bandwidth usage, reduces the number of requests required to get a
successful response, and maximizes the throughput of requests in
concurrent environments.
现在这是你要说的但是我正在批处理我不能这样做. Yup配料属于同样的防洪保护.您的批次充斥着服务器.是的,我知道它说你可以发送100个请求,如果请求在每个请求之间花费足够的时间不符合洪水的要求,你可能可以,但是你的请求显然没有.
我的建议是你试着把它减去10个请求,然后慢慢踩它.使用批量配额使用时,您不能自行保存任何内容,就像您没有批量配置一样.你不能比洪水保护允许的更快.
标签:java,google-api,google-app-engine,google-drive-sdk 来源: https://codeday.me/bug/20190705/1391968.html