问题描述
我有两台服务器。第一个是使用 Apollo Server 的后端,另一个是我使用 Next.js 的前端服务器
后端托管在 api.example.com,前端托管在 www.example.com。两台服务器都在 Google App Engine 上作为同一应用中的两种不同服务运行。
我遇到的问题是我使用了 graphql-query-complexity 来对我的应用程序应用速率限制,而且它还限制了我的前端服务器的速率,因为前端服务器使用服务器端渲染来预加载请求数据。
我的问题是:如何将我的前端服务器列入白名单,以便它可以根据需要发出尽可能多的请求,而不必在每次将新代码推送到前端生产时手动更新服务器 IP 地址的环境变量服务器?我也不确定我的前端对后端的请求是使用本地网络还是使用 www?
这是与限制我的服务器速率相关的代码:
if (config.NODE_ENV !== 'test') {
// If the cost of the query is more than 500 throw an error
if (cost > 500) {
throw new Error(`Query cost exceeds single request token limit. The limit is 500 tokens and the query has a cost of ${cost} tokens. Please reduce the query cost and try again.`);
}
// Ensure that the IP Address is present on the request
if (context && context.req && context.req.connection && context.req.connection.remoteAddress && context.req.connection.remoteAddress !== ---- not sure what to do about this ----) {
// Check if there is a token allotment already in the database for that IP Address
const tokens = await redis.get(context.req.connection.remoteAddress);
console.log(tokens,cost);
if (tokens !== null) {
// If the request would exceed the token allotment
if (tokens - cost < 0) {
throw new Error('This request would exceed your token allotment. Please reduce the query cost or wait one minute and try again.');
}
// Decrement the token allotmetment by the cost of the query
await redis.decrby(context.req.connection.remoteAddress,cost);
} else {
// Set the value and time-to-live of the token allotment
await redis.setex(context.req.connection.remoteAddress,60,5000 - cost);
}
} else {
throw new Error('An error occured within the request cost calculator. Please try again.');
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)