Comments (9)
@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.
I think a better question is whether there's a use case for deleting routes.
from mux.
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.
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.
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.
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.
thank you sir!
from mux.
@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.
instead of
root(w, r)
use
root.ServeHTTP(w, r)
from mux.
Related Issues (20)
- [bug] Headers are not accessible in Middleware when Request sent from Browser HOT 10
- [bug] Adding to GET the same endpoint with POST and different Queries ends up with inconsistent error messages
- [question] How to retrieve the handler func without the middleware? HOT 2
- [bug] HOT 17
- Path variable not parsed HOT 2
- [bug] : the link for gorilla mux logo is broken
- [feature] Add CONTRIBUTING.md HOT 4
- [bug]
- [question] Is it true? Is the GWT unarchived, for real? HOT 3
- CORS ERROR HOT 10
- JSON parsing failed HOT 7
- [BUG] Go can't find v2.0 HOT 1
- [BUG] MethodNotAllowedHandler does not work for subrouter with different routes HOT 9
- [BUG] Router does not distinguish between `/` and `%2F` in the request path HOT 1
- Cannot install gorilla/mux with new install command of go HOT 2
- [BUG] Index out of range in (*routeRegexp).Match HOT 1
- [FEATURE] Accidental omission of GetHeaders? HOT 1
- [BUG] API is probably broken for GetQueries per each method HOT 1
- [BUG] runtime error in (*routeRegexp).Match
- [FEATURE] Route metadata
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mux.