kerryjiang / SuperSocket

SuperSocket is a light weight, cross platform and extensible socket server application framework.
Apache License 2.0
3.95k stars 1.15k forks source link

服务器使用await/asyn 异步如何保证数据的时序问题? #556

Open hejiang123 opened 2 years ago

hejiang123 commented 2 years ago

假设玩家先后向服务器发送了两条协议都会改变用户数据。异步情况下如何保证用户请求顺序问题?

chucklu commented 2 years ago

There is a term called as ["double submission"](Prevent double submission of forms in jQuery)

You need to restrict at the client side, make sure the first request send successfully and data modified, then send the second request. It means the user can not send the second request before the first request submit successfully or failed, you need to wait the result of first request.

chucklu commented 2 years ago

You can also try to implement a optimistic lock, add a version number to your data. Optimistic vs. Pessimistic locking

Or you can add a package number in each data, and parse the number at server side.

hejiang123 commented 2 years ago

就是我的需求应该是这样的。普通消息可以不同玩家的请求异步处理,玩家自己的请求需要有顺序的处理。在特殊消息下,又需要所有玩家的请求也能有顺序的。比如抢单之类的

kerryjiang commented 2 years ago

From client, never send another request before you get response of the pervious request.

request => response request => response request => response

kerryjiang commented 2 years ago

And this option may help as well:

https://github.com/kerryjiang/SuperSocket/blob/d0e5bcfe363dd0ee3a4bacff3f8bc16a8cf9301e/src/SuperSocket.Channel/ChannelOptions.cs#L21