Webhook 方法的处理速度比控制器快



在我的应用程序中,有条纹付款。

当我点击"付款"按钮时,必须通过控制器进行处理。Webhook 也同时运行。

问题是响应 Web 挂钩的方法比控制器中的操作更快。

如何减慢 webhook 的方法,以便首先处理控制器中的操作?

网络钩子:

ev = Stripe::Webhook.construct_event(...)
case ev.type
when "invoice.created"
change_invoice(ev)
when "invoice.payment_succeeded"
invoice_paid(ev)

change_invoice(ev)比我的控制器快。

我能想到的最简单的解决方案是使用数据库锁。在这里阅读: https://www.peterdebelak.com/blog/pessimistic-locking-in-rails-by-example/

假设事件是您定义的模型:

# Controller action
def create
ev = Event.create(some_params)
ev.lock!
# Do the stripe processing here
# When you are done with your controller work:
ev.save # will release the lock
end
# Then in your webhook processor
def process
ev = Event.find(some_id) # This will wait until the lock is released in the controller thread. Just be aware that if the controller doesn't release the lock within the timeout limit, this will raise an exception
# now you can process the event because the controller is done
end

最新更新