等待锁定释放时的 Http Statuscode 需要很长时间?
Http Statuscode when waiting for lock-release takes to long?
我正在使用锁定机制来确保两个并行调用不会更新同一行而导致意外行为。所以我的代码是这样的:(没有真实世界的例子)
public class UserController {
public ActionResult AddReputation(int id, int repAmount) {
int lockWait=0;
bool alreadyLocked=true;
while (alreadyLocked) {
alreadyLocked=GetLockForUser(id);
Thread.Wait(1000);
lockWait++;
if (lockWait>10) {
return new HttpStatus(xxx);
}
}
SetlockForUser(id);
AddUserRep(id,repAmount);
return new Content("Well Done");
}
}
所以。如果 10 秒后锁仍然存在,我想告诉调用者 "Please try again later, someone else is just saving data for that user".
REST-API 中最好的 HTTP 代码是什么?
409 Conflict
?或 423 Locked
?
注意:这不是 SQL-DB。我没有可以使用的真正的交易机制。所以我必须实现自己的锁定机制。
你应该问问自己:客户在这种情况下想做什么?
从你的描述来看,客户唯一的选择就是等待并稍后再试。所以 503 (Service Unavailable) 似乎很合适:
indicates that the server is currently unable to handle the request due to a temporary overload or scheduled maintenance, which will likely be alleviated after some delay
此外,通用 500 (Internal Server Error) 随时为您服务。
423(已锁定)可能适合也可能不适合。它被设计为 WebDAV’s locking mechanisms, where clients explicitly lock and unlock resources. Generally, clients are more likely to understand a 503 error (very common in the wild) than a 423 error (uncommon outside of WebDAV): 423 is likely to be treated as a generic client error, which may not be helpful. That said, the definition of 423 itself 的一部分,不需要涉及 WebDAV 锁定。如果您想将这种情况与服务器停机(这将导致 503)之类的情况区分开来,那么 423 可能会起作用。
409 (Conflict) 不 看起来很合适:
This code is used in situations where the user might be able to resolve the conflict and resubmit the request.
我正在使用锁定机制来确保两个并行调用不会更新同一行而导致意外行为。所以我的代码是这样的:(没有真实世界的例子)
public class UserController {
public ActionResult AddReputation(int id, int repAmount) {
int lockWait=0;
bool alreadyLocked=true;
while (alreadyLocked) {
alreadyLocked=GetLockForUser(id);
Thread.Wait(1000);
lockWait++;
if (lockWait>10) {
return new HttpStatus(xxx);
}
}
SetlockForUser(id);
AddUserRep(id,repAmount);
return new Content("Well Done");
}
}
所以。如果 10 秒后锁仍然存在,我想告诉调用者 "Please try again later, someone else is just saving data for that user".
REST-API 中最好的 HTTP 代码是什么?
409 Conflict
?或 423 Locked
?
注意:这不是 SQL-DB。我没有可以使用的真正的交易机制。所以我必须实现自己的锁定机制。
你应该问问自己:客户在这种情况下想做什么?
从你的描述来看,客户唯一的选择就是等待并稍后再试。所以 503 (Service Unavailable) 似乎很合适:
indicates that the server is currently unable to handle the request due to a temporary overload or scheduled maintenance, which will likely be alleviated after some delay
此外,通用 500 (Internal Server Error) 随时为您服务。
423(已锁定)可能适合也可能不适合。它被设计为 WebDAV’s locking mechanisms, where clients explicitly lock and unlock resources. Generally, clients are more likely to understand a 503 error (very common in the wild) than a 423 error (uncommon outside of WebDAV): 423 is likely to be treated as a generic client error, which may not be helpful. That said, the definition of 423 itself 的一部分,不需要涉及 WebDAV 锁定。如果您想将这种情况与服务器停机(这将导致 503)之类的情况区分开来,那么 423 可能会起作用。
409 (Conflict) 不 看起来很合适:
This code is used in situations where the user might be able to resolve the conflict and resubmit the request.