gpt4 book ai didi

java - 使用 APISpark reSTLet 扩展限制对 ReSTLet 资源的请求

转载 作者:行者123 更新时间:2023-12-02 04:54:44 28 4
gpt4 key购买 nike

这是我限制每分钟请求数量的代码:

        MethodAuthorizer ma = createMethodAuthorizer();
ma.setNext(router);

FirewallRule rule = new PeriodicFirewallCounterRule(60, TimeUnit.SECONDS, new IpAddressCountingPolicy());
((PeriodicFirewallCounterRule)rule).addHandler(new RateLimitationHandler(new UniqueLimitPolicy(10)));
FirewallFilter firewallFiler = new FirewallFilter(getContext(), list(rule));
firewallFiler.setNext(ma);

return ma;

问题是没有错误,但即使从资源请求超过10个请求,它仍然不会抛出“Too Many Request”

最佳答案

我使用 GAE 项目中的配置代码和开发服务器使其工作。

我使用ReSTLet 2.3.1版本/GAE 1.9.18版本以及以下代码作为客户端:

public static void main(String[] args) {
int i = 0;
try {
while (i < 30) {
ClientResource cr = new ClientResource("http://localhost:8080/test");
Representation repr = cr.get();
System.out.println(">> call #"+i);
Thread.sleep(100);

i++;
}
} catch (Exception ex) {
System.out.println(">> call #" + i + " failed");
ex.printStackTrace();
}
}

在第 10 次调用后出现以下异常:

>> call #0
>> call #1
>> call #2
>> call #3
>> call #4
>> call #5
>> call #6
>> call #7
>> call #8
>> call #9
>> call #10 failed
429 (429) - The server is refusing to service the request because the user has sent too many requests in a given amount of time ("rate limiting")
at org.restlet.resource.ClientResource.doError(ClientResource.java:590)
at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1153)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1048)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1023)
at org.restlet.resource.ClientResource.handle(ClientResource.java:928)
at org.restlet.resource.ClientResource.get(ClientResource.java:636)
at org.restlet.gae.test.GaeRestletClient.main(GaeRestletClient.java:15)

希望对你有帮助蒂埃里

关于java - 使用 APISpark reSTLet 扩展限制对 ReSTLet 资源的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28899855/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com