Coder Social home page Coder Social logo

deleting routes about mux HOT 9 CLOSED

gorilla avatar gorilla commented on May 2, 2024
deleting routes

from mux.

Comments (9)

 avatar commented on May 2, 2024 6

@Knetic The router does not concurrent modification and request routing. If you want to reload routes, then you should create a new router and swap it with the old one. For example:

type routerSwapper struct {
     mu sync.Mutex
    root *mux.Router
}

func (rs *routerSwapper) Swap(newRouter *mux.Router) {
     rs.mu.Lock()
     rs.root = newRoot
     rs.mu.Unlock
}

func (rs *routerSwappter) ServeHTTP(w http.ResponsWriter, r *http.Request) {
    rs.mu.Lock()
    root := rs.root
   rs.mu.Unlock()
   root(w, r)
}

Use routerSwapper as the root handler with the HTTP server. Call Swap wit a newly constructed router.

from mux.

kisielk avatar kisielk commented on May 2, 2024

I think a better question is whether there's a use case for deleting routes.

from mux.

Knetic avatar Knetic commented on May 2, 2024

I'd +1 this, I use mostly dynamically-mapped routes loaded from a mappings file. Being able to reload the file (and change the routes) without restarting the service (and thereby having to drain connections) would be ideal.

I'm currently using a forked Go stdlib muxer that supports route deregistration, but it's clunky and i'd prefer to use Gorilla's mux.

from mux.

kisielk avatar kisielk commented on May 2, 2024

I agree with @avrtno 's comment on this. Allowing deleting or modification of routes while the router is running would be troublesome to add at this point, and there's not many uses for it.

from mux.

mogthesprog avatar mogthesprog commented on May 2, 2024

Hey guys. I know this is an old issue, but i had a question about the method outlined above.

If i've got an open websocket connection being routed by the router when i call rs.Swap(), will those connections stay up until they're closed by either client/server, or will the router tare down those connections during the swap?

A little more detail, i'm using gorilla/mux and gorilla/websocket to reverse proxy multiple users and wanted to use this method to update the router on the fly. But i'm wondering whether the swapping process will interfere with already established connections.

from mux.

elithrar avatar elithrar commented on May 2, 2024

The connections will stay up.
On Tue, Jul 5, 2016 at 12:45 AM mogthesprog [email protected]
wrote:

Hey guys. I know this is an old issue, but i had a question about the
method outlined above.

If i've got an open websocket connection being routed by the router when i
call rs.Swap(), will those connections stay up until they're closed by
either client/server, or will the router tare down those connections during
the swap?

A little more detail, i'm using gorilla/mux and gorilla/websocket to
reverse proxy multiple users and wanted to use this method to update the
router on the fly. But i'm wondering whether the swapping process will
interfere with already established connections.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#82 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AABIcA-IS8Af-t1f0fyDd5V1vDk8wi0Zks5qSguhgaJpZM4DVyBK
.

from mux.

mogthesprog avatar mogthesprog commented on May 2, 2024

thank you sir!

from mux.

clarencefoy avatar clarencefoy commented on May 2, 2024

@Knetic The router does not concurrent modification and request routing. If you want to reload routes, then you should create a new router and swap it with the old one. For example:

(...)
func (rs *routerSwappter) ServeHTTP(w http.ResponsWriter, r *http.Request) {
    rs.mu.Lock()
    root := rs.root
   rs.mu.Unlock()
   root(w, r)
}

Use routerSwapper as the root handler with the HTTP server. Call Swap wit a newly constructed router.

When I try this, I get: cannot call non-function root (type *mux.Router)

Any ideas? I have tried simply removing it, and then using the Swap() function, but that seems to keep all routes the same? (Where my expectation would have been to see a router with no routes registered until I start registering new routes)

from mux.

SmartPhoneJava avatar SmartPhoneJava commented on May 2, 2024

instead of

 root(w, r)

use

root.ServeHTTP(w, r)

from mux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.