Interface processing method and system for limiting concurrency by single user
The Redis cache-based distributed lock system ensures single-user concurrency by managing locks with request counters, preventing multiple accounts from being logged out simultaneously and improving business call success rates.
Patent Information
- Application Number
- CN202411936082.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-26
- Publication Date
- 2025-05-27
AI Technical Summary
Existing systems fail to prevent multiple accounts of the same user from being forcibly logged out simultaneously, leading to frequent business call failures when no restrictions are applied during user interactions.
Implement a single-user concurrency control method using Redis cache to manage distributed locks, where user account information is recorded in the lock, and usernames are used as identifiers to acquire locks, with request counters ensuring only one user can access concurrently.
Ensures that only one user account can access concurrently, preventing other accounts from being logged out unexpectedly, thereby significantly increasing the success rate of business calls and enhancing user experience.
Smart Images

Figure CN120045567A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of account login, and more particularly, to an interface processing method and system for restricting concurrency for a single user. Background Art
[0002] The content included in the login information is the account number, username, and password. Among them, there is a many-to-many relationship between the account number and the username. From the perspective of user security, at the same time, only one user can perform business calls. If there are no restrictions when the user calls the business, then it will cause the multiple accounts of the same user to be forced to log off each other. And in this state, a large number of failed results will occur during the business calls during this period. Summary of the Invention
[0003] According to the present invention, an interface processing method and system for restricting concurrency for a single user are provided to solve the technical problem that if there are no restrictions when the user calls the business, then it will cause the multiple accounts of the same user to be forced to log off each other. And in this state, a large number of failed results will occur during the business calls during this period.
[0004] According to the first aspect of the present invention, an interface processing method for restricting concurrency for a single user is provided, including:
[0005] Obtain a distributed lock from the redis cache and record the user's account information in the distributed lock;
[0006] Use the username as the identifier of the distributed lock to grab the distributed lock, where multiple users are grabbing the distributed lock at the same time;
[0007] Based on the request counter, determine whether the distributed lock is successfully grabbed. If the distributed lock is not grabbed, obtain the account information of the user recorded in the distributed lock, and determine whether the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock;
[0008] If the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock, reset the expiration time of the distributed lock and perform an atomic increment operation on the request counter.
[0009] Optionally, it further includes:
[0010] If the distributed lock can be grabbed, perform an atomic increment operation on the request counter and then execute the business logic.
[0011] Optionally, it further includes:
[0012] When the business logic is executed, perform an atomic decrement on the request counter;
[0013] Judge whether the value in the request counter is 0. If it is 0, release the distributed lock.
[0014] Optionally, it further includes:
[0015] If the account information recorded in the distributed lock is inconsistent with the account information of the current request to grab the distributed lock, return a prompt message indicating that the lock grab fails to the user.
[0016] According to another aspect of the present invention, there is also provided an interface processing system for restricting concurrency for a single user, including:
[0017] A distributed lock acquisition module, configured to acquire a distributed lock from the redis cache and record the user's account information in the distributed lock;
[0018] A distributed lock grab module, configured to use the user name as the identifier of the distributed lock to grab the distributed lock, where multiple users are simultaneously grabbing the distributed lock;
[0019] A module for failing to grab the distributed lock, configured to determine whether the distributed lock is successfully grabbed based on the request counter. If the distributed lock is not grabbed, obtain the account information of the user recorded in the distributed lock, and determine whether the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock;
[0020] A distributed lock reset module, configured to reset the expiration time of the distributed lock and perform an atomic increment operation on the request counter if the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock.
[0021] Optionally, it further includes:
[0022] A module for successfully grabbing the distributed lock, configured to perform an atomic increment operation on the request counter and then execute the business logic if the distributed lock can be grabbed.
[0023] Optionally, it further includes:
[0024] A business logic execution module, configured to perform an atomic decrement on the request counter after the business logic is executed;
[0025] A distributed lock release module, configured to judge whether the value in the request counter is 0. If it is 0, release the distributed lock.
[0026] Optionally, it further includes:
[0027] A module for returning a lock grab failure message, configured to return a prompt message indicating that the lock grab fails to the user if the account information recorded in the distributed lock is inconsistent with the account information of the current request to grab the distributed lock.
[0028] According to another aspect of the present invention, there is also provided a computer-readable storage medium, on which a computer program is stored, characterized in that when the program is executed by a processor, the steps of the method described in any one of the above are implemented.
[0029] According to another aspect of the present invention, there is also provided an electronic device, characterized in that it includes:
[0030] The computer-readable storage medium described above; and
[0031] One or more processors for executing the program in the computer-readable storage medium.
[0032] Thus, by utilizing the atomicity characteristic of the redis cache execution command, the function of the distributed lock is implemented. At the same time, combined with the number of calls recorded by the request counter, it can effectively ensure that each user can make concurrent calls for the same account, but different accounts cannot make calls at the same time. In this way, the success rate of business calls is greatly improved, and the situation where an account is forced to log off by other accounts during use is effectively avoided. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] By referring to the following drawings, the exemplary embodiments of the present invention can be more completely understood:
[0034] Figure 1 It is a schematic flowchart of a single-user restricted concurrency interface processing method described in this embodiment;
[0035] Figure 2 It is a schematic logical diagram of a single-user restricted concurrency interface processing method described in this embodiment;
[0036] Figure 3 It is a schematic diagram of a single-user restricted concurrency interface processing system described in this embodiment. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0037] Now, the exemplary embodiments of the present invention will be introduced with reference to the drawings. However, the present invention can be implemented in many different forms and is not limited to the embodiments described herein. These embodiments are provided to disclose the present invention in detail and completely, and to fully convey the scope of the present invention to those skilled in the art. The terms in the exemplary embodiments shown in the drawings are not limitations on the present invention. In the drawings, the same unit / element uses the same reference numeral.
[0038] Unless otherwise specified, the terms (including technical terms) used herein have the commonly understood meanings to those skilled in the art. In addition, it is understood that the terms defined in commonly used dictionaries should be understood to have the same meanings as those in the context of the relevant fields, and should not be understood as idealized or overly formal meanings.
[0039] According to a first aspect of the present invention, a single-user concurrency-limited interface processing method 100 is provided. Figure 1 As shown, the method 100 includes:
[0040] S101: Obtain a distributed lock from the redis cache and record the user's account information in the distributed lock;
[0041] S102: Using the user name as the identifier of the distributed lock to grab the distributed lock, wherein multiple users are grabbing the distributed lock at the same time;
[0042] S103: judging whether the distributed lock is successfully grabbed based on the request counter; if the distributed lock is not grabbed, obtaining the user's account information recorded in the distributed lock, and judging whether the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock;
[0043] S104: If the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock, reset the expiration time of the distributed lock and perform an atomic increment operation on the request counter.
[0044] Among them, the distributed lock is an exclusive lock (Exclusive Locks), referred to as X locks, also known as write locks or exclusive locks, which is a basic lock type. If transaction T1 adds an exclusive lock to data object O1, then during the entire locking period, only transaction T1 is allowed to read and update O1, and no other transaction can perform any type of operation on this data object - until T1 releases the exclusive lock. The core of the exclusive lock is how to ensure that there is only one transaction that obtains the lock, and after the lock is released, all transactions waiting to obtain the lock can be notified.
[0045] Distributed locks should meet the following conditions:
[0046] ① In a distributed system environment, a method can only be executed by one thread of a machine at the same time;
[0047] ② Highly available acquisition and release of locks;
[0048] ③High-performance lock acquisition and lock release;
[0049] ④It has the feature of reentrancy;
[0050] ⑤Have a lock failure mechanism to prevent deadlocks;
[0051] ⑥Have the characteristics of a non-blocking lock, that is, if the lock cannot be acquired, it will directly return that the lock acquisition fails.
[0052] Specifically, the present invention mainly includes two important parts. One is the acquisition of the lock, and the other is the release of the lock. In the whole process, acquiring the lock is a necessary prerequisite for releasing the lock. When performing the lock acquisition operation, the user name is used as the lock identifier, and at the same time, the account information of the operation is detailedly recorded in the lock. And when performing the lock release operation, it is necessary to ensure that all current business requests have been completely executed.
[0053] Reference Figure 2 As shown, the overall process is as follows:
[0054] Step 1: The request enters and needs to grab the lock first;
[0055] Step 2: If the lock is successfully grabbed, perform an atomic increment operation on the request counter. If the lock cannot be grabbed, determine whether the account recorded in the lock is the same as the account of the current request. If they are the same, also perform an atomic increment operation on the request counter; if they are different, return a friendly prompt message to the user.
[0056] Step 3: Execute the business logic;
[0057] Step 4: Atomically decrement the request counter;
[0058] Step 5: Determine whether the value in the request counter is 0. If it is 0, release the lock.
[0059] Optionally, it also includes:
[0060] If the distributed lock can be grabbed, perform an atomic increment operation on the request counter and then execute the business logic.
[0061] Optionally, it also includes:
[0062] After the business logic is executed, atomically decrement the request counter;
[0063] Determine whether the value in the request counter is 0. If it is 0, release the distributed lock.
[0064] Optionally, it also includes:
[0065] If the account information recorded in the distributed lock is different from the account information of the current request to grab the distributed lock, return a prompt message indicating that the lock grab fails to the user.
[0066] The present invention realizes the concurrent limit of multiple user accounts and has the following advantages:
[0067] First: In the case where there is a many-to-many relationship between accounts and usernames, the present invention successfully enables a single account to concurrently invoke services, while multiple accounts corresponding to a single username cannot be invoked simultaneously. During this process, the present invention also provides a highly versatile idea for concurrent limit, providing a new direction and possibility for the development of related technical fields.
[0068] Second: By strictly restricting the concurrent use of multiple user accounts, the present invention effectively solves the thorny problem of different accounts being forcibly logged out of each other when a user invokes a service. In this way, users can more smoothly invoke services, greatly improving the user experience and bringing more convenient and efficient services to users.
[0069] According to another aspect of the present invention, there is also provided an interface processing system 300 for restricting concurrency for a single user. Referring to Figure 3 as shown, the system 300 includes:
[0070] A distributed lock acquisition module 310, configured to acquire a distributed lock from a redis cache and record the user's account information in the distributed lock;
[0071] A distributed lock grabbing module 320, configured to use the username as an identifier of the distributed lock to grab the distributed lock, where multiple users are simultaneously grabbing the distributed lock;
[0072] A module for not grabbing the distributed lock 330, configured to determine whether the distributed lock is successfully grabbed based on a request counter. If the distributed lock is not grabbed, obtain the account information of the user recorded in the distributed lock, and determine whether the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock;
[0073] A distributed lock reset module 340, configured to, if the account information recorded in the distributed lock is consistent with the account information of the current request to grab the distributed lock, reset the expiration time of the distributed lock and perform an atomic increment operation on the request counter.
[0074] Optionally, it further includes:
[0075] A module for grabbing the distributed lock, configured to, if the distributed lock can be grabbed, perform an atomic increment operation on the request counter and then execute the business logic.
[0076] Optionally, it further includes:
[0077] A business logic execution module, configured to, after the business logic is executed, perform an atomic decrement on the request counter;
[0078] A distributed lock release module, configured to determine whether the value in the request counter is 0. If it is 0, release the distributed lock.
[0079] Optionally, it further includes:
[0080] A module for returning a lock acquisition failure message, which is used to return a prompt message of lock acquisition failure to the user if the account information recorded in the distributed lock is inconsistent with the account information of the current request to acquire the distributed lock.
[0081] An interface processing system 300 for single-user restricted concurrency according to an embodiment of the present invention corresponds to a method 100 for single-user restricted concurrency according to another embodiment of the present invention, and will not be described in detail herein.
[0082] According to another aspect of the present invention, there is also provided a computer-readable storage medium, on which a computer program is stored, characterized in that when the program is executed by a processor, the steps of the method described in any one of the above are implemented.
[0083] According to another aspect of the present invention, there is also provided an electronic device, characterized in that it includes:
[0084] The computer-readable storage medium described above; and
[0085] One or more processors for executing the program in the computer-readable storage medium.
[0086] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code. The solutions in the embodiments of the present application can be implemented in various computer languages, for example, object-oriented programming languages such as Java and interpreted scripting languages such as JavaScript.
[0087] The present application is described with reference to the flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, and the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate means for implementing the functions specified in Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0088] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to operate in a particular manner, such that the instructions stored in the computer-readable memory produce a manufacture including an instruction device that implements the functions specified in one or more of the procedures Figure 1 or more procedures and / or blocks Figure 1 or more blocks specified in the function.
[0089] These computer program instructions can also be loaded onto a computer or other programmable data processing apparatus, such that a series of operational steps are performed on the computer or other programmable apparatus to produce a computer-implemented process, whereby the instructions executed on the computer or other programmable apparatus provide steps for implementing the functions specified in one or more of the procedures Figure 1 or more procedures and / or blocks Figure 1 or more blocks specified in the function.
[0090] Although the preferred embodiments of the present application have been described, those skilled in the art can make additional changes and modifications once they learn the basic creative concepts. Therefore, the appended claims are intended to be construed to include the preferred embodiments and all changes and modifications that fall within the scope of the present application.
[0091] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalent technologies, the present application is also intended to include these modifications and variations.
Claims
1. A single-user limited concurrent interface processing method, characterized in that: include: Get the distributed lock from the redis cache and record the user's account information in the distributed lock; Using the user name as the identifier of the distributed lock to grab the distributed lock, where multiple users are grabbing the distributed lock at the same time; Based on the request counter, determine whether the distributed lock is successfully grabbed. If the distributed lock is not grabbed, obtain the user's account information recorded in the distributed lock, and determine whether the account information recorded in the distributed lock is consistent with the account information currently requesting to grab the distributed lock; If the account information recorded in the distributed lock is consistent with the account information currently requesting to grab the distributed lock, the expiration time of the distributed lock is reset and the request counter is atomically incremented by one.
2. The method according to claim 1, characterized in that Also includes: If the distributed lock can be obtained, the request counter is atomically incremented by one and the business logic is executed.
3. The method according to claim 2, characterized in that Also includes: When the business logic is executed, the request counter is atomically reduced by one; It is determined whether the value in the request counter is 0, and if so, the distributed lock is released.
4. The method according to claim 1, characterized in that: Also includes: If the account information recorded in the distributed lock is inconsistent with the account information currently requesting to grab the distributed lock, a prompt message indicating that the lock grabbing failed is returned to the user.
5. A single-user limited concurrency interface processing system, characterized in that: include: Get the distributed lock module, which is used to obtain the distributed lock from the redis cache and record the user's account information in the distributed lock; A distributed lock grabbing module is used to grab the distributed lock using the user name as the identifier of the distributed lock, wherein multiple users are grabbing the distributed lock at the same time; The distributed lock failure module is used to determine whether the distributed lock is successfully seized based on the request counter. If the distributed lock is not seized, the user's account information recorded in the distributed lock is obtained to determine whether the account information recorded in the distributed lock is consistent with the account information of the current request to seize the distributed lock; The distributed lock reset module is used to reset the expiration time of the distributed lock and atomically add one to the request counter if the account information recorded in the distributed lock is consistent with the account information currently requesting to grab the distributed lock.
6. The system according to claim 5, characterized in that Also includes: The distributed lock module is used to atomically increment the request counter by one if the distributed lock can be grabbed, and then execute the business logic.
7. The system according to claim 6, characterized in that Also includes: The business logic execution module is used to atomically reduce the request counter by one after the business logic is executed; The distributed lock release module is used to determine whether the value in the request counter is 0, and if it is 0, release the distributed lock.
8. The system according to claim 5, characterized in that Also includes: The lock grabbing failure information module is used to return the lock grabbing failure prompt information to the user if the account information recorded in the distributed lock is inconsistent with the account information currently requesting to grab the distributed lock.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the method according to any one of claims 1 to 4 are implemented.
10. An electronic device, characterized in that: include: The computer readable storage medium as claimed in claim 9; as well as One or more processors are used to execute the program in the computer-readable storage medium.
Citation Information
Patent Citations
Distributed session processing method, device and system
CN111385356A
Client number distribution method and device based on Redis cluster
CN112688799A
High-concurrency seckilling system
CN114119129A
Method for controlling account single device login based on Redis in distributed system
CN114238888A