加权支付网关旋转器

发布于 2024-10-01 12:21:03 字数 178 浏览 3 评论 0原文

所以我的新任务(笑)。我正在寻找创建一个可以轮换支付网关的脚本,例如我将在数据库中添加 5 个支付网关,并且必须为它们赋予总计高达 100% 的权重,就像负载均衡器一样。最好的方法是什么以及我应该如何构建数据库来实现这项工作,其中一个网关获得为该支付网关 ID 设置的任何权重百分比。如果您能引导我走向正确的方向或有任何好的教程,我将不胜感激

So my new mission (lol). I'm looking to create a script that will rotate payment gateways for instance i'll add 5 payment gateways in a database and have to give them a weight all totaling up to 100% just like a load balancer. What is the best way and how should i structure the database to make this work where one gateway gets whatever weight percentage that is set for that payment gateway id. If you could led me in the right direction or have any good tutorials that would be greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2024-10-08 12:21:03

我的想法:
为每个网关提供一个“点击”列。包含人们使用它的次数。还有一个百分比栏——每个网关所需的权重。
所以表是:
名称:网关
列:网关、点击数、百分比

然后在向用户发送网关之前,将所有网关的所有点击数相加,然后将总点击数除以每个网关的点击数,并将这些值存储在数组中。数组的结果实际上是网关的当前百分比。然后发送期望百分比与当前百分比差异最大的网关。在 PHP 中,连接到数据库后,这看起来像这样:

$cur_perc_query=mysql_query("SELECT * FROM gateways");
$totalhits=0;
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
$totalhits+=$cur_get['Hits'];
}
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
$gate_perc[]=$cur_get['Percentage']-($totalhits/$cur_get['Hits']);
}
$highest=$gate_perc[0];
$gate_number=0;
foreach($gate_perc as $numb => $value) {
if ($highest<$value) { $highest=$value; $gate_number=$numb; }
}
$count=0;
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
if ($count=$gate_number) { $chosen_gate=$cur_get['Gateway']; }
$count++;
}
echo $chosen_gate;

我还没有测试过它,但它应该以这种方式工作。
您需要做的另一件事是,每次用户使用当前网关时,将其点击量加 1。
希望这有帮助。

My idea:
Give each gateway a "hits" column. Containing the number of times people used it. And a percentage column-the desired weight for each gateway.
So the table is:
Name: gateways
Columns: Gateway,Hits,Percentage

Then before sending the user the gateway, sum up all of the hits for all of the gateways, then divide the total hits to the hits for every gateway and store the values in an array. The results of the array will actually be the current percentage of the gateways. Then send the gateway with the biggest difference between the desired percentage and the current percentage. In PHP this would look something like this, after connecting to the database:

$cur_perc_query=mysql_query("SELECT * FROM gateways");
$totalhits=0;
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
$totalhits+=$cur_get['Hits'];
}
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
$gate_perc[]=$cur_get['Percentage']-($totalhits/$cur_get['Hits']);
}
$highest=$gate_perc[0];
$gate_number=0;
foreach($gate_perc as $numb => $value) {
if ($highest<$value) { $highest=$value; $gate_number=$numb; }
}
$count=0;
while ($cur_get=mysql_fetch_array($cur_perc_query)) {
if ($count=$gate_number) { $chosen_gate=$cur_get['Gateway']; }
$count++;
}
echo $chosen_gate;

I havent tested it but it should work this way.
The other thing you need to do is add 1 to the hits of the current gateway each time a user uses it.
Hope this helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文