kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.27k stars 2.47k forks source link

Dynamic Route Rule #2167

Open lhdhtrc opened 1 year ago

lhdhtrc commented 1 year ago

Is your feature request related to a problem? Please describe. https://github.com/kataras/iris/issues/1507

Describe the solution you'd like I need to dynamically modify the runtime routing, including request paths, request middleware, request handlers, and possibly add/remove a routing rule dynamically, because I want to build a gateway service with iris

Describe alternatives you've considered We have thought about using middleware to deal with these contents, but it is not reasonable. At present, the whole gateway is based on gin. However, we may need to consider other ways to achieve our requirements due to the above problems

Additional context Add any other context or screenshots about the feature request here.

kataras commented 1 year ago

Hello @2802402118,

Can I ask how you do it with gin? Because it shouldn't be allowed to modify the routing while the server is running, if you are doing it I assume it crashes because of concurrent access of the gin's routing map.

With Iris you can re-build the router while it's running but I am not recommending it. In order to build a gateway, you have to think differently - you don't have to modify the routing at serve-time. You just need wildcard/dynamic subdomains or/and route paths. Can you show us an example so I can help you to refactor and improve your code with Iris?

lhdhtrc commented 1 year ago

Hello @kataras

In gin I use handle to dynamically add routes and middleware to intercept deleted routes because my routing information is stored in mysql.

However, when the runtime uses handle to add the same route repeatedly, it causes an error. In this case, I wanted to modify the routing middleware, but unfortunately I did not find a solution from gin.

I tried to find a solution on chartgpt, but the option gpt gave me was to use iris instead, or my description to gpt was wrong. There is also iris's route deletion method, which cannot delete the corresponding route at runtime. I do not know whether the method I used is wrong or other circumstances.

We had a bunch of monolithic subsystems that needed to be fused, so we needed a robust gateway to manage permissions and authentication for those subsystems. Here we want to have full control over each interface, and it would be slow if the service had to be restarted every time the route was changed. We have implemented gin to dynamically add routing rules and middleware to intercept deleted/disabled routes, but middleware modification/removal of routes seems to have hit a bottleneck. Of course, we also use iris to achieve the above two points, iris is better than gin, iris seems to provide static route deletion function, but what we need is dynamic, of course, deletion is not necessary, because there is a middleware can do this, currently stuck in the problem of unable to modify the routing rules, if you have a suitable solution, Can you describe it? Thank you for your reply!

kataras commented 1 year ago

Hello @2802402118,

Chat GPT is getting smarter and smarter :)

I think the best way is to just just use dynamic routing path parameters, e.g. /api/{name}/{action:path} and then you can fetch from the mysql database in the handler, to find the gateway and serve it. However, if you really want to modify the routing at serve-time, it will cause performance issues because it would require extra locking the server on request initialization and unlocking on the end of each request, I can write a system for Iris like that ofc but I really don't recommend it. I need to see more details to provide the best solution.

lhdhtrc commented 1 year ago

Hello @kataras

Thank you. I'll try your solution

kataras commented 1 year ago

Hello @2802402118, just to know, if you still want to modify the underline routes while server is up and running, you can use the new EnableDynamicHandler/WithDynamicHandler setting . Example:

app.Listen(":8080", iris.WithDynamicHandler)

And after each modification (e.g app.Get("...", ...)) you have to call the app.RefreshRouter method (as previously but now it's safer).

Note that, if the EnableDynamicHandler setting is enabled, you'll add negative impact on the performance of the server.

To update to the latest revision:

go get github.com/kataras/iris/v12@master
lhdhtrc commented 1 year ago

hello @kataras

Thanks, I will try a new approach, currently I am using middleware + request forwarding to implement;

kataras commented 1 year ago

And I totally agree with it @2802402118!