CN111399815A - Asynchronous function flow control method suitable for network target range construction - Google Patents

Asynchronous function flow control method suitable for network target range construction Download PDF

Info

Publication number
CN111399815A
CN111399815A CN202010157263.9A CN202010157263A CN111399815A CN 111399815 A CN111399815 A CN 111399815A CN 202010157263 A CN202010157263 A CN 202010157263A CN 111399815 A CN111399815 A CN 111399815A
Authority
CN
China
Prior art keywords
variable
function
instance
class
commitment
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202010157263.9A
Other languages
Chinese (zh)
Other versions
CN111399815B (en
Inventor
杨亦松
李昆
刑元
单联强
温泉
刘康涛
夏旸
唐舸轩
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Institute of Computer Technology and Applications
Original Assignee
Beijing Institute of Computer Technology and Applications
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Institute of Computer Technology and Applications filed Critical Beijing Institute of Computer Technology and Applications
Priority to CN202010157263.9A priority Critical patent/CN111399815B/en
Publication of CN111399815A publication Critical patent/CN111399815A/en
Application granted granted Critical
Publication of CN111399815B publication Critical patent/CN111399815B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/20Software design
    • G06F8/24Object-oriented

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention relates to an asynchronous function flow control method suitable for network target range construction, and belongs to the technical field of information safety. The invention strictly adheres to the function naming rule in the Promise/A + specification and carries out extended encapsulation on the Promise/A + specification. The method is realized based on the PHP language, is realized by depending on the characteristics of an interface in the PHP language, abandons the realization method of using a Promise/A + rule as a parent class, thereby ensuring the reliability, designs a flow control mechanism for dealing with large-scale chain requests and large-scale concurrent requests in an asynchronous environment according to the actual requirements of network target range construction, improves the construction efficiency and reliability of the network target range, and finally realizes a set of complete asynchronous function flow control method suitable for network target range construction.

Description

Asynchronous function flow control method suitable for network target range construction
Technical Field
The invention belongs to the technical field of information security, and particularly relates to an asynchronous function flow control method suitable for network shooting range construction.
Background
The network shooting range is used as an important infrastructure for coping with network security threats, network countermeasure technical equipment tests, network attack and defense tactic drills and cultivation and generation of network attack and defense countermeasures, and is applied to numerous network security departments. At present, the bottom layer construction of the network target range is mostly constructed based on open source architectures such as OpenStack, CEPH, OpenVswitch and the like. These open source architectures provide externally HTTP request-based API interfaces, such as creating networks, creating storage volumes, software-defined networking, and the like. The construction of the network shooting range requires synchronous or asynchronous calls to the external API interfaces to complete corresponding functions. Because the construction process of the network shooting range is complex, a strict process control mechanism is required to ensure the safety and reliability of interface calling. If a function flow control mechanism is not established, a callback function or even a plurality of layers of nested callback functions frequently appears in the process of asynchronously calling the external API by a program. The problem of call-back prisons is easily caused by a large number of multi-layer nested call-back functions, and difficulties are caused to later expansion and code maintenance of a network shooting range. More seriously, in the multi-layer nested callback function, the system hands the callback function to the nested third-party compiler for processing, so that the control right of the function is abandoned, and meanwhile, the return value and the memory stack information of the callback function are lost, and a serious safety problem exists. Therefore, the callback function mode does not meet the construction requirement of the network range. How to construct a set of asynchronous function flow control method suitable for constructing a network target range is a problem which needs to be solved urgently.
The Promise/A + standard is released, so that the safety problem of a callback function is solved, namely, a plurality of layers of nested asynchronous callback functions are converted into sequential asynchronous functions, and a program can obtain the lost return value and the memory stack information in the nested functions again, thereby controlling the execution flow of the program. The Promise/A + specification provides a new solution for asynchronous flow control, and many WEB construction frameworks design own asynchronous flow control mechanisms based on the Promise/A + specification. However, the Promise/a + specification and these flow control mechanisms are not completely suitable for constructing network target ranges. The reason is mainly as follows:
the 1Promise/a + standard does not specify a specific implementation mechanism of callback function conversion, and a method for capturing exceptions is not given, so that the program flow cannot be specified by directly using the Promise/a + standard.
2, most of the existing architectures implement the Promise/a + rule as a parent class, and when the Promise/a + rule implementation class is inherited by the subclasses, the subclasses can modify variable values contained in the parent class, which affects the reliability of the program under specific conditions.
3 the most popular flow control mechanism frame at present extends the naming mode in the Promise/A + specification, even modifies the method name specified in the Promise/A + specification. Such modifications may conflict with the subsequent Promise/a + standard, causing difficulties in late program expansion and maintenance.
4 in the process of realizing the Promise/A + rule by the existing architecture, the actual requirement of network target range construction is not considered, and the flow control problem of a large-scale concurrent request function and a large-scale chained request function under the asynchronous condition is not considered.
Disclosure of Invention
Technical problem to be solved
The technical problem to be solved by the invention is as follows: how to provide an asynchronous function flow control method suitable for network shooting range construction.
(II) technical scheme
In order to solve the technical problem, the invention provides an asynchronous function flow control method suitable for network shooting range construction, which comprises the following steps:
step one, designing an asynchronous function flow control mechanism class diagram;
based on the first step, class definition and encapsulation are carried out on the Promise/A + rule by using a programming method;
designing a program/A + rule coroutine mode realization flow based on the step two;
and step four, designing a concurrent request function flow control mechanism based on the step one.
(III) advantageous effects
The invention strictly adheres to the function naming rule in the Promise/A + specification and carries out extended encapsulation on the Promise/A + specification. The method is realized based on the PHP language, is realized by depending on the characteristics of an interface in the PHP language, abandons the realization method of using a Promise/A + rule as a parent class, thereby ensuring the reliability, designs a flow control mechanism for dealing with large-scale chain requests and large-scale concurrent requests in an asynchronous environment according to the actual requirements of network target range construction, improves the construction efficiency and reliability of the network target range, and finally realizes a set of complete asynchronous function flow control method suitable for network target range construction.
Drawings
FIG. 1 is a diagram of the asynchronous function flow control mechanism class of the present invention;
FIG. 2 is a flowchart of a Promise/A + rule implementation of the present invention;
FIG. 3 is a flowchart of the implementation of the project/A + rule coroutine mode of the present invention;
FIG. 4 is a flow control diagram of a concurrent request function according to the present invention.
Detailed Description
In order to make the objects, contents, and advantages of the present invention clearer, the following detailed description of the embodiments of the present invention will be made in conjunction with the accompanying drawings and examples.
The invention provides an asynchronous function flow control method suitable for network target range construction, which comprises the following steps:
designing the asynchronous function flow control mechanism class diagram shown in fig. 1, the content includes:
(1) the "Promise" interface defines the reference type of the Promise process. The method name and variable name in the interface are all in accordance with the Promise/A + specification, including then, onFulfilled, onRejected, reject, resolve. Three constants of waiting, executing and rejecting are defined in the interface, and three states of the Promise are represented. Because the interface can not be objectified, the "Promise" class in the architecture of fig. 1 is the implementation of the interface, and no subclass exists in the "Promise" class, the instance variables of the "Promise" class in the architecture will not be affected by any class inheritance relationship, so that the problem of reliability of the Promise process caused by inheritance of the factor class to the parent class is solved.
(2) The "promise" class implements the "promise" interface. This class defines the Promise process specifically. The main variables and functions are as follows:
the "state" variable is used to save the state of the "committed" class instance, with an initial value of "wait".
The result variable is used to save the result value returned by the Promise process.
The "execute function after cancellation" variable points to the function that needs to be executed after the cancel of the Promise procedure.
An "internal encapsulating function" variable points to a function to be executed encapsulated within a "committed" class instance.
The "wait queue" variable holds multiple "committed" class instances that are chained.
The "callback queue" variable holds callback functions with objects of the "committed" class in the "wait" state.
The "execute immediately" function is responsible for calling the function pointed to by the "encapsulate function" variable.
The then function completes the Promise interaction process, and the then function registers a callback function to receive a return value after the Promise executed correctly or a return value of the reason for the failure of the Promise execution. And returns a "committed" class object.
The relax function and reject function are used to change the status of the "committed" class object, the results of which are saved in the "results" variable, which will be used as a parameter for the callback function.
And 10, using a function called after failure to register a callback function, and using a variable value of a result as a callback function parameter. Only when the variable value of the "commitment" class object "state" is "reject", the callback function registered by the "call after failure" function is called.
The 'execution queue' function is used for establishing a function queue and storing callback functions in the Promise process.
The "format conversion" function can clean up, i.e., convert, data into objects of the "committed" class.
(3) The "committed implementation" class implements the "committed" interface. Indicating that a Promise process was executed correctly and that the "onfulfiltered" callback function registered in the then function will be executed immediately. (4) The "commit failed" class implements the "commit" interface. Indicating that a Promise process was not executed correctly and that the onRejected callback function registered in the then function will be executed immediately.
(5) The "coroutine" class implements a "commitment" interface. This class is a specific encapsulation implementation of the Promise process. The constructor of the class calls an external iterator and returns a "committed" class that satisfies the final return value of the iterator. In the execution process, the class maintains a current commitment object which is used for storing the commitment class object meeting the current return value of the iterator, after the value returned by the iterator is processed each time, the program control right is returned to the iterator again, and the coroutine processing mode can reduce the complexity of codes and reduce the memory occupation amount when processing large-scale asynchronous modular chained requests.
(6) The "concurrency control" class implements the "commitment generation" interface. The class implementation aims at controlling the iterative execution of multiple "commitment" class objects using one "commitment" class object. The method can be used for flow control of the concurrent request function resource pool, and comprises the steps of setting the exit rate of the concurrent request function resource pool and processing data in the request pool by using uniform processing logic.
(7) The exception class of the system is inherited by the exception when canceling, the exception of the set and the exception of the promise. For throwing out exceptions and errors in the program.
(8) The "queue to execute" class implements the "queue to execute" interface. A FIFO queue is implemented for holding callback functions to be run and waiting for these callback functions to be called when appropriate.
This section describes how, based on the first section, the Promise/a + rules are class-defined and encapsulated using programming methods to make them usable in a real production environment. FIG. 2 is a flowchart of implementation of the promise/A + standard, which includes:
2.1Promise/A + rule implementation process:
1 → 2 → 3 → (one of 4 to 10) → 11 → (one of 12 to 14) → (one of 15 to 18) → 19
1 constructs a "commitment" class instance, e.g., "a". And assigning the address of the function to be executed to the variable of the internal encapsulation function.
2 execute the then function and load the callback function into the "committed" class instance.
3 judging the variable value of the state in the commitment class instance A.
4 if the variable value of the state of the instance A is waiting, saving the callback function by using the callback queue array in the instance A. Waiting for an appropriate time to invoke.
And 5, if the variable value of the state of the instance A is rejected and the corresponding callback function is included, taking the variable of the result of the instance A as the parameter of the callback function, and adding the callback function into the queue constructed by the queue to be executed.
6 if the variable value of the state of the instance A is 'reject' and does not contain the corresponding callback function, converting the variable of the result of the instance A into the instance of the commitment class and returning.
And 7, if the variable value of the state of the instance A is execution and comprises a corresponding callback function, and the variable value of the result of the instance A is constant, taking the variable of the result of the instance A as a callback function parameter, and adding the callback function into a queue constructed by the queue to be executed.
8 if the "state" variable value of instance "A" is "execute", and instance "A" includes a corresponding callback function, and the "result" variable type of instance "A" is the "commit" interface, then the callback function included by instance "A" was loaded by the then method that called the "result" variable.
9 if the variable value of the "state" of the instance "a" is "execute" and includes a corresponding callback function, and the "result" variable of the instance "a" includes the then method, the "result" variable is converted into a "commit" type instance, and then the then method calling the converted "commit" class instance loads the callback method included in the previous "commit" class instance "a".
And 10, if the variable value of the state of the instance A is 'execution' and does not contain the corresponding callback function, converting the variable of the result of the instance A into the instance of the commitment class and returning.
Figure BDA0002404522310000081
Calling the "immediate execution" method in the example "A" executes the function to be executed indicated by the "internal encapsulation function" variable.
Figure BDA0002404522310000082
If the variable value of the state in the instance A is waiting, an error is thrown out, and the program is ended.
Figure BDA0002404522310000083
If the variable value of the state in the instance A is not waiting and the method of the immediate execution in the instance A is successfully executed, the variable value of the state in the instance A is set to be executed, and the return value of the function of the immediate execution is saved in the variable of the result in the object A.
Figure BDA0002404522310000084
If the variable value of the state in the instance A is not waiting and the method of the immediate execution in the instance A is not successfully executed, the variable value of the state in the instance A is set to reject and the return value of the immediate execution function is saved in the variable of the result in the instance A.
Figure BDA0002404522310000085
If the callback queue variable array in this instance "A" is empty, the program ends.
Figure BDA0002404522310000086
If the callback function is included in the variable array of the callback queue in the instance A and the then method is not included in the result variable in the instance A, the result variable of the instance A is used as the parameter of the callback function, and the callback function is added into the queue constructed by the queue to be executed.
Figure BDA0002404522310000087
If the callback function is included in the callback queue variable array in the instance A, the result variable in the instance A is the commitment type object, and the state variable in the result variable has the value of wait, the callback queue variable array in the previous instance A is merged with the callback queue variable array in the result variable. Waiting for subsequent processing.
Figure BDA0002404522310000091
If the callback function is included in the callback queue variable array in the instance "A", and the result variable in the instance "A" is the "committed" type object, and the state value of the state variable in the result variable is not "wait", then the callback function included in the previous instance "A" is loaded by calling the then method in the result variable.
Figure BDA0002404522310000092
All functions in the queue constructed by the "queue to be executed" class are executed.
2.2 scheme description:
the event state is preserved by constructing the instances of the 'commitment' class, wherein one 'commitment' class instance represents one event, and the 'commitment' class instance state is divided into waiting, executing and rejecting. The "committed" class instance state can only be converted to execution by wait or rejected. When the state of the object changes, the state cannot be changed again. The type of the "state" variable in the Promise class is private, changing the state of a "committed" class object only by executing its own reserve and reject methods. The "internal encapsulation function" variable points to the function executed by the Promise event. The Then function encapsulates the callback function to be executed after the event is executed successfully or unsuccessfully.
The callback queue is an array variable and stores callback functions of the commitment class instances in a waiting state. The internal format of the "callback queue" array is [ Promise, onFullfiltered, onRejected ], where the Promise variable is the new "committed" instance returned by the function; onFullfiltered, onRejected is the callback function that the then function loads. When the "committed" class instance calls the then function multiple times while in the "wait" state, all callback functions will be saved in the "callback queue" array.
Callback functions are pre-stored in a first-in-first-out (FIFO) queue implemented by the "queue to execute" class, and the callback functions held in the queue may use a closed form. The space addresses of the callback functions are actually stored in the queue, and compared with a callback function nesting mode, all the callback function addresses in the queue are visible, so that the safety of the callback functions is guaranteed. And the queue to be executed class ensures that all callback functions in the queue can be effectively executed after the program script is executed through a register _ shutdown _ function in the PHP language.
The second part of implementation of the Promise/A + rule class is syntax support of the encapsulation process, and the second part of implementation is based on the second part of implementation. Fig. 3 is a flowchart of implementation of the Promise/a + rule coroutine mode, which includes the following specific contents:
in the construction of a network range, many asynchronous sequential requests of chained calls are inevitably encountered. For example, A, B, C three http requests require that after a request is sent, a B request is constructed according to the return result of a, then a B request is sent, and finally a C request is constructed and sent according to the return result of B.
The above process, if expressed as per the Promise process implemented in fig. 2, would be:
A->wait()
A->resolve()
A->then(B->wait())->then(C->wait())
it can be seen that the order of execution of the requests can be controlled in accordance with the Promise/a + rules implemented in fig. 2. However, the following problems exist:
a large amount of duplicate code may appear in the 1 program.
2 all callback codes are pre-stored in the queue realized by the "queue to be executed", which will cause a large burden to the system when processing large-scale chained call requests. 3.1 designing a program coordination mode for solving the problems to realize the Promise/A + rule coordination, wherein the program flow is as follows:
1 construct a "commitment" type instance result.
2 set up generator variable to point to an external iterator.
The current element in the external iterator is loaded and converted to a "committed" class instance, e.g., "a".
And 4, if the variable value of the state of the commitment class instance A is execution, sending the result variable in the A to an external iterator. Turning to step 5; if "promise"
If the state of the class instance A is 'reject', the variable value of 'result' in 'A' is thrown out as the error reason. Go to step 6;
and 5, judging the state of the external iterator, and if the state is invalid, indicating that the iteration is finished. Calling the resolve function of result object, whose parameter is in "promise" class instance "A
The "result" variable value. Go to step 7. If the status is valid, it indicates that the iteration is not completed.
Go to step 6;
6, moving the pointer in the external iterator backward once, and returning to the step 3;
and 7, ending the flow.
3.2 description of the procedure:
the method separates the execution program from the flow control program in a coroutine mode. The program generates a request queue using an iterator. And creates a "committed" class instance for saving the return value or "committed" class object after the agent iteration is completed for subsequent processing. After the program has processed the result returned by the iterator, the control is returned to the iterator and the result value is returned, and this process completes the then method in the "commitment" class. After the iterator grasps the control right, the feedback result is used as a parameter to continuously execute the subsequent function, and meanwhile, program abnormity and errors are processed. This process completes the "execute immediately", resolve, etc. methods in the "committed" class. The transfer of the control authority can flexibly design a program flow, is convenient for capturing the abnormity, reduces the repetition rate of codes and improves the efficiency.
Compared with the implementation method of the third chapter, the coroutine mode can ensure that only one callback function returned by the iterator is saved in the memory used by the process or the thread. In the case of processing large-scale sequential asynchronous requests, the improvement in performance is positive.
Designing a concurrent request function flow control mechanism as shown in fig. 4, the specific contents are as follows:
concurrent request function flow control is an important problem to be solved in asynchronous function flow control. The concurrent request mode is a common mode in the network target range construction process, has a good concurrent mechanism, can more efficiently utilize hardware resources, and reduces the occurrence of abnormal requests. There are many ways to implement concurrent requests, such as curl, fsockopen, swoole, etc. which can cooperate with the PHP to complete concurrent requests. After the request function is encapsulated into a commitment class object in the architecture of the invention, wherein the variable of the internal encapsulation function is the request function, how to control the starting, ending, concurrent rate, concurrent lock and exception handling of the concurrent execution of the function becomes a key problem in the flow control of the asynchronous function. In order to solve the above problems, the present section provides a method for controlling a parallel function flow based on a coroutine mode, which is implemented based on the first section.
4.1 the specific flow is as follows:
1. a "concurrency control class" such as a is constructed.
2. The external iterator address is assigned to the "iterator" variable in a.
3. And assigning functions to be executed after the execution of the request function is successful or failed to the onFullfiltered and onRejected variables in the A.
4. And constructing a 'commitment' type variable, wherein the 'immediate execution method' variable of the variable calls all functions in an iterator pointed by the 'iteration' variable in the A in an iteration mode. And assigns the "committed" type variable to the "Total control" variable in A.
5. And setting a parallel speed value, and assigning to an exit speed variable in the A.
6. And setting the size of the array to be sent in the A to be equal to the variable of the exit rate.
7. The load method in A is called to store the current element in the iterator variable into the pending array, and the element is converted into the committed class instance.
8. And calling an iteration method in the step A, and moving the pointer in the variable of the iterator backward by one bit.
9. Step 7 and step 8 are repeated until the "to-do" array is full.
10. The "committed" instances in the "pending" array are concurrent.
11. If the concurrent requests are completed, executing the 'confirmation completion' method in the step A, judging whether elements exist in the 'iterator' variable or not, and if so, repeatedly executing the steps 7 to 10.
12. When the iterator variables are completely iterated, the master control variable in the A executes a reserve method, and the parameter value is null, so as to finish the concurrent request.
4.2 scheme description:
4.2.1 concurrent Rate control:
concurrent function flow control must control the rate, too low a rate cannot fully utilize the performance of the hardware, and too high a rate may cause system crash. The method packages all request functions into 'commitment' class objects, caches the objects by using a storage array, and then executes the objects concurrently. After concurrent execution, clearing the array position where the request is completed, synchronously retrieving a new request function from the iterator, adding the new request function into the storage array, and iteratively executing the process until the content in the iterator is iteratively completed. Different space size values are set for the array storing the 'commitment' class object, and then the control of the concurrent request rate is completed.
4.2.2 asynchronous concurrent controller design:
the method designs the concurrency controller as a 'commitment' class instance, and uses the 'commitment' class instance to control the concurrent execution of a plurality of requests (the 'commitment' class instance). The specific method is that the method pointed by the variable of the internal encapsulation function of the commitment class instance of the controller is used for executing the 'immediate execution' method of all the commitment class instances in the storage array in an iteration mode. When the "execute immediately" method of the controller instance is invoked, the "execute immediately" methods of all instances in the storage array are iteratively invoked.
After all the contents in the iterator are iterated, the controller's "committed" class instance will call the resolve method, indicating that the controller "committed" class instance has been executed.
4.2.3 concurrent Lock mechanism:
the data in the iterator is converted into a 'commitment' class instance and then stored in the array for concurrent execution, and then the data is retrieved from the iterator again and saved in the array to wait for subsequent iterations. In the process, because the return time of concurrent requests is not uniform, the problem of accessing the data of the iterator for multiple times at the same time can be caused, and the same data in the same iterator is called recursively. To avoid this, the method sets a "mutual exclusive lock" variable and an "iterative" method in the "concurrency control" class to complete the concurrent lock, as shown in fig. 1. Where the "iterative" method is responsible for stepping the function in the iterator one position further. The "mutex" variable is a boolean type that indicates the switching of concurrent locks.
When a process or a thread calls the iteration method, if the mutual exclusion lock variable is true, returning. At this time, the other threads call the iterator; otherwise, if the "mutual exclusion lock" variable is false, the "mutual exclusion lock" is set to true, and the "iterative" method is executed. After the function call is completed, the "mutex" is set to false again. Thus, a thread lock is implemented.
4.2.4 Exception handling
If the concurrent request function fails to send normally, an exception handling function is called. The present invention loads subsequent exception handling by calling the then method of the "promise" class.
The above description is only a preferred embodiment of the present invention, and it should be noted that, for those skilled in the art, several modifications and variations can be made without departing from the technical principle of the present invention, and these modifications and variations should also be regarded as the protection scope of the present invention.

Claims (10)

1. An asynchronous function flow control method suitable for network target range construction is characterized by comprising the following steps:
step one, designing an asynchronous function flow control mechanism class diagram;
based on the first step, class definition and encapsulation are carried out on the Promise/A + rule by using a programming method;
designing a program/A + rule coroutine mode realization flow based on the step two;
and step four, designing a concurrent request function flow control mechanism based on the step one.
2. The method of claim 1, wherein in step one, the asynchronous function flow control mechanism class diagram is designed such that the diagram includes the following:
(1) the "Promise" interface defines the reference type of the Promise process; the method name and the variable name in the interface are consistent with the Promise/A + specification and comprise the then, the onFulfilled, the onRejected, the reject and the resolve, and the interface defines three constants of waiting, executing and rejecting and represents three states of the Promise;
(2) the "promise" class implements the "promise" interface; the class specifically defines the Promise process, and includes the following variables and function functions:
the "state" variable is used to save the state of the "committed" class instance, with an initial value of "wait";
the result variable is used for saving a result value returned by the Promise process;
the variable of the 'execution function after cancellation' points to a function which needs to be executed after the cancel of the Promise process;
an "internal encapsulation function" variable points to a function to be executed encapsulated within a "committed" class instance;
the "wait queue" variable holds multiple "committed" class instances that are chained;
the callback queue variable stores callback functions of objects of the commitment class in a waiting state;
the "execute immediately" function is responsible for calling the function pointed to by the "encapsulate function" variable;
the then function completes the mutual process of the Promise, the then function registers the callback function to receive the return value after the Promise executes correctly or the return value of the reason of the failure of the Promise execution, and returns a 'commitment' class object;
the result function and reject function are used for changing the status of the object of the 'commitment' class, and the result is saved in a 'result' variable which is used as the parameter of the callback function;
and 10, using a function called after failure to register a callback function, and using a variable value of a result as a callback function parameter. Only when the variable value of the 'commitment' class object 'state' is 'refused', the callback function registered by the 'calling after failure' function is called;
an execution queue function is used for establishing a function queue and storing a callback function in the Promise process;
the format conversion function can perform cleaning conversion on the data, namely converting the data into an object of a commitment class;
(3) the "committed realization" class realizes the "committed" interface; indicating that a Promise process is executed correctly and will immediately execute the "onfulfiltered" callback function registered in the then function;
(4) the "failure to commit" class implements the "commit" interface; indicating that a Promise process was not executed correctly and immediately executing the onRejected callback function registered in the then function;
(5) the "coroutine" class implements the "commitment" interface; the class is a specific packaging implementation of the Promise process; the constructor of the class calls an external iterator and returns a 'commitment' class which meets the final return value of the iterator; in the execution process, the class maintains a current commitment object which is used for storing the commitment class object meeting the current return value of the iterator, and after the value returned by the iterator is processed each time, the program control right is returned to the iterator again;
(6) the 'concurrent control' class implements a 'commitment generation' interface; the method is characterized in that one 'commitment' class object is used for controlling the iterative execution of a plurality of 'commitment' class objects, and the method can be used for flow control of a concurrent request function resource pool, and comprises the steps of setting the exit rate of the concurrent request function resource pool and processing data in the request pool by using uniform processing logic;
(7) the 'abnormality when canceling', 'abnormality in set', 'abnormality in promise' inherits the exception class of the system and is used for throwing out the exception and error in the program;
(8) the queue to be executed class implements a queue to be executed interface, implements a FIFO queue for storing callback functions to be run, and waits for the callback functions to be called.
3. The method as claimed in claim 2, wherein in step two, the Promise/a + rule implementation flow is:
1 → 2 → 3 → (one of 4 to 10) → 11 → (one of 12 to 14) → (one of 15 to 18) → 19
①, constructing a "commitment" class instance "A", and assigning the address of the function to be executed to an "internal encapsulation function" variable;
② executing the then function and loading the callback function into the "committed" class instance;
③ determining the value of the variable "status" in "commitment" class instance "A";
④ if the variable value of the state of the instance A is waiting, saving the callback function by using the callback queue array in the instance A to wait for calling;
⑤ if the variable value of the state of the instance A is 'reject' and contains a corresponding callback function, taking the variable of the result of the instance A as the parameter of the callback function, and adding the callback function into the queue constructed by the class 'queue to be executed';
⑥ if the variable value of the state of the instance A is 'reject' and does not contain a corresponding callback function, converting the variable of the result of the instance A into the instance of the commitment class and returning;
⑦ if the variable value of the state of the instance A is execution and contains the corresponding callback function, and the variable value of the result of the instance A is constant, then the result variable of the instance A is taken as the callback function parameter and the callback function is added into the queue constructed by the queue to be executed;
⑧ if the "state" variable value of instance "A" is "execute", and instance "A" includes a corresponding callback function, and the "result" variable type of instance "A" is "committed" interface, then the callback function included by instance "A" is loaded by the then method that called the "result" variable;
⑨ if the variable value of the state of the instance A is ' execute ' and contains a corresponding callback function, and the ' result ' variable of the instance A contains the then method, the ' result ' variable is converted into a ' commitment ' type instance, then the converted ' commitment ' type instance is called, the then method of the ' commitment ' type instance is loaded, and the callback method contained in the ' commitment ' type instance A ' is loaded;
⑩ if the variable value of the state of the instance A is 'execute' and does not contain the corresponding callback function, the variable of the result of the instance A is converted into the instance of the commitment class and returned;
Figure FDA0002404522300000051
calling the method of 'immediate execution' in the instance 'A' to execute the function to be executed pointed by the variable of 'internal encapsulation function';
Figure FDA0002404522300000052
if the variable value of the state in the instance A is 'waiting', throwing out an error and ending the program;
Figure FDA0002404522300000053
if the variable value of the state in the instance A is not waiting and the method of the immediate execution in the instance A is successfully executed, the variable value of the state in the instance A is set to be executed, and the return value of the function of the immediate execution is saved in the variable of the result in the object A;
Figure FDA0002404522300000054
if the variable value of the state in the instance A is not waiting and the method of the immediate execution in the instance A is not successfully executed, the variable value of the state in the instance A is set to reject and the return value of the function of the immediate execution is saved in the variable of the result in the instance A;
Figure FDA0002404522300000055
if the variable array of the callback queue in the instance A is empty, the program is ended;
Figure FDA0002404522300000056
if the callback function is contained in the variable array of the callback queue in the instance A and the then method is not contained in the result variable in the instance A, taking the result variable of the instance A as the callback function parameter and adding the callback function into the queue constructed by the queue to be executed;
Figure FDA0002404522300000057
if the callback function is contained in the callback queue variable array in the instance A, the result variable in the instance A is a commitment type object, and the state variable in the result variable is waiting, merging the callback queue variable array in the previous instance A with the callback queue variable array in the result variable, and waiting for subsequent processing;
Figure FDA0002404522300000061
if the callback function is contained in the callback queue variable array in the instance A, the result variable in the instance A is a commitment type object, and the state value of the state variable in the result variable is not waiting, the callback function contained in the instance A before the loading of the then method in the result variable is called;
Figure FDA0002404522300000062
all functions in the queue constructed by the "queue to be executed" class are executed.
4. The method as claimed in claim 3, wherein in the process of implementing Promise/a + rule in step two, the event state is saved by constructing instances of "commitment" class, one "commitment" class instance represents an event, the "commitment" class instance state is divided into three types of waiting, executing and rejecting, the "commitment" class instance state can only be converted into executing or rejecting by waiting, when the object state changes, the state cannot be changed again, the type of the "state" variable in Promise class is private, the state of a "commitment" class object is changed, only by executing its own method of relove and reject, the "internally encapsulated function" variable points to the function executed by Promise event, the Then n function encapsulates the callback function to be executed after the event is executed successfully or fails;
the callback queue is an array variable, and stores a callback function of a commitment class instance in a waiting state, wherein the internal format of the array of the callback queue is [ Promise, onfulfiltered, onRejected ], and the Promise variable is a new commitment instance returned by the then function; onFullfiltered, onRejected is the callback function loaded by the then function, when the next "committed" class instance calls the then function for many times in the "wait" state, all callback functions will be saved in the "callback queue" array;
the callback function is pre-stored in a first-in first-out queue realized by a queue to be executed, the callback function stored in the queue can use a closure form, and the space address of the callback function is actually stored in the queue.
5. The method as claimed in claim 3, wherein in step three, the implementation flow of the Promise/a + rule coroutine mode is designed as follows:
① constructs a "committed" type instance result;
② establishing a generator variable pointing to an external iterator;
③ load the current element in the external iterator and convert it into a "committed" class instance, "A";
④, if the variable value of "state" of "commitment" class instance "A" is "execute", then send the variable value of "result" in "A" to external iterator, go to step ⑤, if the variable value of "state" of "commitment" class instance "A" is "reject", then throw the variable value of "result" in "A" as error cause, go to step ⑥;
⑤, judging the state of the external iterator, if the state is invalid, indicating that the iteration is completed, calling a resolve function of a result object, wherein the parameter is the variable value of the result in the commitment class instance A, and turning to step ⑦, if the state is valid, indicating that the iteration is not completed, turning to step ⑥;
⑥ moving the pointer in the outer iterator backward once, returning to step ③;
⑦ the flow ends.
6. The method as claimed in claim 5, wherein in step three, the executive program and the process control program are separated by means of coroutine, the program uses an iterator to generate the request queue, and creates a "commitment" class instance for storing the return value or the "commitment" class object after the iterator completes iteration for subsequent processing, after the program processes the result returned by the iterator, the program returns the control right to the iterator and returns the result value, this process completes the then method in the "commitment" class, after the iterator grasps the control right, the returned result is used as a parameter to continue to execute the subsequent function, and processes program exception and error, this process completes the "immediate execution" and resolve method in the "commitment" class.
7. The method as claimed in claim 2, wherein the flow control mechanism of the concurrent request function designed in step four has the following specific flows:
1. constructing a concurrency control class A;
2. assigning the address of the external iterator to an iterator variable in the A;
3. assigning the function to be executed after the execution of the request function is successful or failed to the onFullfiltered and onRejected variables in the A;
4. constructing a 'commitment' type variable, wherein the 'immediate execution method' variable calls all functions in an iterator pointed by the 'iteration' variable in the A in an iteration mode, and assigns the 'commitment' type variable to a 'master control' variable in the A;
5. setting a parallel speed value, and assigning the value to an exit speed variable in the A;
6. setting the size of the array to be issued in the step A to be equal to an exit rate variable;
7. calling a loading method in the A to store the current element in the iterator variable into a pending array, and converting the element into a commitment class instance;
8. calling an iteration method in the step A, and moving a pointer in an iterator variable backward by one bit;
9. repeating the step 7 and the step 8 until the array of the 'to-be-sent' is full;
10. concurrently sending the 'committed' instance in the 'pending' array;
11. if the concurrent request is completed, executing a 'confirmation completion' method in the step A, judging whether an element exists in an 'iterator' variable, and if so, repeatedly executing the step 7 to the step 10;
12. when the iterator variables are completely iterated, the master control variable in the A executes a reserve method, and the parameter value is null, so as to finish the concurrent request.
8.The method of claim 7, wherein in step four, all request functions are packaged as "commitment" class objects, and a storage array is used to cache the objects, and then the objects are executed concurrently, after the concurrent execution, the array position where the request is completed is cleared, and a new request function is synchronously retrieved from the iterator and added to the storage array, and the process is executed iteratively until the content in the iterator is completed iteratively, and different space size values are set for the array storing the "commitment" class objects, so that the control of the concurrent request rate is completed.
9. The method according to claim 7, wherein in step four, the concurrency controller is designed as a "committed" class instance, and one "committed" class instance is used to control the concurrent execution of multiple requests, and the specific method is that the method indicated by the "internal encapsulation function" variable of the "committed" class instance of the controller is made to iterate and execute the "immediate execution" method of all the "committed" class instances in the storage array, and when the "immediate execution" method of the controller instance is called, the "immediate execution" method of all the instances in the storage array is called iteratively; after all the contents in the iterator are iterated, the controller's "committed" class instance will call the resolve method, indicating that the controller "committed" class instance has been executed.
10. The method as claimed in claim 7, wherein in step four, the data in the iterator is converted into the "committed" class instance and then stored in the array for concurrent execution, and then the data is retrieved from the iterator again and saved in the array, and the subsequent iteration is waited for, during this process, a "mutual exclusion lock" variable and an "iteration" method are set in the "concurrency control" class to complete the concurrent lock, wherein the "iteration" method is responsible for stepping the function in the iterator forward by one position, and the "mutual exclusion lock" variable is of a boolean type and represents the switch of the concurrent lock;
when a process or a thread calls the iteration method, if the mutual exclusion lock variable is true, returning, and at the moment, indicating that other threads call the iterator; otherwise, if the variable of the 'mutual exclusion lock' is false, setting the 'mutual exclusion lock' to true, executing an 'iteration' method, and after the function call is finished, setting the 'mutual exclusion lock' to false again to realize the thread lock.
CN202010157263.9A 2020-03-09 2020-03-09 Asynchronous function flow control method suitable for network target range construction Active CN111399815B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010157263.9A CN111399815B (en) 2020-03-09 2020-03-09 Asynchronous function flow control method suitable for network target range construction

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010157263.9A CN111399815B (en) 2020-03-09 2020-03-09 Asynchronous function flow control method suitable for network target range construction

Publications (2)

Publication Number Publication Date
CN111399815A true CN111399815A (en) 2020-07-10
CN111399815B CN111399815B (en) 2023-06-30

Family

ID=71430582

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010157263.9A Active CN111399815B (en) 2020-03-09 2020-03-09 Asynchronous function flow control method suitable for network target range construction

Country Status (1)

Country Link
CN (1) CN111399815B (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112055026A (en) * 2020-09-11 2020-12-08 湖南泛联新安信息科技有限公司 Network target range physical environment construction method and system
CN113568686A (en) * 2020-10-30 2021-10-29 深圳市九九互动科技有限公司 Asynchronous processing method and device of Lua language, computer equipment and storage medium

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20140237452A1 (en) * 2013-02-15 2014-08-21 Microsoft Corporation Call Stacks for Asynchronous Programs
US9152533B1 (en) * 2011-12-06 2015-10-06 Amazon Technologies, Inc. Asynchronous programming system
US20160070773A1 (en) * 2014-09-04 2016-03-10 Home Box Office, Inc. Asynchronous models
CN108874523A (en) * 2018-06-21 2018-11-23 深圳点猫科技有限公司 A kind of programmed method based on the AI and promise of children education control asynchronous function sequence
CN110109756A (en) * 2019-04-28 2019-08-09 北京永信至诚科技股份有限公司 A kind of network target range construction method, system and storage medium

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9152533B1 (en) * 2011-12-06 2015-10-06 Amazon Technologies, Inc. Asynchronous programming system
US20140237452A1 (en) * 2013-02-15 2014-08-21 Microsoft Corporation Call Stacks for Asynchronous Programs
US20160070773A1 (en) * 2014-09-04 2016-03-10 Home Box Office, Inc. Asynchronous models
CN108874523A (en) * 2018-06-21 2018-11-23 深圳点猫科技有限公司 A kind of programmed method based on the AI and promise of children education control asynchronous function sequence
CN110109756A (en) * 2019-04-28 2019-08-09 北京永信至诚科技股份有限公司 A kind of network target range construction method, system and storage medium

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112055026A (en) * 2020-09-11 2020-12-08 湖南泛联新安信息科技有限公司 Network target range physical environment construction method and system
CN113568686A (en) * 2020-10-30 2021-10-29 深圳市九九互动科技有限公司 Asynchronous processing method and device of Lua language, computer equipment and storage medium
CN113568686B (en) * 2020-10-30 2023-11-28 深圳市九九互动科技有限公司 Asynchronous processing method and device for Lua language, computer equipment and storage medium

Also Published As

Publication number Publication date
CN111399815B (en) 2023-06-30

Similar Documents

Publication Publication Date Title
JP3863917B2 (en) Method for implementing a hierarchical call structure in a real-time asynchronous software application
Hinrichsen et al. Actris: Session-type based reasoning in separation logic
US20070169042A1 (en) Object-oriented, parallel language, method of programming and multi-processor computer
Alistarh et al. The complexity of renaming
CN111399815A (en) Asynchronous function flow control method suitable for network target range construction
Hammond Why parallel functional programming matters: Panel statement
WO1992007335A1 (en) A scalable parallel vector computer system
Dong et al. Model checking UML statecharts
Dovland et al. Verification of concurrent objects with asynchronous method calls
CN103631665B (en) A kind of method and system of the inter-thread communication based on message queue
CN109918054A (en) A kind of service bus micro-kernel frame design method based on Formal Specification
CN110968876A (en) MILS architecture-based secure operating system
CN104166539A (en) Parallel atomic increment
Johnsen et al. Combining active and reactive behavior in concurrent objects
Panunzio et al. Ada ravenscar code archetypes for component-based development
Nobakht et al. Programming with actors in Java 8
Salcic et al. A heterogeneous multi-core SoC for mixed criticality industrial automation systems
JP2023544911A (en) Method and apparatus for parallel quantum computing
Kim Synthesizing multithreaded code from real-time object-oriented models via schedulability-aware thread derivation
Papathomas et al. ‘Supporting Software Reuse in Concurrent Object-Oriented Languages: Exploring the Language Design Space
CN112749020A (en) Microkernel optimization method of Internet of things operating system
CN104951299A (en) Semaphore chain table stacking method based on atomic operation
Nikhil Id: A language with implicit parallelism
Ferenczi Parallel execution of object-oriented programs: Message handling strategies
Salcic et al. Optimizing latencies and customizing noc of time-predictable heterogeneous multi-core processor

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant