Network service speed limiting method and electronic device

By introducing blocking arrays and wake-up computer system into the token bucket service, the problem of bandwidth sharing difficulties in network service programs when facing multiple sockets is solved, and more fair bandwidth allocation and priority transmission of important users is achieved.

CN115733805BActive Publication Date: 2025-03-04TIANJIN TIANDY DIGITAL TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211324606.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-10-27
Publication Date
2025-03-04
Estimated Expiration
2042-10-27

AI Technical Summary

Technical Problem

In the prior art, when a network service program faces multiple sockets, it is difficult for a network bandwidth to share, resulting in too few bandwidth resources obtained by some users.

Method used

By introducing a blocking array into the token bucket service, obtaining the situation of the user in the blocking array and the parameters of the current token bucket, calculate the next most recent user who can obtain the token and its wake-up time, and distribute the token when the wake-up time is reached.

Benefits of technology

When the number of tokens for multiple users is insufficient, the bandwidth resources of each user are taken into account, which alleviates the problem of too few bandwidth resources obtained by some users, and gives priority to providing bandwidth to more important users.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115733805B_ABST
    Figure CN115733805B_ABST
Patent Text Reader

Abstract

The present invention provides a network service speed limiting method and electronic device, which belongs to the technical field of network programming and alleviates the problem of too little bandwidth resources obtained by some users in the prior art. The method comprises: when a preset condition is met, the following steps are performed: obtaining the situation of users in a blocking array and the parameters of a current token bucket; wherein the blocking array is used to store a user queue to obtain a token; calculating the next nearest user who can obtain a token in the blocking array and the wake-up time for the nearest user to obtain the token according to the parameters of the token bucket; when the wake-up time is reached, distributing tokens to the nearest user and removing the nearest user from the blocking array.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of network programming, and in particular to a network service speed limiting method and electronic equipment. Background Art

[0002] If a network service involves uploading and downloading content resources, it is usually necessary to limit the speed of such activities. First, it is to limit the use of bandwidth, and second, it can allow all activities to share bandwidth resources relatively fairly.

[0003] The existing token bucket series algorithms (single bucket single speed, dual bucket dual speed, and combined with color blind or non-color blind mode, etc.) are all applied to network cards or switch firmware. When a message arrives, the result and color value given by the token bucket algorithm determine how the message is sent out. As for network service programs as applications, they are faced with multiple sockets. Usually, network service programs face many sockets of remote users, and each socket also sends and receives data through a thread, but these sockets belonging to different threads are difficult to share network bandwidth, so there is a problem that some users get too little bandwidth resources. Summary of the invention

[0004] The purpose of the present invention is to provide a network service speed limiting method and electronic device, which alleviates the problem in the prior art that some users obtain too little bandwidth resources.

[0005] In a first aspect, the present invention provides a network service rate limiting method, which is applied to a token bucket service, and the method comprises:

[0006] When the preset conditions are met, the following steps are performed:

[0007] Obtain the status of users in the blocking array and the parameters of the current token bucket; wherein the blocking array is used to store the user queue to obtain tokens;

[0008] Calculate the next nearest user who can obtain the token in the blocking array and the wake-up time when the nearest user obtains the token according to the parameters of the token bucket;

[0009] When the wake-up time is reached, tokens are distributed to the most recent user and the most recent user is removed from the blocking array.

[0010] Further, the step of calculating the next nearest user who can obtain the token in the blocking array and the wake-up time when the nearest user obtains the token according to the parameters of the token bucket includes:

[0011] The wake-up time of each user in the blocking array is calculated using the following formula:

[0012]

[0013] The time when the user gets enough tokens = the user's time difference + the time when the tokens were last distributed

[0014] Wake-up time = min (the time when the user gets enough tokens, the time when the user operation times out)

[0015] The user with the shortest wake-up time is determined as the next nearest user who can obtain the token.

[0016] Furthermore, the user weights are determined according to the order in which the users are added to the blocking array.

[0017] Furthermore, the preset condition is that a parameter of the token bucket changes, or a user joins or moves out of the blocking array.

[0018] Furthermore, the step of obtaining the status of users in the blocking array and the parameters of the current token bucket includes:

[0019] Get the parameters of the current token bucket;

[0020] Determine whether the current user has not yet joined the blocking array;

[0021] If not, the step of calculating the next nearest user who can obtain the token in the blocking array and the wake-up time when the nearest user obtains the token is performed according to the parameters of the token bucket;

[0022] Furthermore, after the step of determining whether the current user has not yet joined the blocking array, the following steps are also included:

[0023] If so, determine whether there are other users in the blocking array;

[0024] If yes, add the current user to the blocking array;

[0025] If not, the remaining tokens in the token bucket are distributed to the current user.

[0026] Furthermore, after the step of distributing the remaining tokens in the token bucket to the current user, the method further includes:

[0027] Determine whether the current user's token is sufficient;

[0028] If yes, it enters standby mode;

[0029] If not, add the current user to the blocking array.

[0030] In a second aspect, the present invention further provides an electronic device, comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, and wherein the processor implements the steps of the above method when executing the computer program.

[0031] In a third aspect, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions. When the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to execute the above method.

[0032] The network service speed limiting method provided by the present invention can be executed by a token bucket service user without occupying the thread of the token bucket service. When the preset conditions are met, the situation of the users in the blocking array and the parameters of the current token bucket are obtained, and the next nearest user who can obtain the token and the wake-up time of the nearest user to obtain the token are calculated. When the wake-up time arrives, the token is distributed to the nearest user and the nearest user is removed from the blocking array. When the number of tokens of multiple users is insufficient, the bandwidth resources of each user can be taken into account, which alleviates the problem of too little bandwidth resources obtained by some users in the prior art.

[0033] Correspondingly, an electronic device and a computer-readable storage medium provided by the embodiments of the present invention also have the above-mentioned technical effects. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0035] Figure 1 A flowchart of a network service speed limiting method provided by an embodiment of the present invention;

[0036] Figure 2 Another flow chart of the network service speed limiting method provided by an embodiment of the present invention;

[0037] Figure 3 A flowchart of adjusting token bucket parameters in an embodiment of the present invention;

[0038] Figure 4 A schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0039] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0040] The terms "including" and "having" and any variations thereof mentioned in the embodiments of the present invention are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device including a series of steps or units is not limited to the listed steps or units, but may optionally include other steps or units that are not listed, or may optionally include other steps or units that are inherent to these processes, methods, products or devices.

[0041] Usually, network service programs face many remote user sockets, and each socket also sends and receives data through a thread. We hope that these sockets belonging to different threads can share network bandwidth and send data flexibly through delay. At the same time, we may also need to allow more important activities to obtain more bandwidth resources.

[0042] The existing token bucket single-bucket single-speed algorithm is roughly as follows:

[0043] Tokens are generated in the token bucket at a certain rate, and each token represents the right to send one byte. The token bucket has a capacity limit, and the tokens generated cannot exceed the capacity. The user tries to get a certain number of tokens from the bucket. If there are enough tokens in the bucket, the corresponding number of tokens will be taken out and a pass will be returned to the user. The user can then send data of the token size. If there are not enough tokens in the bucket, a prohibition will be returned to the user, and the user will give up sending this data.

[0044] The embodiment of the present invention is based on the token bucket algorithm, but the processing of user requests is different. The algorithm idea is as follows:

[0045] Provide a token bucket, which generates tokens at a certain rate. Each token represents the right to send one byte. The token bucket has a capacity limit, and the tokens generated cannot exceed the capacity. The user tries to get a certain number of tokens from the bucket. If there are enough tokens in the bucket, the corresponding number of tokens will be taken out and returned to the user. The user can then send data of the token size.

[0046] If there are insufficient tokens in the bucket, the user will be blocked in this operation, waiting for enough tokens to be generated in the bucket, or the user operation times out and returns a certain number of tokens that can be obtained at the time of timeout.

[0047] If multiple users are blocked in this operation, a user queue for storing the tokens to be obtained is formed, that is, a blocking array. They will distribute the tokens generated in the bucket according to their respective weights until the user obtains enough tokens, or the user operation times out, and a certain number of tokens that can be obtained at the time of timeout are returned.

[0048] Based on this, the embodiment of the present invention provides a network service speed limiting method, which can be applied to token bucket service users, such as Figure 1As shown, the method comprises the following steps:

[0049] When the preset conditions are met, the following steps are performed:

[0050] S1: Get the status of users in the blocking array and the parameters of the current token bucket.

[0051] The blocking array is used to store the user queues to be provided with tokens, and the parameters of the token bucket include the capacity of the token bucket, the token generation rate, etc.

[0052] S2: Calculate the next nearest user in the blocking array who can obtain the token and the wake-up time when the nearest user obtains the token based on the parameters of the token bucket.

[0053] S3: When the wake-up time is reached, tokens are distributed to the nearest user and the nearest user is removed from the blocking array.

[0054] The purpose of setting the above preset conditions is to perform token distribution operations at critical moments, so the preset conditions can be:

[0055] The parameters of the token bucket change (such as the rate at which tokens are generated is artificially changed);

[0056] A user joins the blocking array or moves out of the blocking array (a user operation times out or wakes up).

[0057] In a possible implementation, step S2 includes:

[0058] The wake-up time of each user in the blocking array is calculated using the following formula:

[0059]

[0060] Among them, the user weight is determined according to the order in which the user joins the blocking array. For example, the user who joins the blocking array earliest has the largest weight, and the weights of users who join the blocking array later decrease in sequence. At the same time, the weight can also be assigned based on the importance of the user. For example, the weight of users who transmit important data can be appropriately increased. The purpose of 1000 in the formula is to convert milliseconds of the token generation rate into seconds.

[0061] The time when the user gets enough tokens = the user's time difference + the time when the tokens were last distributed

[0062] Wake-up time = min (the time when the user gets enough tokens, the time when the user operation times out)

[0063] The user with the shortest wake-up time is determined as the next nearest user who can obtain the token.

[0064] The most recent user is the next user who will "get enough tokens or the operation times out", and can also be called the "next user". When this user times out or the token is enough to wake up, the user thread calls the function that distributes tokens. Therefore, it is necessary to calculate the time when each user "gets enough tokens or the operation times out", that is, calculate the smaller of the two times "the time when enough tokens are obtained" and "the time when the operation times out".

[0065] The network service speed limiting method provided by the embodiment of the present invention can be executed by a token bucket service user without occupying the thread of the token bucket service. When the preset conditions are met, the situation of the users in the blocking array and the parameters of the current token bucket are obtained, and the next nearest user who can obtain the token and the wake-up time for the nearest user to obtain the token are calculated. When the wake-up time arrives, the token is distributed to the nearest user and the nearest user is removed from the blocking array. When the number of tokens for multiple users is insufficient, the bandwidth resources of each user can be taken into account, which alleviates the problem of too little bandwidth resources obtained by some users in the prior art.

[0066] like Figure 2 As shown, the detailed implementation of the network service speed limiting method provided by the embodiment of the present invention is as follows:

[0067] The token bucket service is first in standby state. When a user joins or moves out of the blocking array (meeting the preset conditions), the user thread calls the function of distributing tokens:

[0068] Step a, obtaining parameters: including the situation of each user in the blocking array (the number of tokens still required by the user, the timeout time, the user weight), and the parameters of the current token bucket (the capacity of the token bucket, the token generation rate).

[0069] Step b, determine whether the current user has been removed from the blocking array.

[0070] If the preset condition is that the current user moves out of the blocking array, the process returns to the standby state; if the preset condition is that the current user joins the blocking array, the process proceeds to the next step.

[0071] Step c, obtain the current time.

[0072] Step d, determine whether the current user has not yet joined the blocking array, that is, determine whether the current user is a new user. If not, execute step i; if yes, execute step e.

[0073] Step e, determine whether there are other users in the blocking array. If yes, execute step h; if no, execute step f.

[0074] Step f: distribute the remaining tokens in the token bucket to the current user.

[0075] If there is only the current user in the blocking array, the remaining tokens in the token bucket are distributed to the current user.

[0076] Step g, determine whether the current user's token is sufficient. If yes, enter the standby state; if not, execute step h.

[0077] Step h, add the current user to the blocking array.

[0078] If the remaining tokens in the token bucket are not enough for the current user, the current user is added to the blocking array and the number of tokens still required by the current user is recorded.

[0079] Step i: Calculate the most recent user and his / her wake-up time.

[0080] Specifically, the algorithm of step S2 mentioned above may be used.

[0081] Step j: whether the current user is the most recent user. If yes, execute step k; if no, execute step l.

[0082] Step k: When the wake-up time is reached, tokens are distributed to the most recent user (current user), and the most recent user is removed from the blocking array. After that, the system enters the standby state.

[0083] Step 1, wake up the most recent user. The most recent user thread calls the token distribution function. When the wake-up time is reached, the token is distributed to the most recent user and the most recent user is removed from the blocking array. After that, the system enters the standby state.

[0084] like Figure 3 As shown in FIG. 1 , when the bucket parameters need to be adjusted dynamically, such as changing the token bucket capacity or the token production rate, the generated tokens need to be calculated and distributed according to the original parameters before the change.

[0085] S31: Distribute tokens.

[0086] The distribution can be performed using the above-mentioned function of distributing tokens.

[0087] S32: Make the number of tokens equal to the bucket capacity.

[0088] If you are setting token bucket parameters for the first time, set the current number of tokens in the token bucket to be equal to the bucket capacity.

[0089] S33: Set bucket parameters.

[0090] Set parameters such as token bucket capacity or token production rate.

[0091] S34: Calculate the maximum delay.

[0092] When the time between the first token distribution or the two token distributions is long, the calculated number of tokens may exceed the computer's limit, so when the time difference exceeds a certain value, the time difference is normalized to this value and the maximum delay.

[0093] The maximum delay can be large enough, and 65536 times the time it takes to fill the bucket can be taken as the maximum delay. For example, if the token generation rate of the bucket is prod_rate = 100*1024*1024 (i.e. 100MB / S), the maximum delay is calculated as:

[0094]

[0095] The formula for the number of tokens generated is:

[0096]

[0097] The purpose of the 1000 in the formula is to convert milliseconds of the token generation rate into seconds.

[0098] S35: Calculate the nearest user.

[0099] Specifically, the algorithm of step S2 mentioned above may be used.

[0100] S36: If there is a recent user, wake up the recent user.

[0101] The function that distributes tokens is called by the most recent user thread.

[0102] The network service speed limiting method provided by the embodiment of the present invention can be executed by a token bucket service user without occupying the thread of the token bucket service. By calculating the next nearest user who can obtain the token and its wake-up time, when the wake-up time arrives, the token is distributed to the nearest user, and the nearest user is removed from the blocking array. When the number of tokens of multiple users is insufficient, the wake-up time is calculated using the weight of each user, which can take into account the bandwidth resources of each user and adapt to various situations of single user blocking and multiple user blocking, thereby alleviating the problem of too little bandwidth resources obtained by some users in the prior art, and can also give priority to providing bandwidth for the transmission of more important users.

[0103] An electronic device provided by an embodiment of the present invention, such as Figure 4 As shown, the electronic device 800 includes a memory 801 and a processor 802. The memory stores a computer program that can be run on the processor. When the processor executes the computer program, the steps of the method provided in the above embodiment are implemented.

[0104] like Figure 4As shown, the electronic device further includes: a bus 803 and a communication interface 804, a processor 802, a communication interface 804 and a memory 801 are connected via the bus 803; the processor 802 is used to execute executable modules stored in the memory 801, such as computer programs.

[0105] The memory 801 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 804 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.

[0106] The bus 803 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 4 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0107] Among them, the memory 801 is used to store programs, and the processor 802 executes the program after receiving the execution instruction. The method executed by the device defined by the process disclosed in any embodiment of the present invention can be applied to the processor 802 or implemented by the processor 802.

[0108] The processor 802 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 802. The above processor 802 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present invention can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiment of the present invention can be directly embodied as a hardware decoding processor for execution, or a combination of hardware and software modules in the decoding processor for execution. The software module may be located in a storage medium mature in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 801, and the processor 802 reads the information in the memory 801 and completes the steps of the above method in combination with its hardware.

[0109] Corresponding to the above method, an embodiment of the present invention further provides a computer-readable storage medium, which stores machine-executable instructions. When the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to execute the steps of the above method.

[0110] Finally, it should be noted that the above-described embodiments are only specific implementations of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The protection scope of the present invention is not limited thereto. Although the present invention is described in detail with reference to the above-mentioned embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-mentioned embodiments within the technical scope disclosed by the present invention, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present invention. They should all be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.

Claims

1. A network service speed limiting method, characterized in that: Applied to the token bucket service, the method comprises: When the preset conditions are met, the following steps are performed: Obtain the status of users in the blocking array and the parameters of the current token bucket; wherein the blocking array is used to store the user queue to obtain tokens; The next nearest user who can obtain a token in the blocking array and the wake-up time when the nearest user obtains the token are calculated according to the parameters of the token bucket; including: The wake-up time of each user in the blocking array is calculated using the following formula: The time when the user gets enough tokens = the user's time difference + the time when the tokens were last distributed Wake-up time = min (The moment when the user obtains enough tokens, the moment when the user operation times out) The user with the shortest wake-up time is determined as the next nearest user who can obtain the token; When the wake-up time is reached, tokens are distributed to the most recent user and the most recent user is removed from the blocking array; The user weight is determined according to the order in which the users join the blocking array; the preset condition is that the parameters of the token bucket change, or a user joins or moves out of the blocking array.

2. The method according to claim 1, characterized in that: The steps for obtaining the status of users in the blocking array and the parameters of the current token bucket include: Get the parameters of the current token bucket; Determine whether the current user has not yet joined the blocking array; If not, the step of calculating the next nearest user in the blocking array who can obtain the token and the wake-up time when the nearest user obtains the token is performed according to the parameters of the token bucket.

3. The method according to claim 2, characterized in that After the step of determining whether the current user has not yet joined the blocking array, it also includes: If so, determine whether there are other users in the blocking array; If yes, add the current user to the blocking array; If not, the remaining tokens in the token bucket are distributed to the current user.

4. The method according to claim 3, characterized in that After distributing the remaining tokens in the token bucket to the current user, the following steps are also included: Determine whether the current user's token is sufficient; If yes, it enters standby mode; If not, add the current user to the blocking array.

5. An electronic device comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 4 are implemented.

6. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions. When the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to execute the method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • Flow control method and device

    CN110768913A

  • Distributed current limiting method, device and system, equipment and storage medium

    CN113765818A