gpt4 book ai didi

ios - Rails API - 并行处理多个交易 - 平衡支付

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:54 26 4
gpt4 key购买 nike

我已经为我的 iOS 应用程序设置了一个 Rails API。我们有一个小组拆分功能,允许人们邀请其他人加入他们的小组并平分整个账单。它目前已实现并且运行良好,但随着组规模的增加,处理事务的时间确实变慢了(因为它们处于循环中而不是一次全部)。

我很好奇是否有其他人解决过类似的问题或有任何资源可以为我指明正确的方向来解决此类问题。目前我们正在对每个组成员卡进行搁置,如果所有搁置都成功,我们将捕获它们。我们这样做是为了确保小组中没有人卡退回,如果我们直接从卡中扣款,如果有人卡退回,我们可能会承担拒付责任。我们正在使用 Balanced Payments ruby​​ 客户端。

我看过赛璐珞 gem 或 sidekiq。我通常会做所有的 iOS 工作,但我们一直很难找到好的人来做 API,我决定尝试一下这个应用程序,到目前为止一直很好,直到我遇到这个问题。

@booking.group.group_members.each do |group_member|
credit_card = CreditCard.find(group_member.credit_card_id)
customer = Balanced::Account.find(group_member.user.customer_uri)

booking = Booking.create(:venue_id => @venue.id,
:user_id => group_member.user_id,
:group_id => @booking.group_id,
:subtotal => @booking.subtotal / @group_count,
:total => @booking.total / @group_count,
:gratuity_fee => @booking.gratuity / @group_count,
:credit_card_fee => @booking.fees / @group_count,
:credit_card_id => group_member.credit_card_id,
:receipt_id => @booking.id,
)

begin
hold = customer.hold({:amount => ((booking.subtotal + booking.booking_fee + booking.gratuity_fee + booking.credit_card_fee) * 100).to_int,
:on_behalf_of_uri => @merchant_uri,
:appears_on_statement_as => "...",
:source_uri => credit_card.credit_card_uri}
)

rescue Balanced::Error => error
#handle errors
render json: error.description, status: error.status_code
end
end
end

这就是我对每张卡片进行搁置的方式。有没有人对我如何并行而不是顺序处理所有这些有任何想法。我知道 sidekiq 或 celluloid 可以很容易地处理类似的事情,但我担心如果在处理付款时发生错误,以及如果一次处理所有这些错误该如何处理。

最佳答案

如果你有made your Rails app thread-safe ,您可以只调用 Thread.new 并在单独的线程中处理每个客户。然后,当您加入线程时,您可以检查错误并继续。

更多关于如何使用线程的信息 here .

如果组可以变得非常大,您可能希望限制并发线程的数量。我通常将线程保存在一个数组中,如果它达到大小限制,我会在继续之前加入第一个线程。

关于ios - Rails API - 并行处理多个交易 - 平衡支付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18294964/

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