Method and device for implementing delay queue based on Redis
By introducing a query delay queue switch in the client, the working status of the work thread is controlled, and the read pressure and delay time problems caused by timed task polling and querying Redis delay queues are solved, and the read and write performance and message processing efficiency of Redis are improved.
Patent Information
- Application Number
- CN202411829015.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-08
AI Technical Summary
In the prior art, timed task polling is used to query Redis delay queues, resulting in frequent queries during low-peak periods of services that increase the read pressure of Redis, reduce the read and write performance of Redis, and may increase the delay time for delay messages to be processed.
By introducing a query delay queue switch in the client, the average time spent on the last N query delay queue and the remaining time of the earliest expired message in the delay queue are calculated in real time, and the working state of the work thread is controlled based on this information, including the execution of tasks and the sleep state, to avoid frequent query of the delay queue.
It effectively avoids frequent query of Redis delay queues during low-peak periods of business, improves Redis' read and write performance, and reduces the delay time for messages to be processed.
Smart Images

Figure CN119782002A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method and device for implementing a delay queue based on Redis. Background Art
[0002] A delay queue is a queue where messages cannot be consumed immediately after being put into the queue, but can only be consumed after a set time. For example, an order is canceled 30 minutes after the payment is made, and a pop-up window prompts the user's call information after the phone rings for 2.5 seconds. Redis is a data middleware with high performance and high availability. Because the data is stored in memory, the data reading speed is very fast. The elements of the zset collection have a sorting function, which can achieve any delay level and millisecond delay, and has the effect of low latency and high throughput.
[0003] In the related technology, a scheduled task can be used to poll the zset collection. However, when there are no delayed messages in the Redis delay queue during the off-peak period of business, it will continue to query in sequence, which greatly increases the read pressure of Redis and reduces the read and write performance of Redis. Moreover, after the scheduled task is executed, if there are still messages that are about to expire in the delay queue, these messages will also need to wait until the next scheduled task is executed to be obtained and processed, which may increase the delay time for the delayed messages to be processed. Summary of the invention
[0004] The embodiments of the present application provide a method and device for implementing a delay queue based on Redis, so as to solve the problem in the related art that the use of scheduled task polling query increases the read pressure of Redis and reduces the read and write performance of Redis.
[0005] In a first aspect, an embodiment of the present application provides a method for implementing a delay queue based on Redis, which is applied to a client, and the method includes: Determine the average time consumed by querying a delay queue within a preset historical time period and the remaining time of the earliest expired message in the delay queue, where the delay queue is a zset set using Redis; Based on the average time consumption and the remaining time, the working state of the working thread of the client is controlled by querying the delay queue switch, and the working state includes task execution and sleep state.
[0006] In a second aspect, an embodiment of the present application provides a device for implementing a delay queue based on Redis, which is applied to a client, and the device includes: A determination module, used to determine the average time consumed in querying a delay queue within a preset historical time length and the remaining time of the earliest expired message in the delay queue, wherein the delay queue is a zset set using Redis; A control module is used to control the working state of the working thread of the client by querying the delay queue switch based on the average time consumption and the remaining time, and the working state includes task execution and sleep state.
[0007] In a third aspect, an embodiment of the present application provides a client, comprising a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method described in the first aspect.
[0008] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method described in the first aspect are implemented.
[0009] In a fifth aspect, an embodiment of the present application provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions, which, when executed by a computer, implement the steps of the method described in the first aspect.
[0010] In an embodiment of the present application, the average time consumed by querying the delay queue within a preset historical time length and the remaining time of the earliest expired message in the delay queue are first determined. The delay queue is a zset collection using Redis. Then, based on the average time consumed and the remaining time, the working state of the working thread is controlled by querying the delay queue switch. The working state includes task execution and sleep state. The embodiment of the present application controls the working state of the working thread by setting the query delay queue switch, which can avoid frequent query of the delay queue during business off-peak periods, improve the read and write performance of Redis, and reduce the delay time for message processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings: Figure 1 It is a flowchart of a method for implementing a delay queue based on Redis provided in an embodiment of the present application; Figure 2 is a flowchart of processing a delayed message received by a client provided in an embodiment of the present application; Figure 3 It is a detailed flow chart of a method for implementing a delay queue based on Redis provided in an embodiment of the present application; Figure 4 It is a schematic diagram of a device for implementing a delay queue based on Redis provided in an embodiment of the present application; Figure 5 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0012] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0013] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here. In addition, the "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally represents that the objects associated with each other are in an "or" relationship.
[0014] The following is combined with Figures 1 to 5 , a method and device for implementing a delay queue based on Redis provided by the embodiment of the present application are described in detail through specific embodiments and their application scenarios. In the embodiment of the present application, a query delay queue switch is introduced to calculate the average time consumed for the most recent N query delay queues in real time, calculate the remaining time of the earliest expired message in the delay queue, and other functions. According to the remaining time and the average time consumed, it is decided whether the thread continues to query the delay queue or the thread sleeps. The sleep time is the remaining time of the earliest expired message in the delay queue or the preset query interval time, which plays the role of querying the delay queue irregularly, can obtain expired messages for processing faster, can effectively reduce the delay time of delayed messages being processed, and can also avoid the problem of frequent queries to the Redis delay queue when there are no delayed messages during the off-peak period of business, thereby improving the read and write performance of Redis, and can also effectively avoid the problem of excessive memory consumption when the client has a large number of delayed messages during the peak period of business.
[0015] The meanings of the terms in this embodiment are as follows: (1) eventTime: the time of the message, which can be the time when the upstream message is generated, or the current time when the message is received, in milliseconds.
[0016] (2) currentTime: The current system time obtained in real time, in milliseconds.
[0017] (3)delayTime: The delay time of the message, in milliseconds.
[0018] (4)ignoreTime: The time range in which the message is exempted from entering the delay queue, in milliseconds. When currentTime-(eventTime+delayTime)<=ignoreTime, the message is directly pushed to the target queue without being placed in the delay queue.
[0019] (5) workSwitch: Queries the delay queue switch, a client memory variable, modified using Java volatile. After a thread modifies the value, other threads can immediately obtain the modified value. 0 means the switch is off, 1 means the switch is on, and the default value is 1.
[0020] (6) Target queue: After the delayed message expires, it is pushed to the target queue for consumption by consumers; this is implemented using Kafka or Redis middleware.
[0021] (7)minScore: The score of the earliest expired message in the delay queue.
[0022] (8)avgQueryCostTime: The average time taken to query the delay queue for the most recent N (configurable) times when the results are not empty, in milliseconds. If minScore-currentTime is less than or equal to avgQueryCostTime, it means that continuing to query the delay queue may obtain expired messages. At this time, the worker thread sleeps for 0 seconds. If the worker thread does not sleep, it will directly execute the process of obtaining delayed messages again, thus reducing the delay time for message processing.
[0023] (9) Worker thread: Polls at irregular intervals. When the workSwitch is turned on, it obtains the elements in the Redis zset whose score is less than or equal to the current timestamp in batches and pushes them to the target queue.
[0024] (10)queryInteval: The preset task query interval, in milliseconds. The execution interval of the worker thread will not be greater than queryInteval. The execution interval is determined by avgQueryCostTime and minScore.
[0025] (11)emptyQueryCount: The number of times the Redis delay queue is queried consecutively and the result is empty.
[0026] (12) emptyQueryCountThreshold: The threshold for the number of times the result of continuous query of Redis delay queue is empty. When emptyQueryCount is greater than or equal to emptyQueryCountThreshold, turn off the workSwitch switch.
[0027] (13) switchCloseWorkCount: The number of consecutive polling times of the worker thread when the workSwitch switch is closed.
[0028] (14) switchCloseWorkCountThreshold: The threshold of the number of continuous polling. When the workSwitch switch is turned off, when switchCloseWorkCount is greater than or equal to switchCloseWorkCountThreshold, it is necessary to determine whether the workSwitch switch needs to be turned on.
[0029] (15) hasDelayDataInput: whether there is a delayed message entering the delay queue, 0: no, 1: yes; client memory variable, modified by Java volatile, after one thread modifies the value, other threads can immediately obtain the modified value. After the message enters the delay queue, the value is changed to 1. Before the working thread queries the delay queue, the value is set to 0. After the query, whether the value is 0 determines whether there is a delayed message entering the delay queue.
[0030] like Figure 1 As shown, it is a flow chart of a method for implementing a delay queue based on Redis provided in an embodiment of the present application. Figure 1 As shown, the method for implementing a delay queue based on Redis is applied to a client, and the method for implementing a delay queue based on Redis may include: the contents shown in S101 to S102.
[0031] In S101, the average time consumed in querying a delay queue within a preset historical time period and the remaining time of the earliest expired message in the delay queue are determined, and the delay queue is a zset set using Redis.
[0032] Among them, the preset historical duration refers to the historical duration of the most recent N query delay queues, where N is a positive integer and can be determined based on actual conditions. This embodiment does not limit this and is subject to actual application.
[0033] It should be noted that in this embodiment, the zset collection of Redis is used as the delay queue, the time when the delayed message expires is used as the score, and the message content is used as the value. A single thread sleep mode is used in a single service instance of the client to execute irregularly, and the messages in the delay queue are obtained in batches and pushed to the target queue. The work after the thread sleeps is the execution of a task. This embodiment describes the case where the delay queue has a zset collection. When the delay queue has multiple zset collections to share the pressure of messages and reading and writing, this application is also applicable. At this time, a query thread can be assigned to each zset collection.
[0034] In S102, based on the average time consumption and the remaining time, the working state of the working thread of the client is controlled by querying the delay queue switch, and the working state includes task execution and sleep state.
[0035] In an embodiment of the present application, the average time consumed by querying the delay queue within a preset historical time length and the remaining time of the earliest expired message in the delay queue are first determined. The delay queue is a zset collection using Redis. Then, based on the average time consumed and the remaining time, the working state of the working thread is controlled by querying the delay queue switch. The working state includes task execution and sleep state. The embodiment of the present application controls the working state of the working thread by setting the query delay queue switch, which can avoid frequent query of the delay queue during business off-peak periods, improve the read and write performance of Redis, and reduce the delay time for message processing.
[0036] In a possible implementation of the present application, based on the average time consumed and the remaining time, the working status of the working thread is controlled by querying the delay queue switch, which may include: when there is the earliest expiring message in the delay queue and the difference between the remaining time and the current time is less than or equal to the average time consumed, turning on the query delay queue switch, the working thread executes the task, and sends the earliest expiring message in the delay queue to the target queue, and the messages in the target queue are used for consumer consumption.
[0037] That is to say, there is a message in the delay queue that is about to expire, and the expiration time of the message is less than the average time consumed by the query delay queue within the preset historical duration. At this time, there is no need to wait for the remaining time. You can turn on the query delay queue switch, and the working thread will directly send the message to the target queue to reduce the delay time for the message to be processed.
[0038] In a possible implementation of the present application, the method for implementing a delay queue based on Redis may also include: when the client receives a delayed message, parsing the basic information of the delayed message, the basic information of the delayed message includes the generation time eventTime of the delayed message, the reception time currentTime of the delayed message received by the client, and the delay time delayTime of the delayed message; based on eventTime, currentTime and delayTime, determining the queue into which the delayed message is placed, the queue includes a target queue and a delay queue; when currentTime-(eventTime+delayTime) is less than or equal to the time range ignoreTime for the message to be exempted from entering the delay queue, the working thread executes the task and sends the delayed message to the target queue; when currentTime-(eventTime+delayTime) is greater than ignoreTime, the delayed message is placed in the delay queue, and the query delay queue switch is turned on.
[0039] In this embodiment, after receiving the delayed message, the client parses eventTime and determines whether currentTime-(eventTime+delayTime) is less than ignoreTime. If it is less than or equal to, it means that the message will expire in a time less than or equal to ignoreTime, and it is directly pushed to the target queue and returned; if it is greater, eventTime+delayTime is calculated as the score of the delayed message and put into the delay queue, hasDelayDataInput=1, and the workSwitch switch of the client is turned on, such as Figure 2 shown.
[0040] In a possible implementation of the present application, the method for implementing a delay queue based on Redis may also include: when the query delay queue switch is turned off, determining the sleep time; when the sleep time is a preset task query interval, obtaining the number of consecutive pollings of the working thread when the query delay queue switch is turned off; based on the number of consecutive pollings, determining whether to turn on the query delay queue switch.
[0041] That is to say, when the query delay queue switch is turned off, it can be determined whether to reopen the query delay queue switch according to the situation.
[0042] In a possible implementation of the present application, determining whether to turn on the query delay queue switch based on the number of consecutive pollings may include: based on the number of consecutive pollings, judging whether the number of consecutive pollings is greater than or equal to a continuous polling number threshold; when the number of consecutive pollings is less than the continuous polling number threshold, the working thread enters a sleep state; when the number of consecutive pollings is greater than or equal to the continuous polling number threshold, setting the number of consecutive pollings of the working thread when the query delay queue switch is turned off to zero, and querying whether there is a message in the delay queue; when there is no message in the delay queue, ending the query and the working thread enters a sleep state.
[0043] In this embodiment, the working thread executes, sleepTime=queryInteval, and it is necessary to determine whether the workSwitch switch is turned on. At this time, it is necessary to obtain switchCloseWorkCount and determine whether switchCloseWorkCount is greater than or equal to switchCloseWorkCountThreshold. If it is less than, the query process ends, and the thread sleeps for sleepTime time and waits for the next task to be executed. If it is greater than or equal to, switchCloseWorkCount is set to 0, and whether there is a message in the delay queue in Redis. If there is no message, it means that the workSwitch switch does not need to be turned on, and the query process ends. The thread sleeps for sleepTime time and waits for the next task to be executed.
[0044] In a possible implementation of the present application, the method for implementing a delay queue based on Redis may also include: when there are messages in the delay queue, turning on the delay queue query switch and setting whether there are delayed messages entering the delay queue to zero; querying the delay queue to obtain the expiration time of the messages in the delay queue; and determining the working status of the working thread based on the expiration time.
[0045] That is to say, if there are messages in the delay queue, it means that other client instances may have put delayed messages in, so the current instance also participates in processing delayed messages. Turn on the workSwitch switch, set hasDelayDataInput to 0, query the delay queue, and use the lua script to obtain elements with a score less than or equal to currentTime in batches, which is the preset task query interval (queryResult). Then remove the obtained elements from the delay queue, query the minimum score of the remaining elements in the delay queue, which is minScore, return queryResult and minScore, and if queryResult is not empty, send it to the target queue, and calculate avgQueryCos according to the query time. tTime; determine whether minScore exists. If so, set emptyQueryCount to 0 and turn on the workSwitch switch. Determine whether minScore-currentTime is less than avgQueryCostTime. If it is less than or equal to, it means that there are messages in the delay queue that are about to expire and there is no need to wait for the next task to be executed. SleepTime=0, then the worker thread executes; if it is greater than, get the smaller value of minScore-currentTime and queryInterval and assign it to sleepTime to ensure that the earliest task in the delay queue can be immediately obtained and processed after it expires, ending the query process. The thread sleeps for sleepTime time and waits for the next task to be executed, as shown below.
[0046] In a possible implementation of the present application, determining the working status of a working thread based on an expiration time may include: when the expiration time is less than or equal to the current time, the working thread executes the task and sends a message whose expiration time is less than or equal to the current time to a target queue.
[0047] In a possible implementation of the present application, the method for implementing a delay queue based on Redis may also include: querying the expiration time of the message with the smallest expiration time among the remaining messages, the remaining messages being the messages in the message queue except those sent to the target queue; and determining the working status of the working thread based on the query results.
[0048] In a possible implementation of the present application, determining the working status of the working thread based on the query result may include: when the query result of the expiration time of the message with the smallest expiration time among the remaining messages is non-empty, setting the number of times the continuous query delay queue result is empty to zero, and turning on the query delay queue switch; determining the difference between the expiration time of the message with the smallest expiration time among the remaining messages and the current time; when the difference is less than or equal to the average time consumption, setting the sleep time to zero, the working thread executes the task, and sends the message with the smallest expiration time among the remaining messages to the target queue; when the difference is greater than the average time consumption, comparing the difference with the preset task query interval time; determining the smaller value between the difference and the preset task query interval time as the sleep time, and setting the working status of the working thread to the sleep state.
[0049] In a possible implementation of the present application, determining the working state of the working thread based on the query result may include: when the query result of the expiration time of the message with the smallest expiration time in the remaining messages is empty, adding one to the number of times the continuous query delay queue result is empty to obtain a new number of times the continuous query delay queue result is empty; based on the number of times the new continuous query delay queue result is empty, judging whether the number of times the new continuous query delay queue result is empty is greater than or equal to the threshold number of times the continuous query delay queue result is empty; when the number of times the new continuous query delay queue result is empty is less than the threshold number of times the continuous query delay queue result is empty, the working thread enters a dormant state; when the number of times the new continuous query delay queue result is empty is greater than or equal to the threshold number of times the continuous query delay queue result is empty, querying whether there is a delayed message entering the delay queue; when a delayed message enters the delay queue, setting the number of times the continuous query delay queue result is empty to zero, turning on the query delay queue switch, and setting the working state of the working thread to a dormant state; when no delayed message enters the delay queue, setting the number of times the continuous query delay queue result is empty to zero, turning off the query delay queue switch, and setting the working state of the working thread to a dormant state.
[0050] That is to say, if the query result of the expiration time of the message with the smallest expiration time among the remaining messages is empty, then emptyQueryCount+1 is added to determine whether emptyQueryCount is greater than or equal to emptyQueryCountThreshold. If it is less than, it means that the workSwitch switch cannot be turned off, the thread sleeps for sleepTime time, ends the query process, and waits for the next execution of the task; if it is greater than or equal to, it means that the workSwitch switch can be turned off, and it is determined whether hasDelayDataInput is equal to 1. If it is equal to 1, it means that a delayed message enters the delay queue during the execution of this task. At this time, the workSwitch switch cannot be turned off. Set emptyQueryCount to 0 and turn on the workSwitch switch to end the query process. The thread sleeps for sleepTime time and waits for the next execution of the task; if hasDelayDataInput is equal to 0, it means that no new delayed message enters the delay queue during the execution of the task. Set emptyQueryCount to 0 and turn off the workSwitch switch to end the query process. The thread sleeps for sleepTime time and waits for the next execution of the task. For details, Figure 3 shown.
[0051] In the related art, querying the Redis delay queue by thread timing polling will increase the delay time of delayed messages being processed. At the millisecond delay level, it may cause the delayed message to expire when it is processed. During the off-peak period of business, when there is no message in the delay queue, Redis will be constantly queried to increase the read and write performance of Redis; saving tasks on the client through channel broadcasting, and querying the Redis delay queue after the task expires, when there are a large number of delayed messages, it may cause the client to have insufficient memory. The embodiment of the present application queries the Redis delay queue by thread irregular polling, introduces a query Redis delay queue switch, can avoid the problem of insufficient memory caused by client storage tasks, and can also avoid the problem of frequent query of the Redis delay queue during the off-peak period of business, when there is no message in the delay queue, thereby reducing the read and write performance of Redis. The execution interval of the thread is determined by the average time spent querying the delay queue and the minimum score in the delay queue, as well as the minimum value of the preset execution interval and (minimum score in the delay queue-current timestamp), which can effectively delay the delay time of delayed messages in the delay queue.
[0052] like Figure 4 As shown in FIG. 1 , a schematic diagram of a device for implementing a delay queue based on Redis provided by the present application is shown. Figure 4As shown, the device for implementing a delay queue based on Redis can be applied to a client, and the device for implementing a delay queue based on Redis can include: a determination module 401 and a control module 402 .
[0053] Among them, the determination module 401 is used to determine the average time consumed in querying the delay queue within a preset historical time length and the remaining time of the earliest expired message in the delay queue. The delay queue is a zset collection using Redis; the control module 402 is used to control the working state of the client's working thread by querying the delay queue switch based on the average time consumed and the remaining time. The working state includes task execution and sleep state.
[0054] In the embodiment of the present application, the determination module 401 first determines the average time consumed by querying the delay queue within the preset historical time length and the remaining time of the earliest expired message in the delay queue. The delay queue is a zset collection using Redis. Then the control module 402 controls the working state of the working thread by querying the delay queue switch based on the average time consumed and the remaining time. The working state includes task execution and sleep state. The embodiment of the present application controls the working state of the working thread by setting the query delay queue switch, which can avoid frequent query of the delay queue during the off-peak period of business, improve the read and write performance of Redis, and reduce the delay time of message processing.
[0055] In a possible implementation of the present application, the control module 402 is used to: when there is the earliest expiring message in the delay queue and the difference between the remaining time and the current time is less than or equal to the average time, turn on the query delay queue switch, and the working thread executes the task to send the earliest expiring message in the delay queue to the target queue, and the messages in the target queue are used for consumer consumption.
[0056] In a possible implementation manner of the present application, the apparatus for implementing a delay queue based on Redis may further include: a parsing module, a second determination module, a third determination module and a fourth determination module.
[0057] Among them, the parsing module is used to parse the basic information of the delayed message when the client receives the delayed message, and the basic information of the delayed message includes the generation time eventTime of the delayed message, the reception time currentTime of the delayed message by the client, and the delay time delayTime of the delayed message; the second determination module is used to determine the queue into which the delayed message is placed based on eventTime, currentTime and delayTime, and the queue includes a target queue and a delay queue; the third determination module is used to execute the task and send the delayed message to the target queue when currentTime-(eventTime+delayTime) is less than or equal to the time range ignoreTime during which the message is exempted from entering the delay queue; the fourth determination module is used to put the delayed message into the delay queue and turn on the query delay queue switch when currentTime-(eventTime+delayTime) is greater than ignoreTime.
[0058] In a possible implementation manner of the present application, the apparatus for implementing a delay queue based on Redis may further include: a fifth determination module, an acquisition module and a sixth determination module.
[0059] Among them, the fifth determination module is used to determine the sleep time when the query delay queue switch is turned off; the acquisition module is used to obtain the number of consecutive pollings of the working thread when the query delay queue switch is turned off when the sleep time is the preset task query interval time; the sixth determination module is used to determine whether to turn on the query delay queue switch based on the number of continuous polling.
[0060] In a possible implementation of the present application, the sixth determination module is used to: based on the number of continuous polling, determine whether the number of continuous polling is greater than or equal to the continuous polling number threshold; when the number of continuous polling is less than the continuous polling number threshold, the working thread enters a sleep state.
[0061] In a possible implementation of the present application, the sixth determination module is used to: when the number of consecutive polling is greater than or equal to a threshold number of consecutive polling, set the number of consecutive pollings of the working thread to zero when the query delay queue switch is turned off, and query whether there is a message in the delay queue; when there is no message in the delay queue, end the query and the working thread enters a sleep state.
[0062] In a possible implementation of the present application, the sixth determination module is used to: when there are messages in the delay queue, turn on the delay queue query switch, and set whether there are delayed messages entering the delay queue to zero; query the delay queue to obtain the expiration time of the messages in the delay queue; and determine the working status of the working thread based on the expiration time.
[0063] In a possible implementation of the present application, the sixth determination module is used to: when the expiration time is less than or equal to the current time, the working thread executes the task and sends the message whose expiration time is less than or equal to the current time to the target queue.
[0064] In a possible implementation of the present application, the sixth determination module is used to: query the expiration time of the message with the smallest expiration time among the remaining messages, the remaining messages being the messages in the message queue except those sent to the target queue; and determine the working status of the working thread based on the query result.
[0065] In a possible implementation of the present application, the sixth determination module is used to: when the query result of the expiration time of the message with the smallest expiration time among the remaining messages is non-empty, set the number of consecutive empty query results of the delay queue to zero, and turn on the query delay queue switch; determine the difference between the expiration time of the message with the smallest expiration time among the remaining messages and the current time; when the difference is less than or equal to the average time, set the sleep time to zero, the working thread executes the task, and sends the message with the smallest expiration time among the remaining messages to the target queue.
[0066] In a possible implementation of the present application, the sixth determination module is used to: when the difference is greater than the average time consumption, compare the difference with the preset task query interval time; determine the smaller value between the difference and the preset task query interval time as the sleep time, and set the working state of the working thread to the sleep state.
[0067] In a possible implementation of the present application, the sixth determination module is used to: when the query result of the expiration time of the message with the smallest expiration time among the remaining messages is empty, add one to the number of times the continuous query delay queue result is empty, and obtain a new number of times the continuous query delay queue result is empty; based on the new number of times the continuous query delay queue result is empty, determine whether the new number of times the continuous query delay queue result is empty is greater than or equal to a threshold number of times the continuous query delay queue result is empty; when the new number of times the continuous query delay queue result is empty is less than the threshold number of times the continuous query delay queue result is empty, the working thread enters a dormant state.
[0068] In a possible implementation of the present application, the sixth determination module is used to: when the number of times the new continuous query delay queue result is empty is greater than or equal to the continuous query delay queue result empty number threshold, query whether there is a delayed message entering the delay queue; when there is a delayed message entering the delay queue, set the number of times the continuous query delay queue result is empty to zero, turn on the query delay queue switch, and set the working state of the working thread to sleep.
[0069] In a possible implementation of the present application, the sixth determination module is used to: when no delayed message enters the delay queue, set the number of times the delay queue is continuously queried to zero, turn off the delay queue query switch, and set the working state of the working thread to a sleep state.
[0070] The device for implementing a delay queue based on Redis provided in the embodiment of the present application can achieve Figures 1 to 3 The various processes implemented in the method embodiment achieve the same technical effect and will not be described again here to avoid repetition.
[0071] like Figure 5 As shown, the embodiment of the present application also provides a client 500, including a processor 501, a memory 502, and a program or instruction stored in the memory 502 and executable on the processor 501. When the program or instruction is executed by the processor 501, each process of the above-mentioned communication processing method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0072] Optionally, the embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by the processor, each process of the above-mentioned method embodiment for implementing a delay queue based on Redis can be implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0073] Optionally, an embodiment of the present application also provides a computer program product, the computer program product includes a computer program stored on a non-transitory computer-readable storage medium, the computer program includes program instructions, and when the program instructions are executed by a computer, the various processes of the method embodiment for implementing a delay queue based on Redis as described above are implemented, and the same technical effect can be achieved. To avoid repetition, they are not repeated here.
[0074] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0075] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0076] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims
1. A method for implementing a delay queue based on Redis, characterized in that: Applied to a client, the method comprises: Determine the average time consumed by querying a delay queue within a preset historical time period and the remaining time of the earliest expired message in the delay queue, where the delay queue is a zset set using Redis; Based on the average time consumption and the remaining time, the working state of the working thread of the client is controlled by querying the delay queue switch, and the working state includes task execution and sleep state.
2. The method according to claim 1, characterized in that The controlling the working state of the working thread by querying a delay queue switch based on the average time consumption and the remaining time includes: When there is the earliest expiring message in the delay queue and the difference between the remaining time and the current time is less than or equal to the average time, the query delay queue switch is turned on, the working thread executes the task, and sends the earliest expiring message in the delay queue to the target queue. The messages in the target queue are used for consumer consumption.
3. The method according to claim 1, characterized in that The method further comprises: When the client receives a delay message, parse basic information of the delay message, where the basic information of the delay message includes eventTime, the time when the delay message is generated, currentTime, the time when the client receives the delay message, and delayTime, the delay time of the delay message; Based on eventTime, currentTime and delayTime, determine the queue into which the delayed message is placed, the queue including a target queue and a delay queue; When currentTime-(eventTime+delayTime) is less than or equal to the time range ignoreTime where the message is exempted from entering the delay queue, the worker thread executes the task and sends the delayed message to the target queue; When currentTime-(eventTime+delayTime) is greater than ignoreTime, the delayed message is put into the delay queue and the query delay queue switch is turned on.
4. The method according to claim 1, characterized in that The method further comprises: When the query delay queue switch is turned off, determining a sleep time; When the sleep time is a preset task query interval, obtaining the number of consecutive pollings of the working thread when the query delay queue switch is turned off; Based on the number of consecutive pollings, it is determined whether to turn on the query delay queue switch.
5. The method according to claim 4, characterized in that The determining whether to turn on the query delay queue switch based on the number of consecutive pollings includes: Based on the number of continuous polling, determining whether the number of continuous polling is greater than or equal to a continuous polling number threshold; When the number of continuous polling is less than the continuous polling number threshold, the working thread enters a dormant state.
6. The method according to claim 5, characterized in that The method further comprises: When the number of continuous polling is greater than or equal to the continuous polling number threshold, the number of continuous polling of the working thread when the query delay queue switch is turned off is set to zero, and query whether there is a message in the delay queue; If there is no message in the delay queue, the query is terminated and the worker thread enters a sleep state.
7. The method according to claim 6, characterized in that The method further comprises: When there is a message in the delay queue, turn on the query delay queue switch, and set whether there is a delayed message entering the delay queue to zero; Query the delay queue to obtain the expiration time of the messages in the delay queue; Based on the expiration time, a working state of the working thread is determined.
8. The method according to claim 7, characterized in that The determining the working state of the working thread based on the expiration time includes: In the case where the expiration time is less than or equal to the current time, the worker thread executes the task and sends the message whose expiration time is less than or equal to the current time to the target queue.
9. The method according to claim 8, characterized in that The method further comprises: Querying the expiration time of the message with the smallest expiration time among the remaining messages, wherein the remaining messages are the messages in the message queue except those sent to the target queue; Based on the query result, the working status of the working thread is determined.
10. The method according to claim 9, characterized in that The determining the working state of the working thread based on the query result includes: When the query result of the expiration time of the message with the smallest expiration time among the remaining messages is non-empty, the number of times the result of continuous query of the delay queue is empty is set to zero, and the query delay queue switch is turned on; Determine the difference between the expiration time of the message with the smallest expiration time among the remaining messages and the current time; When the difference is less than or equal to the average time consumption, the sleep time is set to zero, the worker thread executes the task, and sends the message with the smallest expiration time among the remaining messages to the target queue.
11. The method according to claim 10, characterized in that The method further comprises: When the difference is greater than the average time consumption, the difference is compared with a preset task query interval time; The smaller value between the difference and the preset task query interval is determined as the sleep time, and the working state of the working thread is set to the sleep state.
12. The method according to claim 9, characterized in that The determining the working state of the working thread based on the query result includes: When the query result of the expiration time of the message with the smallest expiration time among the remaining messages is empty, the number of times the continuous query delay queue result is empty is increased by one to obtain a new number of times the continuous query delay queue result is empty; Based on the number of times the new continuous query delay queue result is empty, determining whether the number of times the new continuous query delay queue result is empty is greater than or equal to a continuous query delay queue result empty number threshold; When the number of times the new continuous query delay queue result is empty is less than the threshold number of times the new continuous query delay queue result is empty, the working thread enters a dormant state.
13. The method according to claim 12, characterized in that The method further comprises: When the number of times the new continuous query delay queue result is empty is greater than or equal to the continuous query delay queue result empty number threshold, query whether there is a delayed message entering the delay queue; When a delayed message enters the delay queue, the number of times the delay queue is continuously queried and the result is empty is set to zero, the query delay queue switch is turned on, and the working state of the working thread is set to a dormant state.
14. The method according to claim 13, characterized in that The method further comprises: When no delayed message enters the delay queue, the number of times the delay queue is continuously queried and the result is empty is set to zero, the query delay queue switch is turned off, and the working state of the working thread is set to a dormant state.
15. A device for implementing a delay queue based on Redis, characterized in that: Applied to a client, the device comprises: A determination module, used to determine the average time consumed in querying a delay queue within a preset historical time length and the remaining time of the earliest expired message in the delay queue, wherein the delay queue is a zset set using Redis; A control module is used to control the working state of the working thread of the client by querying the delay queue switch based on the average time consumption and the remaining time, and the working state includes task execution and sleep state.
16. A client, characterized in that: The method comprises a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method according to any one of claims 1 to 14.
17. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 14 are implemented.
18. A computer program product, characterized in that The computer program product comprises a computer program stored on a non-transitory computer-readable storage medium, wherein the computer program comprises program instructions, and when the program instructions are executed by a computer, the steps of the method according to any one of claims 1 to 14 are implemented.
Citation Information
Cited By
Operation and maintenance scheduling system based on urban train operation and maintenance data and positioning data
CN121119790A
An operation and maintenance scheduling system based on urban train operation and maintenance data and positioning data
CN121119790B