[0036] In the embodiment of the present invention, the network node device obtains the token bucket parameters according to the attributes of the message; then obtains the current number of tokens according to the set time threshold and the token bucket parameters; compares the current number of tokens with the current The length of the message, the message is processed according to the comparison result.
[0037] Hereinafter, the present invention will be further described in detail through the accompanying drawings and specific embodiments.
[0038] The flow of the token bucket rate limiting method provided by the embodiment of the present invention is as follows figure 1 As shown, the method mainly includes the following steps:
[0039] Step 201: The network node device obtains token bucket parameters according to the attributes of the message;
[0040] Specifically, the network node device finds the token bucket parameters carried in the message data packet through the index of the message data packet; where,
[0041] The token bucket parameters include: token addition rate token_rate, the last message arrival time time_last, the number of tokens token_last after the token bucket has limited the rate of the last message, and the token bucket capacity bucket_size;
[0042] The bucket_size of the token bucket is used to limit the total amount of tokens in the token bucket; for example, if the bucket_size of a token bucket is 100Mbit, it means that the token bucket can hold at most 100Mbit tokens. Cards will overflow the token bucket.
[0043] Step 202: The network node device obtains the current number of tokens according to the time threshold time_threshhold and the token bucket parameter;
[0044] Specifically, the network node device determines the time difference delta_time according to the arrival time of the last message in the token bucket parameters; determines the current message arrival time order according to the time threshold time_threshhold, the time difference delta_time, and the token addition rate in the token bucket parameters The added amount delta_token of the card; and then the current amount of tokens token_current is determined according to the added amount delta_token of the current message at the time of arrival of the message and the number of tokens after the last message rate limit operation in the token bucket parameter; where,
[0045] The network node device determining the time difference delta_time according to the last message arrival time in the token bucket parameter is specifically: subtracting the current message arrival time time_now from the last message arrival time time_last to obtain the time difference delta_time, namely: delta_time= time_now-time_last;
[0046] For example, starting from time 0, the current message arrival time time_now is 10ms, the last message arrival time time_last is 6ms, and the time difference delta_time is equal to 4ms by the above algorithm;
[0047] According to the time threshold time_threshhold, the time difference delta_time, and the token addition rate in the token bucket parameter, obtaining the token addition amount delta_token at the time of the current message arrival is specifically: comparing the time threshold value time_threshhold and the time difference delta_time, when the time difference delta_time When the time threshold is greater than the time_threshhold, the amount of token addition delta_token at the time of the current message arrival is equal to the product of the time difference delta_time and the token addition rate token_rate, that is, delta_token=delta_time*token_rate; when the time difference delta_time is less than or equal to the When the time threshold is time_threshhold, the amount delta_token added to the token at the time of the current message arrival is equal to zero, that is, delta_token=0;
[0048] The above method is used because the time threshold time_threshhold can divide the addition of tokens into the token bucket into random block additions, that is, add tokens to the token bucket every time (this period of time is greater than the time threshold). The number of tokens added will be relatively large. Therefore, the total amount of tokens in the token bucket will always maintain a relatively large number after each addition. In this way, regardless of whether the currently arriving message is a long packet or a short packet, the token The total amount is sufficient to serve the current message, so that the long packet can obtain the same token service opportunity as the short packet;
[0049] The time threshold time_threshhold can be a preset threshold; the time threshold time_threshhold can not be set too large, too large will cause the token in the token bucket to overflow, resulting in the loss of rate-limiting bandwidth, and it cannot be too small. Can not improve the service quality of long-packet rate limit; set the appropriate time threshold time_threshhold according to the message sending rate and the token adding rate of the token bucket;
[0050] For example, the token bucket parameters carried in the message data packet are as follows: the token adding rate token_rate=8000bit/s, the last message arrival time time_last=6ms, the token bucket is the order after the rate limit operation of the last message The number of cards token_last=5000bit, the current message arrival time is 10ms, and the preset time_threshhold=3ms; according to the above method:
[0051] delta_time=4ms; 4ms is greater than the time threshold 3ms; therefore,
[0052] delta_token=delta_time*token_rate=0.004s*8000bit/s=32bit;
[0053] Said obtaining the current number of tokens token_current according to the added amount delta_token of the token at the time of arrival of the current message is specifically: the current number of tokens token_current is equal to the token after the token bucket has operated on the rate limit of the previous message The sum of the quantity token_last and the added quantity delta_token of the token at the moment of arrival of the current message, namely token_current=token_last+delta_token; and the maximum value of the current token quantity token_current is the value of the token bucket capacity bucket_size, namely token_current=
[0054] For example, in the above example, token_current=5000+32=5032bit.
[0055] Step 203: The network node device compares the current number of tokens with the current message length, and performs corresponding processing on the message according to the comparison result;
[0056] Specifically, the current number of tokens token_current is compared with the current packet length, and when the current number of tokens token_current is greater than or equal to the current packet length, the current packet is forwarded, and the number of tokens in the token bucket token_current Reduce the number equal to the length of the current message; when the current number of tokens token_current is less than the length of the current message, the message is discarded, and the number of tokens in the token bucket remains unchanged;
[0057] For example, in the above example, the current number of tokens token_curren is 5032bit. If the current message length is 100 bytes, that is, 800bit, the message length is less than token_curren, which means that the tokens in the token bucket are enough to serve the message , The token bucket forwards the message, and the number of tokens in the token bucket is reduced by 800 bits, and the current number of tokens in the token bucket is 4232 bits; if the current message length is 1000 bytes, that is, 8000 bits, the message length If it is greater than token_curren, it means that there are not enough tokens in the token bucket to serve the message, and the message is discarded. The number of tokens in the token bucket is still 5032 bits.
[0058] image 3 Shown is a schematic diagram of the token bucket rate limit provided by the embodiment of the present invention; as shown in the figure, when adding tokens to the token bucket, first determine whether the time difference delta_time is greater than the time threshold time_threshhold, and when it is greater than the time threshold time_threshhold When, add the token to the token bucket, otherwise no token is added;
[0059] The above-mentioned time difference delta_time is the time difference between the current message arrival time time_now and the previous message arrival time time_last; the time threshold time_threshhold is a preset threshold, which is used to add tokens in random blocks instead of linear addition;
[0060] After tokens are added to the token bucket, the current number of tokens in the token bucket, token_current, is the sum of the added amount of tokens delta_token this time and the number of tokens token_last after the last packet rate limit calculation; and, the current The maximum number of tokens token_current is the total capacity of the token bucket bucket_size, that is, when the sum of the added amount of tokens delta_token and the number of tokens token_last after the last packet rate limit calculation exceeds the total capacity of the token bucket bucket_size , The token will overflow out of the bucket;
[0061] The processing of the current message is: compare the current message length with the current number of tokens token_current. When the current number of tokens token_current is greater than or equal to the current message length, it indicates that there are enough tokens in the token bucket at this time. It can serve the current message, forward the message, and reduce the number of tokens in the token bucket by a number equal to the length of the message; when the current number of tokens token_current is less than the length of the current message, it indicates that the token bucket is in the token bucket at this time. Insufficient tokens to serve the current message, discard the message, and the number of tokens in the token bucket remains unchanged;
[0062] Through the above set time threshold time_threshhold, the addition of tokens becomes a non-continuous block addition. Because this block addition method adds a large number of tokens each time, the long packet and short packet of the message face equal Forwarding opportunity, which solves the problem that short packets passing through the existing token rate limiting method are far greater than long packets;
[0063] For example, the capacity of the token bucket is 1500 bytes, and the token bucket is full at the beginning, and the token bucket rate is limited for a group of messages with different lengths. There are two kinds of messages with 60 bytes and 1000 bytes. Mixed sending, the message sending rate is 120 bytes/s, and the token adding rate of the token bucket is 100 bytes/s, if the existing linear token bucket rate limit method is used, the token bucket will be It will be in a state of “empty”. The tokens in the token bucket are only enough to serve 60-byte messages, and 1000-byte messages are difficult to obtain token services for a long time. Therefore, 1000-byte messages The quality of the message service will be greatly affected. Only in the initial stage of the message transmission, it can be forwarded through the token bucket, and the token service cannot be obtained in the later period. The chance of passing a 60-byte message is much greater than that of a 1000-byte message ; However, the use of the solution provided by the present invention will avoid the above problems; if the time threshold time_threshhold is set to 6s, the token is added when the message transmission interval is greater than 6s, otherwise it is not added; this random block addition method The advantage is that a large number of tokens can be added each time, and the added amount is greater than 600 bytes. The number of tokens in the token bucket will maintain a large base after each addition, and it will not be due to message The sending rate is higher than the token adding rate and the token bucket is exhausted. The number of tokens in the token bucket is enough to serve the 1000-byte long packet and the 60-byte short packet. Moreover, the long packet and the short packet are obtained. Opportunities for licensing services are equal.
[0064] Figure 4 Shown is a schematic structural diagram of a token bucket rate limiting device provided by an embodiment of the present invention. The device includes: a parameter acquisition module 40, a determination module 41, and a message processing module 42; wherein,
[0065] The parameter obtaining module 40 is configured to obtain token bucket parameters according to the attributes of the message;
[0066] The determining module 41 is configured to determine the current number of tokens according to the time threshold and the token bucket parameters;
[0067] The message processing module 42 is configured to compare the current number of tokens with the current message length, and perform corresponding processing on the message according to the comparison result;
[0068] The parameter acquisition module 40 is specifically configured to search for the token bucket parameters carried in the message data packet through the index of the message data packet; wherein,
[0069] The token bucket parameters include: token addition rate token_rate, the last message arrival time time_last, the number of tokens token_last after the token bucket has limited the rate of the last message, and the token bucket capacity bucket_size;
[0070] The token bucket capacity bucket_size is used to limit the total token amount in the token bucket;
[0071] The structure of the determining module 41 is Figure 5 As shown, it specifically includes: a time difference determination module 50, a token addition amount determination module 51, and a token quantity determination module 52; wherein,
[0072] The time difference determining module 50 is configured to determine the time difference delta_time according to the arrival time of the last message in the token bucket parameters;
[0073] The token addition amount determining module 51 is configured to determine the token addition amount delta_token at the time of arrival of the current message according to the time threshold time_threshhold, the time difference delta_time, and the token addition rate in the token bucket parameter;
[0074] The token quantity determining module 52 is configured to determine the current token quantity token_current according to the added quantity delta_token of the token at the time of arrival of the current message and the token quantity after the last message rate limit operation in the token bucket parameter;
[0075] The time difference determining module 50 is specifically configured to subtract the current message arrival time time_now from the last message arrival time time_last to obtain the time difference delta_time, that is, delta_time=time_now-time_last;
[0076] The token addition amount determining module 51 is specifically configured to compare the time threshold time_threshhold and the time difference delta_time. When the time difference delta_time is greater than the time threshold time_threshhold, the time difference delta_time and the token addition rate token_rate are multiplied to obtain the current report. When the time difference delta_time is less than or equal to the time threshold time_threshhold, when the time difference delta_time is less than or equal to the time threshold time_threshhold, the added amount delta_token of the token at the current message arrival time is equal to zero, that is, delta_token=0;
[0077] The time threshold time_threshhold is a preset threshold;
[0078] The token quantity determining module 52 is specifically configured to add the token quantity token_last calculated by the token bucket to the last message rate limit and the token addition quantity delta_token at the time of the current message arrival to obtain the current message. The number of tokens token_current, that is, token_current=token_last+delta_token; and the maximum value of the current number of tokens token_current and the value of the token bucket capacity bucket_size, that is, token_current=
[0079] The message processing module 42 is specifically configured to compare the current number of tokens token_current with the current message length, and when the current number of tokens token_current is greater than or equal to the current message length, forward the current message, and Reduce the current number of tokens in the token bucket token_current by a number equal to the length of the current message; when the current number of tokens token_current is less than the current message length, discard the message and maintain the number of tokens in the token bucket Unchanged
[0080] The aforementioned parameter acquisition module 40, determination module 41, and message processing module 42 can all be implemented by a CPU or a main chip in a network node device; the network node device includes devices such as routers and switches.
[0081] If the token bucket rate limiting method in the embodiment of the present invention is implemented in the form of software and sold or used as an independent product, it can also be stored in a computer readable storage medium. Based on this understanding, those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, this application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, this application may take the form of a computer program product implemented on one or more computer-usable storage media containing computer-usable program codes, the storage media including but not limited to U disk, mobile hard disk, read-only memory ( ROM, Read-Only Memory), disk storage, CD-ROM, optical storage, etc.
[0082] The network node device disclosed in the embodiment of the present invention obtains token bucket parameters according to the attributes of the message, and then obtains the current number of tokens according to the time threshold and the token bucket parameters, and compares the current number of tokens with the current report. The length of the message, and the corresponding processing of the message according to the comparison result, so that the long packet and short packet of the message have equal token service opportunities, improve the service quality of the long message packet, and make up for the existing token bucket rate limiting method defect.
[0083] The above are only the preferred embodiments of the present invention and are not used to limit the scope of protection of the present invention. Any modification, equivalent replacement and improvement made within the spirit and principle of the present invention shall be included in Within the protection scope of the present invention.