Spark: Remove all or certain routes

Created on 27 Jan 2015  路  24Comments  路  Source: perwendel/spark

We have a need to remove all routes and certain routes (given a path and an optional HTTP method) from our class that derives from SparkBase without having to restart the Web server. To support this, SparkBase should expose the following methods:

+ removeRoute(path: String): boolean
+ removeRoute(path: String, httpMethod: HttpMethod): boolean
+ clearRoutes(): void

These would then forward the request to SimpleRouteMatcher which needs to expose analogous methods.

Most helpful comment

do you think you're going to be injecting and removing routes live in bytecode?

@dessalines Sometimes yes.

I'm using spark-rest from Apache Camel and having problem removing and adding routes because they can't be removed from Spark.

I think that the discussion here is related to remove routes from Spark, why not?
What kind of problems can happen in Spark implementing this feature?

All 24 comments

why would you want this??

For times when routing isn't so static as usual

I got that, I'm really interested in the real world scenario where this would happen. Maybe it can be accomplished in another way.

I poured over the code base and couldn't find a way to do this in a less obtrusive manner. With only 3 methods added to SparkBase and no static helper methods added to Spark, I'm hoping that this low-level API addition will be acceptable for those rare cases (like I have) where more dynamism is needed. If there is a better way, please let me know, and I'd be happy to send a new PR.

wow... I'm not saying this is wrong, I'm just asking you your use case because I'm interested, I never ran into this problem and I would like to learn from it... If it's confidential that's cool

Ping me offline, @amarseillan, and I'll explain the use case in more detail. Too much to type.

Unless you can provide a real world use case I doubt it'll get implemented in the framework... but hey, I'm not @perwendel so what do I know?

FWIW, I'm also having trouble understanding why someone would need this feature (ie: when "routing isn't so static as usual")

I'll soon start to take care of the trailing Spark work, including this. I'm currently on parental leave with two kids so my mind is somewhere else :-).

@travisspencer can you explain the use case to me as well so I can contemplate if this is the best solution for it? Thanks

The use case is that we need to remove routes without having to restart the server in order to avoid unnecessary downtime.

I understand from the PoV of the community that it is important to protect Spark from feature creep. Unfortunately, I can't say more about what we're doing in an open forum. I'd be willing to do so using a more discrete method of communication (e.g., telephone). You've got my number, @perwendel. Call me some time, and I'll explain a bit more. If there's a better way to implement what we need, I'm happy to send a different patch.

Either way, thanks for considering this, and sorry that I can't be more forthcoming at this time.

Yeah I'm confused the use case for this too. Spark takes about 3 seconds to restart. If you really need this, why don't you just make your own repl that can spawn spark with only the routes you want in its own thread, and a command to kill the thread and restart it?

What is this super-secret use case?

Really disappointing that we're getting so much resistance on this. We've been good members of the community promoting Spark to our customers, at the Java user group meeting in Stockholm which we financed and hosted, we've sent other patches that have been committed, and are willing to do more to see Spark grow because we really like it and believe in it.

If we can't get this feature without disclosing IP that we're not comfortable sharing publicly, we'll have to code our own server start up, fork Spark, or do other less desirable alternatives that are all very disappointing.

Another thing you can do, is instead of actually removing the route, just disable it by disallowing all request IP(s). So you could do:

get(....) {
ensureRemovedAndThrowAnException(static REMOVE_A_ROUTE)
}

I'm having trouble believing you'd be willing to code your own server when you refuse to code simpler alternatives like what @tchoulihan is proposing

I said "server startup," @juanformoso, not server. Coding a server is a huge and unnecessary job.

Using @tchoulihan's suggestion makes the logic very convoluted in our context. There is no static call to get in our case. It's all dynamic, so adding that exception really means having a map of enabled routes. If it's not in our map, then we through. Sure, we could do that, but Spark already has such a map of routes. Why must we keep another? Why can't we just remove elements from Spark's map?

The solution I suggested would take about two minutes to code. You're requesting that spark be able to remove specific routes, and here's how to do that.

So with a dynamic get:

Initialize a map with what routes you want removed.
Map removeMap = ....

get("..../:dynamicPart ...) {
String dynamicPart = req.param(":dynamicPart");
ensureRemovedAndThrowARuntimeException(static map.get(dynamicPart));
}

I keep a separate fork of spark in my company because we need special things and because we need things that are not yet merged, there's no problem with that, its just how open source works

And if the reason you need this is secret, then there's no point on making a public release IMO

I'm a spark newbie, so I do not know if there are performance implication to what I suggest (thus do not know if it is a viable alternative), but instead of removing the route, maybe you could replace the handler for the route by a handler that would return a 410 Gone, or a 404 Not found.

Hope this help

:point_up_2: Routes cannot be overwritten as far as I know, first match first serve

Hi, I'm writing a dynamic service that needs to add/remove/change routes on demand. Seems like extending the spark.Service class to access the routes element is not an option (because Service is final).
Any other option to manage routes once added?
I don't like the invalidation alternative because the service may end having a huge number of unnecessary routes.
I don't like the server restart option because I'll need to recreate the server several times.
I'm using version 2.6.0.
Thanks in advance!
Marcos

I don't like the invalidation alternative because the service may end having a huge number of unnecessary routes.

You still have to code the routes you would remove regardless.... do you think you're going to be injecting and removing routes live in bytecode?

Just keep a map that you can access in some way to disable and enable certain routes.

do you think you're going to be injecting and removing routes live in bytecode?

@dessalines Sometimes yes.

I'm using spark-rest from Apache Camel and having problem removing and adding routes because they can't be removed from Spark.

I think that the discussion here is related to remove routes from Spark, why not?
What kind of problems can happen in Spark implementing this feature?

I also fork this repo and add a method in spark.Spark: removeRoute(String path, String httpMethod) { getInstance().routes.remove(path, httpMethod); } I work on a piece of software that allows endpoint creation through a web interface, and when the logic for that endpoint changes, it's preferred to remove the route and add the new logic than to restart the server which would disrupt work on other routes.

I've submitted a pull request which adds the method mentioned above, but it would be much easier if we didn't have to fork the repo and add this method each time a new version of spark is released. If this should not be implemented directly in the spark.Spark class, I feel there should at least be some method of doing this without restarting the server. Just my two cents...

It is a rare use case, but multiple people seem to have asked for it, so it would be a very welcome addition.

EDIT I just had a commit merged that should fit this use case. This is now available in version 2.9.3 via the Spark.unmap(String route) and Spark.unmap(String route, String httpMethod)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RichoDemus picture RichoDemus  路  7Comments

rbordon-meli picture rbordon-meli  路  5Comments

wowselim picture wowselim  路  5Comments

magynhard picture magynhard  路  4Comments

airinfection picture airinfection  路  3Comments