fiber icon indicating copy to clipboard operation
fiber copied to clipboard

📝 [Proposal]: Allow removing registered route

Open rebaz94 opened this issue 1 year ago • 11 comments

Feature Proposal Description

Following the addition of RebuildTree method #2769, which enabled dynamic request handling, I propose enhancing this functionality by allowing the removal of registered routes.

Currently, while we can dynamically add routes, the ability to remove them is limited. When adding a route with the same path, instead of updating the existing handler, a new one is added, which prevents the updated handler from being invoked. To address this issue, and to provide a way to remove routes that are no longer needed, we require a method to completely remove the route.

Alignment with Express API

N/A

HTTP RFC Standards Compliance

N/A

API Stability

The proposed feature will involve the addition of a single method, RemoveRoute, which will be used to unregister specific routes based on the path and HTTP methods. This function will maintain the stability of the API by ensuring that the removal process is straightforward and does not interfere with other routes or the overall routing structure.

func (app *App) RemoveRoute(path string, methods ...string) {
    // TODO:
}
 

Feature Examples

func addMyRoute() {
    // Remove route if it already exists
    s.app.RemoveRoute("/hello", fiber.MethodHead, fiber.MethodGet)

    // Add new route
    s.app.Get("/hello", func(c *fiber.Ctx) error {
        return c.SendString("hello")
    })

    // Rebuild the routing tree to reflect changes
    s.app.RebuildTree()
}

s.app.Get("/define", func(c *fiber.Ctx) error {
    before := s.app.HandlersCount()
    addMyRoute()
    after := s.app.HandlersCount()

    return c.JSON(map[string]any{
        "hcBefore": before,
        "hcAfter":  after,
    })
})

Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have searched for existing issues that describe my proposal before opening this one.
  • [X] I understand that a proposal that does not meet these guidelines may be closed without explanation.

rebaz94 avatar Aug 08 '24 12:08 rebaz94

@rebaz94 Rebuilding the tree by itself is not thread-safe already, adding this functionality would make it even worse.

gaby avatar Aug 08 '24 12:08 gaby

Hi @gaby, of course but it's important to note that production applications typically don't define routes at runtime. This approach is safe during development when you're iterating on your application. In my case I'm building a no-code solution where I need to add, update or remove routes while the server is running during development but for the production environment, I will generate the routes statically.

rebaz94 avatar Aug 08 '24 13:08 rebaz94

@rebaz94 The trade-off is huge. We ran benchmarks and it requires adding a Mutex in several parts which would make Fiber way slower across the board. Especially when calling Next().

gaby avatar Aug 08 '24 13:08 gaby

Sure. but I don't think adding RemoveRoute would cause any issues during development. we just need to remove route info and call RebuildTree.

rebaz94 avatar Aug 08 '24 14:08 rebaz94

@ReneWerner87, is there any chance this feature could be included in v3?

rebaz94 avatar Aug 08 '24 23:08 rebaz94

@rebaz94 From me, I'd say yes, but we may need to add a "Development" page to the docs 😂

gaby avatar Aug 08 '24 23:08 gaby

This probably needs to be analyzed thoroughly, and also it's good to notice that the RebuildTree method is extremely intense and slow for use. If the feature is approved, I can check out the code and see if it's not going to break anything major.

luk3skyw4lker avatar Aug 21 '24 12:08 luk3skyw4lker

Hi, I would like to work on this one.

ckoch786 avatar Oct 17 '24 14:10 ckoch786

@ckoch786 Assigned

gaby avatar Oct 18 '24 00:10 gaby

Hi @ckoch786 any updates?

efectn avatar Dec 01 '24 12:12 efectn

Hi @efectn abi, yes. I just submitted a PR 😺

ckoch786 avatar Dec 05 '24 06:12 ckoch786