This is a created article in which the information may have evolved or changed.
Recently looked at Golang's martini framework, also tried, this cottage from node. JS's Express framework and the latest KOA framework of the Golang framework,
It is also characterized by low coupling, but is still worrying about its performance because the core uses reflect reflectance technology.
Golang http package hander, handerfuc these to solve the problem of middleware, such as authentication, logs, etc., take a layer of packaging,
Like onions. The disadvantage of this code is the high degree of coupling. If the order of the middleware or the name transformation, there is a lot of code to change.
To reduce dependency, you need to let your code not directly reference the name of the previous layer and add an indirect layer to the third party. Reflect is feasible, but reflect reflection is called by the name of each function that takes a stupid approach to finding the object type (struct), based on the object's knowledge.
This can be better solved if you take the following methods:
Each component to be added adheres to a simple interface convention, such as keeping functions that need to be exported as external calls in a data structure like an array of function pointers, so that the framework will know how to use the components to be enabled, in favor of synergy between the two. Then, if you enable the component, you can put the array together with an array of other components and the Web app calls in turn. This lower-level approach is more efficient or can be higher.
In order to further consider improving the efficiency, if the component running time is longer, can use Gorutine + channel way to achieve the effect of parallel execution of the filter mode of each middleware? Golang Net/http Bottom has adopted the Gorutine processing concurrent connection, do not know will conflict?
KOA Framework Core Co is based on the yield keyword in the ES6 new specification to achieve the effect of the co-process, which is similar to the Gorutine + channel.