且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用应用程序状态确定活动用户

更新时间:2023-02-06 22:12:45

遇到问题时,您是唯一使用该应用程序的用户吗?如果没有,有几件事可能会出错。



首先,如果多个用户同时登录或多个会话同时结束,则计数可能会变得不正确,因为多个线程可能会读取值(在任何更新之前)而不是严格的读/写序列。



此外,如果用户不存在或不登录,则计数可能出错了,因为它可能不会在那时增加,但在会话结束时系统地减少。



然后根据你存储应用程序状态的位置,应用程序可能已重新启动之间用户可能仍然登录,因此当应用程序重启可能不合适时重置计数。



如果用户输入错误的密码似乎也是如此,您将找不到任何用户,因为您的查询也会过滤密码(我希望密码在数据库中加密)。因此,我认为用户数不会增加,但会话结束时无论如何都会减少。



您总是可以在数据库中添加一些跟踪或信息帮助找出问题所在。



***在数据库中为每个用户指定一个值,该值指示用户是否已登录,然后只计算将该标志设置为具有活跃用户的数量,因为您不需要同步原语(据我所知),您应该永远不会得到负值,因为您计算符合条件的对象。
When you have the problem, are you the only user using the application? If not, there are a couple of things that can goes wrong.

First, if multiple user log at the same time or multiple sessions ends at the same time, it might be possible that count become incorrect because multiple thread might read the values (bafore any update) instead of having a strict read/write sequence.

Also, if the user does not exists or don't log in, then the count might get wrong as it might not be incremented at that point but decremented systematically when the session ends.

Then depending on where you store the application state, the aplication might have restarted between so the user might still be logged in so resetting the count when the application restart might not not be apropriate.

It also seems that if the user enter the wrong password, you won't find any user since your query also filter the password (I hope that password are encrypted in the database). Thus, I think that the user count won't be incremented but would be decremented anyway when the session ends.

You can always add some trace or information in the database to help finding what goes wrong.

It might be best to have a value per user in the database that indicate if the user is logged in and then simply count rows that has that flag set to have the number of active user as you won't need synchronization primitives (as far as I understand) and you should never get a negative value since you count objects that match a criteria.