Resource access method, device, electronic device and storage medium
By storing the timestamp of the token being extracted in the linked list of the token bucket, the server can accurately determine and extract the available tokens, solving the problem of low certainty of available tokens in the token bucket in the prior art and improving service resources Reliability and efficiency of access.
Patent Information
- Application Number
- CN202010903489.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-01
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2040-09-01
AI Technical Summary
In the prior art, it is impossible to accurately determine whether an available token exists in the token bucket, resulting in low user access reliability of service resources.
By storing the timestamps of any token being extracted in the linked list of the token bucket, the server can determine and extract the available token based on the timestamps of the last token being extracted, improving the accuracy and convenience of determining the available token.
Improves the efficiency and reliability of the server's access to service resources, and avoids failure to obtain tokens and waste of resources caused by the inability to accurately determine whether there are available tokens in the token bucket.
Smart Images

Figure CN113765819B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of Internet technology, and in particular to a resource access method, device, electronic device and storage medium. Background Art
[0002] The gateway is an information exchange platform between networks. The gateway provides various interfaces to the outside world and provides services to users through various interfaces, such as service resource access and other services. However, when the gateway provides service resources to users, network congestion may occur, resulting in the inability to provide users with normal access to service resources.
[0003] In the prior art, a token bucket is mainly used to limit network access to avoid network congestion. For example, a token bucket can generate tokens at a certain rate, and users can obtain service resources provided by the token receiving interface from the token bucket.
[0004] In the process of implementing the present application, the inventors found that there are at least the following problems in the prior art: through the solutions in the above-mentioned prior art, it is impossible to accurately determine whether there are available tokens in the token bucket, resulting in low reliability of user access to service resources. Summary of the invention
[0005] Embodiments of the present application provide a resource access method, device, electronic device, and storage medium to solve the problem of low reliability of user access to service resources.
[0006] On the one hand, an embodiment of the present application provides a resource access method, including:
[0007] According to the distributed lock obtained based on the access request for accessing the service resource, a timestamp of when the last token was extracted is obtained from the data of the token bucket, wherein the token bucket includes a linked list, and the linked list stores the timestamp of when any token is extracted;
[0008] If it is determined that there are available tokens in the token bucket according to the timestamp of the last token extraction, extract the available tokens;
[0009] The service resource is accessed according to the available token.
[0010] In the embodiment of the present application, since the token bucket includes a linked list and the linked list stores the timestamp of when any token is extracted, the available token can be determined and extracted from the token bucket according to the timestamp of when the last token was extracted.
[0011] It is worth noting that by extracting the timestamp of the last token extracted from the linked list stored in the token bucket data, and determining and extracting the available token based on the timestamp, the accuracy and convenience of determining the available tokens can be improved, thereby improving the technical effect of improving the efficiency of subsequent access to service resources.
[0012] That is to say, the embodiment of the present application uses a linked list to store the timestamp of any token extraction, so that the server can easily and quickly obtain the timestamp of the last token extraction from the data of the token bucket, and determine to extract an available token from the token bucket based on the timestamp of the last token extraction. This can avoid the drawbacks of the server in the related technology that fails to obtain tokens due to the inability to accurately determine whether there are available tokens in the token bucket, thereby wasting server resources, etc., thereby achieving accuracy and reliability in token extraction, and further achieving the technical effect of improving the efficiency and reliability of the server's access to service resources.
[0013] In some embodiments, the method further comprises:
[0014] Determining the length of the linked list;
[0015] And, if it is determined according to the timestamp of the last token extraction that there is an available token in the token bucket, then extracting the available token includes: if the length of the linked list is less than the preset current limit number, and if it is determined according to the timestamp of the last token extraction that there is an available token in the token bucket, then extracting the available token.
[0016] In an embodiment of the present application, before determining and extracting the available tokens, the length of the linked list is first compared with the current limiting number. If the length of the linked list is less than the current limiting number, it means that access can be continued during this round of service resource access, and then the determination and extraction of the available tokens can be executed. By prioritizing the comparison of the length of the linked list with the current limiting number, the technical effect of the reliability and accuracy of extracting the available tokens can be improved.
[0017] In some embodiments, the method further comprises:
[0018] Determining the length of the linked list;
[0019] If the length of the linked list is greater than or equal to the preset current limiting times, obtaining the timestamp stored in the header of the linked list;
[0020] And, if it is determined that there is an available token in the token bucket based on the timestamp of the last token extraction, then extracting the available token includes: if the difference between the current time and the timestamp stored in the header is greater than the preset current limiting time, and if it is determined that there is an available token in the token bucket based on the timestamp of the last token extraction, then extracting the available token.
[0021] In an embodiment of the present application, if the length of the linked list is greater than or equal to the number of current limiting times, it means that access to service resources in this round may have been restricted, that is, access to service resources has reached the upper limit of this round. In order to further determine whether it has been restricted, the timestamp stored in the header can be obtained. If the difference between the current time and the timestamp stored in the header is greater than the current limiting time, it means that the next round of service resource access has been enabled, and then the available tokens can be determined and extracted, which can achieve the technical effect of high reliability and accuracy of extracting available tokens.
[0022] In some embodiments, the method further comprises:
[0023] The timestamp when the current token is extracted is written into the linked list and serves as the current tail of the linked list.
[0024] In an embodiment of the present application, by writing the timestamp of the current token being extracted into a linked list, the technical effect of improving the accuracy and reliability of subsequent determination and extraction of available tokens can be improved.
[0025] In some embodiments, the method further comprises:
[0026] Determine the expiration time of the timestamp when the current token is extracted;
[0027] Furthermore, writing the timestamp when the current token is extracted into the linked list includes: writing the timestamp when the current token is extracted and the expiration time of the timestamp when the current token is extracted into the linked list.
[0028] In an embodiment of the present application, by writing the timestamp when the current token is extracted and the expiration time of the timestamp when the current token is extracted into a linked list, the data in the linked list can be deleted smoothly to release the storage space of the linked list.
[0029] In some embodiments, determining the expiration time of the timestamp when the current token is extracted comprises:
[0030] Determine whether the timestamp when the current token is extracted is the timestamp when it is first written into the linked list;
[0031] If the timestamp when the current token is extracted is the timestamp that is first written into the linked list, the first time is determined as the expiration time of the timestamp when the current token is extracted.
[0032] In an embodiment of the present application, if the timestamp when the current token is extracted is the timestamp when it is first written into the linked list, it means that this round of service resource access has just been started. Therefore, the first time can be used as the expiration time of the timestamp when the current token is extracted, and the first time can be the current limiting time, thereby achieving the technical effect of determining the reliability and accuracy of the expiration time of the timestamp when the current token is extracted.
[0033] In some embodiments, if the timestamp of when the current token is extracted is not the timestamp of when it is first written into the linked list, the method includes:
[0034] Obtain the length from the head of the linked list to the end of the current link;
[0035] The expiration time of the timestamp when the current token is extracted is determined according to the length from the table header to the current chain tail.
[0036] In an embodiment of the present application, if the timestamp when the current token is extracted is not the timestamp when it was first written into the linked list, the expiration time of the timestamp when the current token is extracted can be determined in a manner different from the above example, thereby achieving the technical effect of flexibility and diversity in determining the expiration time of the timestamp when the current token is extracted.
[0037] In some embodiments, determining the expiration time of the timestamp from which the current token is extracted according to the length from the header to the current chain tail includes:
[0038] If the length from the table header to the current chain tail is less than or equal to the preset current limiting times, the second time is determined as the expiration time of the timestamp when the current token is extracted.
[0039] In an embodiment of the present application, if the length from the table header to the current chain end is less than or equal to the number of current limiting times, it means that the current round of service resource access has been started and has not yet ended, then the second time can be determined as the expiration time of the timestamp when the current token is extracted, and the second time can be half of the current limiting time. Of course, it can also be other times that are greater than, less than or equal to the current limiting time, so as to improve the access efficiency to service resources.
[0040] In some embodiments, determining the expiration time of the timestamp from which the current token is extracted according to the length from the header to the current chain tail includes:
[0041] If the length from the table header to the current chain end is greater than the preset current limiting times, obtaining the timestamp stored in the table header;
[0042] If the difference between the current time and the timestamp stored in the header is greater than the preset current limiting time, the second time is determined as the expiration time of the timestamp when the current secondary token is extracted.
[0043] In an embodiment of the present application, if the length from the header to the current chain end is greater than the current limiting number, the difference between the current time and the timestamp stored in the header can be calculated, and if the difference is greater than the current limiting time, it means that the next round of service resource access has been started, and the second time can be determined as the expiration time of the timestamp when the current token is extracted, so as to improve the access efficiency to service resources.
[0044] In some embodiments, if it is determined that there are available tokens in the token bucket according to the timestamp of the last token extraction, extracting the available tokens includes:
[0045] If the difference between the current time and the timestamp of the last token extraction is greater than or equal to a preset time difference, the available token is determined and extracted, wherein the time difference is determined based on the time when the token bucket generates a token.
[0046] In an embodiment of the present application, the difference between the current time and the timestamp of the last time a token was extracted can be calculated. When the difference is greater than or equal to the time difference, it means that an available token has been generated in the token bucket, and the available token can be extracted from the token bucket, thereby improving the technical effect of the accuracy and reliability of determining and extracting available tokens.
[0047] On the other hand, an embodiment of the present application further provides a resource access device, the device comprising:
[0048] An acquisition module, configured to acquire a timestamp of a last token extraction from data in a token bucket according to a distributed lock obtained based on an access request for accessing a service resource, wherein the token bucket includes a linked list, and the linked list stores a timestamp of an extraction of any token;
[0049] An extraction module, configured to extract an available token if it is determined that there is an available token in the token bucket according to the timestamp of the last token extraction;
[0050] An access module is used to access the service resource according to the available token.
[0051] In some embodiments, the apparatus further comprises:
[0052] A first determining module, used to determine the length of the linked list;
[0053] Furthermore, the extraction module is used to extract the available token if the length of the linked list is less than a preset current limiting number and if it is determined based on the timestamp of the last token extraction that there is an available token in the token bucket.
[0054] In some embodiments, the apparatus further comprises:
[0055] A first determining module, used to determine the length of the linked list;
[0056] The acquisition module is used to acquire the timestamp stored in the header of the linked list if the length of the linked list is greater than or equal to the preset current limiting times;
[0057] And, the extraction module is used to extract the available token if the difference between the current time and the timestamp stored in the header is greater than the preset current limiting time, and if it is determined based on the timestamp of the last token extraction that there is an available token in the token bucket.
[0058] In some embodiments, the apparatus further comprises:
[0059] The writing module is used to write the timestamp when the current token is extracted into the linked list and use it as the current tail of the linked list.
[0060] In some embodiments, the apparatus further comprises:
[0061] A second determination module, configured to determine an expiration time of a timestamp when the current secondary token is extracted;
[0062] The writing module is used to write the timestamp when the current token is extracted and the expiration time of the timestamp when the current token is extracted into the linked list.
[0063] In some embodiments, the second determination module is used to determine whether the timestamp when the current token is extracted is the timestamp when it is first written into the linked list. If the timestamp when the current token is extracted is the timestamp when it is first written into the linked list, the first time is determined as the expiration time of the timestamp when the current token is extracted.
[0064] In some embodiments, if the timestamp when the current token is extracted is not the timestamp when it was first written into the linked list, the second determination module is used to obtain the length from the head of the linked list to the current end of the chain, and determine the expiration time of the timestamp when the current token is extracted based on the length from the head to the current end of the chain.
[0065] In some embodiments, the second determination module is used to determine the second time as the expiration time of the timestamp when the current token is extracted if the length from the table header to the current chain tail is less than or equal to a preset current limiting number of times.
[0066] In some embodiments, the second determination module is used to obtain the timestamp stored in the header if the length from the header to the current chain end is greater than a preset current limiting number, and if the difference between the current time and the timestamp stored in the header is greater than a preset current limiting time, determine the second time as the expiration time of the timestamp when the current token is extracted.
[0067] In some embodiments, the extraction module is used to determine and extract the available token if the difference between the current time and the timestamp of the last token extraction is greater than or equal to a preset time difference, wherein the time difference is determined based on the time when the token bucket generates a token.
[0068] On the other hand, an embodiment of the present application further provides an electronic device, including: a memory, a processor;
[0069] Memory; Memory for storing instructions executable by the processor;
[0070] The processor is configured to execute the method described in any of the above embodiments.
[0071] On the other hand, an embodiment of the present application further provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, they are used to implement the method described in any of the above embodiments.
[0072] A resource access method, device, electronic device and storage medium provided by an embodiment of the present application include: according to a distributed lock obtained based on an access request for accessing a service resource, obtaining a timestamp of the last token extraction from data in a token bucket, the token bucket including a linked list, the linked list storing a timestamp of any token extraction, if it is determined that there is an available token in the token bucket according to the timestamp of the last token extraction, then extracting the available token, and accessing the service resource according to the available token, by using a linked list to store the timestamp of any token extraction, so that the server can quickly and easily obtain the timestamp of the last token extraction from the data in the token bucket, and determine to extract an available token from the token bucket according to the timestamp of the last token extraction, thereby avoiding the drawbacks of the server in the related art such as failure to obtain tokens due to the inability to accurately determine whether there is an available token in the token bucket, wasting server resources, etc., thereby achieving accuracy and reliability in token extraction, and further achieving the technical effect of improving the efficiency and reliability of the server's access to service resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0073] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0074] Figure 1 A schematic diagram of an application scenario of the resource access method of an embodiment of the present application;
[0075] Figure 2 A schematic diagram of a resource access method according to an embodiment of the present application;
[0076] Figure 3 A schematic diagram of a resource access method flow chart of another embodiment of the present application;
[0077] Figure 4 A schematic diagram of a flow chart of a resource access method in another embodiment of the present application;
[0078] Figure 5 A schematic diagram of a flow chart of a resource access method in another embodiment of the present application;
[0079] Figure 6 A schematic diagram of a resource access device according to an embodiment of the present application;
[0080] Figure 7 A schematic diagram of a resource access device according to another embodiment of the present application;
[0081] Figure 8 A schematic diagram of an electronic device according to an embodiment of the present application.
[0082] The above drawings show clear embodiments of the present disclosure, which will be described in more detail below. These drawings and text descriptions are not intended to limit the scope of the present disclosure in any way, but to illustrate the concepts of the present disclosure to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0083] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0084] See also Figure 1 , Figure 1 A schematic diagram of an application scenario of the resource access method according to an embodiment of the present application.
[0085] like Figure 1 As shown, the application scenarios of the resource access method may include gateways and distributed clusters.
[0086] A gateway can also be called a network connector or a protocol converter. A gateway is a network interconnection device that implements network interconnection at the transport layer. For example, a gateway can Figure 1 Multiple servers in a distributed cluster ( Figure 1 3 servers are shown as an example in the figure) connected to a network device (not shown in the figure).
[0087] Distributed clusters can include Figure 1 The multiple servers shown in the figure, of course, the distributed cluster can also include multiple terminal devices (not shown in the figure).
[0088] Among them, the terminal equipment can be a mobile terminal, such as a mobile phone (or "cellular" phone) and a computer with a mobile terminal, for example, it can be a portable, pocket-sized, handheld, computer-built-in or vehicle-mounted mobile device, which exchanges language and / or data with the wireless access network; the terminal equipment can also be a personal communication service (Personal Communication Service, PCS) phone, a cordless phone, a session initiation protocol (Session Initiation Protocol, SIP) phone, a wireless local loop (Wireless Local Loop, WLL) station, a personal digital assistant (Personal Digital Assistant, PDA), a tablet computer, a wireless modem (modem), a handheld device (handset), a laptop computer (laptop computer), a machine type communication (Machine Type Communication, MTC) terminal and other equipment; the terminal equipment can also be called a system, a subscriber unit (Subscriber Unit), a subscriber station (Subscriber Station), a mobile station (Mobile Station), a mobile station (Mobile), a remote station (Remote Station), a remote terminal (Remote Terminal), an access terminal (Access Terminal), a user terminal (User Terminal), a user agent (User Agent), User Device or User Equipment, etc., are not limited here.
[0089] It is worth noting that the application scenarios of the resource access method of the embodiment of the present application can be applicable to different network standards, for example, Narrow Band-Internet of Things system (NB-IoT), Global System for Mobile Communications (GSM), Enhanced Data rate for GSM Evolution (EDGE), Wideband Code Division Multiple Access system (WCDMA), Code Division Multiple Access system (CDMA2000), Time Division-Synchronization Code Division Multiple Access system (TD-SCDMA), Long Term Evolution system (LTE), Bluetooth system, WiFi system, and the three major application scenarios of 5G mobile communication system, eMBB, URLLC and eMTC, etc.
[0090] It should be understood that the above examples are only used to illustrate the application scenarios in which the resource access method may be applied, and the elements that may be included in the application scenarios (such as servers and gateways), and cannot be understood as a limitation on the application scenarios of the resource access method, nor can it be understood as a limitation on the elements of the application scenarios of the resource access method.
[0091] For example, elements can be reduced accordingly in the application scenario, such as reducing some servers.
[0092] For another example, elements may be added to the application scenario accordingly, such as adding servers and network devices.
[0093] And when the element added in the application scenario is a network device, the network device can be a base station, and the base station can be a base station (Base Transceiver Station, BTS) and / or a base station controller in GSM or CDMA, or a base station (NodeB, NB) and / or a radio network controller (Radio Network Controller, RNC) in WCDMA, or an evolved base station (Evolutional Node B, eNB or eNodeB) in LTE, or a relay station or access point, or a base station (gNB) in a 5G network, satellite, device-to-device (D2D) communication, vehicle-to-X (V2X) communication, machine (Machine-to-Machine, M2M) communication, and network devices that perform base station functions in various possible communications in the future, etc., and the present application is not limited here.
[0094] In such Figure 1 In the application scenario shown, each server can be connected to the gateway, and the gateway can provide various interfaces to the outside, that is, the gateway can provide various interfaces for each server and provide different services to each server through various interfaces. For example, the gateway provides each server with access to service resources through a certain interface (hereinafter referred to as interface A), that is, each server can access the service resources through interface A of the gateway.
[0095] Generally speaking, the access frequency of interface A is subject to certain restrictions, such as interface A can provide 5,000 accesses within one second. When the number of accesses to interface A within one second is greater than 5,000 times, network congestion may occur, resulting in the server being unable to access interface A normally. For example, when the number of accesses to interface A within one second is greater than 5,000 times, accesses exceeding 5,000 times may be invalid accesses, and information such as access data initiated by the server may be lost.
[0096] For another example, the server may be a server supporting multiple threads, and the server may establish communication with each terminal device, receive access requests to service resources initiated by each terminal device, and access the service resources to interface A according to each thread.
[0097] In the related art, the commonly used method is the token bucket current limiting method, which generates tokens through the token bucket algorithm, and receives and accesses the service resources provided by interface A based on the generated tokens. For example, if a thread in the server obtains a token after obtaining a distributed lock, the thread can receive and access the service resources provided by interface A based on the obtained token.
[0098] However, through the token bucket current limiting method in the above-mentioned related technology, the server may be unable to accurately determine whether there are available tokens in the token bucket, resulting in low reliability of each thread's access to service resources.
[0099] The inventor of the present application obtained the inventive concept of the present application after creative work: the timestamp of each time a token is extracted is recorded through a linked list in a token bucket, so that the server can determine whether there is a token in the token bucket based on the timestamp.
[0100] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0101] According to one aspect of an embodiment of the present application, an embodiment of the present application provides a resource access method, which can be applied to Figure 1 The application scenario shown.
[0102] See also Figure 2 , Figure 2 A flowchart of a resource access method according to an embodiment of the present application.
[0103] like Figure 2 As shown, the method includes:
[0104] S101: According to the distributed lock obtained based on the access request for accessing the service resource, the timestamp of the last token extraction is obtained from the data of the token bucket, and the token bucket includes a linked list, and the linked list stores the timestamp of any token extraction.
[0105] The execution subject of the resource access method of the embodiment of the present application may be a resource access device, and the resource access device may be as follows: Figure 1 The server shown in , of course, the resource access device can also be a processor or chip that can implement the resource access method of the embodiment of the present application, and the embodiment of the present application is not limited.
[0106] In the embodiment of the present application, an example in which the execution subject is a server is used for exemplary description.
[0107] Among them, distributed locks are a way to control the synchronous access to shared resources between distributed clusters. In a distributed model, there is only one copy of the data (or there are restrictions), and lock technology is needed to control the number of processes that modify the data at a certain time.
[0108] In an embodiment of the present application, a distributed lock can be used to represent a way to ensure consistency by preventing interference between threads of access requests sent by multiple servers at the same time when accessing one or a group of resources. Among them, a thread is the smallest unit that the operating system can perform computational scheduling. A thread refers to a single sequential control flow in a process. Multiple threads can run in parallel in a process, and each thread executes different tasks in parallel.
[0109] Among them, the token bucket is a flow control technology. Generally speaking, the working process of the token bucket is as follows: 1. Tokens are put into the bucket at a certain rate; 2. Each token allows the source to send a certain number of bits; 3. When a packet is sent, the traffic regulator will delete the number of tokens equal to the packet size from the bucket; 4. If there are not enough tokens to send a packet, the packet will wait until there are enough tokens or the packet is discarded; 5. The bucket has a specific capacity. If the bucket is full, the newly added token will be discarded.
[0110] That is to say, the token bucket can generate tokens continuously at a constant rate. If the token is not consumed, or the rate of consumption is lower than the rate of generation, the number of tokens will continue to increase until the bucket is full. The tokens generated later will overflow from the bucket. Finally, the maximum number of tokens retained in the bucket will never exceed the size of the bucket. Data packets that obtain tokens are allowed to perform subsequent operations. When the rate of token consumption is higher than the rate of token generation, data packets that have not obtained tokens will be discarded or enter the cache queue.
[0111] The linked list may be used to represent a storage structure for storing data and having certain storage domains. Each storage domain is used to store data, and the data stored in the storage domain may be cleared.
[0112] For example, in an embodiment of the present application, a linked list can be used to store the timestamp of any token being extracted, that is, the storage domain in the linked list can store the timestamp of any token being extracted, and when the timestamp of the token being extracted is stored in all storage domains, the server can clear the timestamps stored in each storage domain.
[0113] In this step, each server needs to obtain a distributed lock before accessing the service resources to ensure that only one server can modify the relevant information of the shared resources (ie, the service resources accessed by each server) at a certain moment.
[0114] In the embodiments of the present application, there is no limitation on the way in which the server acquires the distributed lock. For example, the server can be implemented based on Redis cache, Redlock, ZooKeeper, Consul, and so on.
[0115] In this step, after the server obtains the distributed lock, since the token bucket includes a linked list and the linked list stores the timestamp of any token being extracted, the server can obtain the timestamp of the last token being extracted from the data in the token bucket.
[0116] S102: If it is determined that there are available tokens in the token bucket according to the timestamp of the last token extraction, the available tokens are extracted.
[0117] The available tokens may be used to indicate that there are tokens in the token bucket that can be extracted by the server.
[0118] This step may specifically include: judging whether there is an available token in the token bucket according to the timestamp of the last token extraction, and if so (ie, there is an available token in the token bucket), extracting the available token from the token bucket.
[0119] Among them, the embodiments of the present application do not limit how to determine whether there are available tokens in the token bucket. For example, it can be to perform time calculation based on the timestamp of the last token extraction to determine whether there are available tokens in the token bucket; or it can be to pre-set a timestamp comparison table and determine whether there are available tokens in the token bucket through the timestamp comparison table and the timestamp of the last token extraction, and so on.
[0120] Based on the above analysis, it can be seen that the token bucket can generate tokens based on a certain rate, and after the server obtains the distributed lock, it can obtain tokens from the token bucket to access service resources based on the tokens. When the rate at which the token bucket generates tokens is lower than the rate at which the server extracts tokens, there may be no tokens in the token bucket for the server to obtain.
[0121] In the embodiment of the present application, since the token bucket includes a linked list and the linked list stores the timestamp of any token being extracted, the server can determine and extract an available token from the token bucket based on the timestamp of the last token being extracted.
[0122] It is worth noting that by extracting the timestamp of the last token extracted from the linked list stored in the token bucket data, and determining and extracting the available token based on the timestamp, the accuracy and convenience of determining the available tokens can be improved, thereby improving the efficiency of subsequent servers accessing service resources.
[0123] S103: Access service resources according to the available token.
[0124] In this step, when the server obtains the available token, it can access the service resources based on the available token.
[0125] Based on the above analysis, it can be seen that an embodiment of the present application provides a resource access method, which includes: according to a distributed lock obtained based on an access request for access to a service resource, obtaining the timestamp of the last token extraction from the data of a token bucket, the token bucket includes a linked list, and the timestamp of any token extraction is stored in the linked list. If it is determined that there is an available token in the token bucket according to the timestamp of the last token extraction, the available token is extracted, and the service resource is accessed according to the available token. By using a linked list to store the timestamp of any token extraction, the server can easily and quickly obtain the timestamp of the last token extraction from the data of the token bucket, and determine to extract an available token from the token bucket according to the timestamp of the last token extraction. This can avoid the drawbacks of the server in the related technology that fails to obtain tokens due to the inability to accurately determine whether there are available tokens in the token bucket, wastes server resources, etc., thereby achieving accuracy and reliability in token extraction, and further achieving the technical effect of improving the efficiency and reliability of the server's access to service resources.
[0126] To help readers more clearly understand how the server determines whether there are available tokens in the token bucket, Figure 3 The resource access of the embodiment of the present application is described in more detail. Figure 3 This is a schematic diagram of a resource access method flow according to another embodiment of the present application.
[0127] like Figure 3 As shown, the method includes:
[0128] S201: According to the distributed lock obtained based on the access request for accessing the service resource, the timestamp of the last token extraction is obtained from the data of the token bucket, and the token bucket includes a linked list, and the linked list stores the timestamp of any token extraction.
[0129] Among them, the description of S201 can be found in S101 and will not be repeated here.
[0130] S202: If the time difference between the current time and the timestamp of the last token extraction is greater than or equal to a preset time difference, then determine and extract an available token.
[0131] The time difference can be set by the server based on demand, historical records, and experiments.
[0132] Among them, this step may include: the server calculates the difference between the current time and the timestamp of the last token extraction, and determines whether the calculated difference is greater than or equal to the time difference. If so (that is, the difference is greater than or equal to the time difference), it is determined that there are available tokens in the token bucket.
[0133] In some embodiments, the calculated difference may also be smaller than the time difference. When the server determines that the calculated difference is smaller than the time difference, it can be determined that there are no available tokens in the token bucket, and the server can enter a sleep state. The time the server is in a sleep state (hereinafter referred to as the sleep time) can be set based on a pre-set current limiting number. For example, if interface A can provide 1,000 accesses to service resources within 1 second, the server can set the sleep time to 5-10 milliseconds.
[0134] In an embodiment of the present application, the server determines whether there are available tokens in the token bucket by comparing two times (i.e., the current time and the timestamp when the token was last extracted), thereby achieving efficient and convenient determination of available tokens, thereby facilitating subsequent server access to service resources.
[0135] S203: Access the service resource according to the available token.
[0136] The description of S203 can be found in S103 and will not be repeated here.
[0137] See also Figure 4 , Figure 4 It is a flowchart of a resource access method in another embodiment of the present application.
[0138] like Figure 4 As shown, the method includes:
[0139] S301: According to the distributed lock obtained based on the access request for accessing the service resource, the timestamp of the last token extraction is obtained from the data of the token bucket, and the token bucket includes a linked list, and the linked list stores the timestamp of any token extraction.
[0140] Among them, the description of S301 can be found in 101 and will not be repeated here.
[0141] S302: If it is determined that there are available tokens in the token bucket according to the timestamp of the last token extraction, the available tokens are extracted.
[0142] The description of S302 may refer to S102 or S202, and will not be repeated here.
[0143] S303: Write the timestamp when the current token is extracted into the linked list and use it as the current end of the linked list.
[0144] Similarly, the timestamp when the current token is extracted is a relative concept, which can be used to represent the timestamp when the server extracts the token from the token bucket this time.
[0145] The current chain tail is also a relative concept, which can be used to represent the storage domain in the linked list where the timestamp of when the token was extracted is stored. In the embodiment of the present application, the current chain tail can be understood as the storage domain in the linked list where the timestamp of when the current token was extracted is stored.
[0146] That is to say, in an embodiment of the present application, when the server extracts a token from the token bucket, it can determine the time when it extracts the token, and write the determined timestamp of the token extraction into the storage domain in the linked list, and the storage domain that stores the timestamp of the current token being extracted can be the current end of the linked list.
[0147] S304: Access service resources according to the available token.
[0148] The description of S304 can be found in S103 and will not be repeated here.
[0149] It is worth noting that in the embodiments of the present application, there is no limitation on the execution order between S303 and S304. That is to say, S303 may be executed first and then S304 as described in the above example; S304 may be executed first and then S303; or S303 and S304 may be executed simultaneously.
[0150] See also Figure 5 , Figure 5 It is a flowchart of a resource access method in another embodiment of the present application.
[0151] like Figure 5 As shown, the method includes:
[0152] S401: According to the distributed lock obtained based on the access request for accessing the service resource, the timestamp of the last token extraction is obtained from the data of the token bucket, and the token bucket includes a linked list, and the linked list stores the timestamp of any token extraction.
[0153] Among them, the description of S401 can be found in 101 and will not be repeated here.
[0154] S402: If it is determined that there are available tokens in the token bucket according to the timestamp of the last token extraction, the available tokens are extracted.
[0155] The description of S402 may refer to S102 or S202, and will not be repeated here.
[0156] S403: Determine the expiration time of the timestamp when the current token is extracted.
[0157] In some embodiments, S403 may include:
[0158] S4031: Determine whether the timestamp when the current token is extracted is the timestamp when it is first written into the linked list. If so (i.e. the timestamp when the current token is extracted is the timestamp when it is first written into the linked list), execute S4032; if not (i.e. the timestamp when the current token is extracted is not the timestamp when it is first written into the linked list), execute S4033.
[0159] S4032: Determine the first time as the expiration time of the timestamp when the current token is extracted.
[0160] In some embodiments, the current limiting time may be determined as the first time, that is, the current limiting time may be determined as the expiration time of the timestamp when the current token is extracted.
[0161] It is worth noting that if the timestamp when the current token is extracted is the timestamp when it was first written into the linked list, it means that the server's access to the service resources based on the current token is the first access, which means that the flow limiting time starts. Therefore, the first time can be set as the flow limiting time, and the first time can be determined as the expiration time of the timestamp when the current token is extracted.
[0162] S4033: Obtain the length from the head of the linked list to the current end of the linked list.
[0163] The header can be used to represent the first storage domain of the linked list.
[0164] The total length of the linked list can be used to characterize the maximum number of current limiting times. Therefore, the length from the table header to the current chain end in the embodiment of the present application can be used to characterize the number of current limiting times recorded from the table header to the chain end.
[0165] S4034: Determine the expiration time of the timestamp when the current token is extracted based on the length from the header to the current end of the chain.
[0166] In some embodiments, S4034 may include:
[0167] S40341: Determine whether the length from the table header to the current chain end is less than or equal to the current limiting number. If so (i.e. the length from the table header to the current chain end is less than or equal to the current limiting number), execute S4342; if not (i.e. the length from the table header to the current chain end is greater than the current limiting number), execute S40343.
[0168] S40342: Determine the second time as the expiration time of the timestamp when the current token is extracted.
[0169] The first time may be the same as or different from the second time, and there is no necessary relationship between the first time and the second time.
[0170] In some embodiments, half of the current limiting time may be determined as the expiration time of the timestamp when the current token is extracted.
[0171] S40343: Obtain the timestamp stored in the header.
[0172] The timestamp stored in the header can be used to represent the timestamp when a token stored in the header is extracted.
[0173] S40344: Calculate the difference between the current time and the timestamp stored in the header.
[0174] S40345: Determine whether the difference calculated in S40344 is greater than the current limiting time. If so, execute S40346; if not, the acquisition of the available token fails and the process ends.
[0175] S40346: Determine the second time as the expiration time of the timestamp when the current token is extracted.
[0176] S404: Write the timestamp when the current token is extracted and the expiration time of the timestamp when the current token is extracted into the linked list.
[0177] That is to say, in an embodiment of the present application, after the server determines the expiration time of the timestamp of the current token extraction based on the above method, the server can identify the timestamp of the current token extraction, and the identification can be understood as the process of storing the expiration time and the timestamp of the current token extraction in the same storage domain, that is, the server stores the timestamp of the current token extraction and the timestamp corresponding to the timestamp of the current token extraction in the same storage domain of the linked list.
[0178] S405: Access service resources according to the available token.
[0179] The description of S405 can be found in S103 and will not be repeated here.
[0180] According to another aspect of the embodiment of the present application, the embodiment of the present application further provides a resource access device for executing the resource access method described in any of the above embodiments, such as executing Figure 2 , Figure 3 , Figure 4 and Figure 5 The resource access method shown in any of the embodiments.
[0181] See also Figure 6 , Figure 6 A schematic diagram of a resource access device according to an embodiment of the present application.
[0182] like Figure 6 As shown, the device comprises:
[0183] An acquisition module 11 is used to obtain a timestamp of the last token extraction from data in a token bucket according to a distributed lock obtained based on an access request for accessing a service resource, wherein the token bucket includes a linked list, and the linked list stores a timestamp of the extraction of any token;
[0184] An extraction module 12, configured to extract an available token if it is determined that there is an available token in the token bucket according to the timestamp of the last token extraction;
[0185] The access module 13 is used to access the service resource according to the available token.
[0186] Combination Figure 7 It can be seen that, in some embodiments, the device further includes:
[0187] A first determining module 14, used to determine the length of the linked list;
[0188] Furthermore, the extraction module 12 is configured to extract the available token if the length of the linked list is less than a preset current limiting number and if it is determined based on the timestamp of the last token extraction that there is an available token in the token bucket.
[0189] Combination Figure 7 It can be seen that, in some embodiments, the device further includes:
[0190] A first determining module 14, used to determine the length of the linked list;
[0191] The acquisition module 11 is used to acquire the timestamp stored in the header of the linked list if the length of the linked list is greater than or equal to the preset current limiting times;
[0192] Furthermore, the extraction module 12 is used to extract the available token if the difference between the current time and the timestamp stored in the header is greater than the preset current limiting time, and if it is determined based on the timestamp of the last token extraction that there is an available token in the token bucket.
[0193] Combination Figure 7 It can be seen that, in some embodiments, the device further includes:
[0194] The writing module 15 is used to write the timestamp when the current token is extracted into the linked list and use it as the current tail of the linked list.
[0195] Combination Figure 7 It can be seen that, in some embodiments, the device further includes:
[0196] A second determination module 16, configured to determine an expiration time of a timestamp when the current secondary token is extracted;
[0197] The writing module 15 is used to write the timestamp when the current token is extracted and the expiration time of the timestamp when the current token is extracted into the linked list.
[0198] In some embodiments, the second determination module 16 is used to determine whether the timestamp when the current token is extracted is the timestamp when it is first written into the linked list. If the timestamp when the current token is extracted is the timestamp when it is first written into the linked list, the first time is determined as the expiration time of the timestamp when the current token is extracted.
[0199] In some embodiments, if the timestamp when the current token is extracted is not the timestamp when it was first written into the linked list, the second determination module 16 is used to obtain the length from the head of the linked list to the current end of the chain, and determine the expiration time of the timestamp when the current token is extracted based on the length from the head to the current end of the chain.
[0200] In some embodiments, the second determination module 16 is used to determine the second time as the expiration time of the timestamp when the current token is extracted if the length from the table header to the current chain tail is less than or equal to a preset current limiting number of times.
[0201] In some embodiments, the second determination module 16 is used to obtain the timestamp stored in the header if the length from the header to the current chain end is greater than a preset current limit number, and if the difference between the current time and the timestamp stored in the header is greater than a preset current limit time, determine the second time as the expiration time of the timestamp when the current token is extracted.
[0202] In some embodiments, the extraction module 12 is used to determine that there is an available token if the difference between the current time and the timestamp when the last token was extracted is greater than or equal to a preset time difference, wherein the time difference is determined based on the time when the token bucket generates a token.
[0203] On the other hand, embodiments of the present application also provide an electronic device and a computer-readable storage medium.
[0204] See also Figure 8 , Figure 8 A schematic diagram of an electronic device according to an embodiment of the present application.
[0205] like Figure 8, is a block diagram of an electronic device according to an embodiment of the present application. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the embodiments of the present application described and / or required herein.
[0206] like Figure 8 As shown, the electronic device includes: one or more processors 101, a memory 102, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. The various components are interconnected using different buses and can be installed on a common mainboard or installed in other ways as needed. The processor can process instructions executed in the electronic device, including instructions stored in or on the memory to display graphical information of the GUI on an external input / output device (such as a display device coupled to the interface). In other embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories. Similarly, multiple electronic devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 8 A processor 101 is taken as an example.
[0207] The memory 102 is a non-transient computer-readable storage medium provided in an embodiment of the present application. The memory stores instructions executable by at least one processor to enable the at least one processor to perform the resource access method provided in an embodiment of the present application. The non-transient computer-readable storage medium of an embodiment of the present application stores computer instructions, which are used to enable a computer to perform the resource access method provided in an embodiment of the present application.
[0208] The memory 102 is a non-transient computer-readable storage medium that can be used to store non-transient software programs, non-transient computer executable programs and modules, such as program instructions / modules in the embodiments of the present application. The processor 101 executes various functional applications and data processing of the server by running the non-transient software programs, instructions and modules stored in the memory 102, that is, implements the resource access method in the above method embodiment.
[0209] The memory 102 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 102 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory 102 may optionally include a memory remotely arranged relative to the processor 101, and these remote memories may be connected to the electronic device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a Blockchain Service Network (BSN), a mobile communication network, and a combination thereof.
[0210] The electronic device may further include: an input device 103 and an output device 104. The processor 101, the memory 102, the input device 103 and the output device 104 may be connected via a bus or other means. Figure 8 The example of connecting through bus is taken in the following.
[0211] The input device 103 can receive input digital or character information, and generate key signal input related to user settings and function control of the electronic device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a trackball, a joystick and other input devices. The output device 104 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The display device may include, but is not limited to, a liquid crystal display (LCD), a light emitting diode (LED) display and a plasma display. In some embodiments, the display device may be a touch screen.
[0212] Various implementations of the systems and techniques described herein can be realized in digital electronic circuit systems, integrated circuit systems, dedicated ASICs (application specific integrated circuits), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0213] These computer programs (also referred to as programs, software, software applications, or code) include machine instructions for programmable processors and can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. As used herein, the terms "machine-readable medium" and "computer-readable medium" refer to any computer program product, device, and / or means (e.g., disk, optical disk, memory, programmable logic device (PLD)) for providing machine instructions and / or data to a programmable processor, including a machine-readable medium that receives machine instructions as a machine-readable signal. The term "machine-readable signal" refers to any signal for providing machine instructions and / or data to a programmable processor.
[0214] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0215] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a Block-chain-based Service Network (BSN), a wide area network (WAN), and the Internet.
[0216] A computer system may include clients and servers. Clients and servers are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship to each other.
[0217] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0218] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A resource access method, It is characterized in that The method comprises: According to the distributed lock obtained based on the access request for accessing the service resource, a timestamp of when the last token was extracted is obtained from the data of the token bucket, wherein the token bucket includes a linked list, and the linked list stores the timestamp of when any token is extracted; If the difference between the current time and the timestamp of the last token extraction is greater than or equal to a preset time difference, it is determined that there is an available token in the token bucket, and the available token is extracted, wherein the time difference is determined according to the time when the token bucket generates a token; Access the service resource according to the available token; The method further comprises: Determining the length of the linked list; If the length of the linked list is greater than or equal to the preset current limiting times, obtaining the timestamp stored in the header of the linked list; If the difference between the current time and the timestamp stored in the header is greater than the preset current limiting time, the available token is extracted.
2. The method according to claim 1, It is characterized in that The method further comprises: If the length of the linked list is less than the preset current limit times, the available tokens are extracted.
3. The method according to claim 1 or 2, It is characterized in that The method further comprises: The timestamp when the current token is extracted is written into the linked list and serves as the current tail of the linked list.
4. The method according to claim 3, It is characterized in that The method further comprises: Determine the expiration time of the timestamp when the current token is extracted; Furthermore, writing the timestamp when the current token is extracted into the linked list includes: writing the timestamp when the current token is extracted and the expiration time of the timestamp when the current token is extracted into the linked list.
5. The method according to claim 4, It is characterized in that Determining the expiration time of the timestamp when the current token is extracted includes: Determine whether the timestamp when the current token is extracted is the timestamp when it is first written into the linked list; If the timestamp when the current token is extracted is the timestamp that is first written into the linked list, the first time is determined as the expiration time of the timestamp when the current token is extracted.
6. The method according to claim 5, It is characterized in that If the timestamp of the current token being extracted is not the timestamp of the first time the token is written into the linked list, the method includes: Obtain the length from the head of the linked list to the end of the current link; The expiration time of the timestamp when the current token is extracted is determined according to the length from the table header to the current chain tail.
7. The method according to claim 6, It is characterized in that The step of determining the expiration time of the timestamp of the current token being extracted according to the length from the table header to the current chain tail includes: If the length from the table header to the current chain tail is less than or equal to the preset current limiting times, the second time is determined as the expiration time of the timestamp when the current token is extracted.
8. The method according to claim 6, It is characterized in that The step of determining the expiration time of the timestamp of the current token being extracted according to the length from the table header to the current chain tail includes: If the length from the table header to the current chain end is greater than the preset current limiting times, obtaining the timestamp stored in the table header; If the difference between the current time and the timestamp stored in the header is greater than the preset current limiting time, the second time is determined as the expiration time of the timestamp when the current secondary token is extracted.
9. A resource access device, It is characterized in that The device comprises: An acquisition module, configured to acquire a timestamp of a last token extraction from data in a token bucket according to a distributed lock obtained based on an access request for accessing a service resource, wherein the token bucket includes a linked list, and the linked list stores a timestamp of an extraction of any token; An extraction module, configured to determine that there are available tokens in the token bucket and extract the available tokens if the difference between the current time and the timestamp of the last token extraction is greater than or equal to a preset time difference, wherein the time difference is determined according to the time when the token bucket generates a token; An access module, used for accessing the service resource according to the available token; The device also includes: A first determining module, used to determine the length of the linked list; The extraction module is used to: if the length of the linked list is greater than or equal to the preset current limiting number, obtain the timestamp stored in the header of the linked list; if the difference between the current time and the timestamp stored in the header is greater than the preset current limiting time, extract the available token.
10. The device according to claim 9, It is characterized in that The extraction module is used to extract the available token if the length of the linked list is less than a preset current limiting number.
11. An electronic device, It is characterized in that include: Memory, processor; Memory; a memory for storing instructions executable by the processor; The processor is configured to execute the method as claimed in any one of claims 1 to 8.
12. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 8 when executed by a processor.
13. A computer program product, It is characterized in that The computer program product stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 8 when executed by a processor.
