Fork me on GitHub
#pedestal
<
2020-11-20
>
simongray09:11:48

Is there some obvious way to implement rate-limiting for a Pedestal web service?

Louis Kottmann10:11:19

with a proxy like Nginx or Kong?

simongray10:11:44

@lkottmann no proxy at the moment. I don't expect I'll need one, though if that's the preferred way to get rate limiting I guess I will?

Louis Kottmann10:11:31

there are ring middlewares if you want a clojure-only solution

simongray12:11:17

I guess an alternative to using ring-middleware in a Pedestal app is to use one these Jetty filters: • https://www.eclipse.org/jetty/documentation/current/dos-filter.htmlhttps://www.eclipse.org/jetty/documentation/current/qos-filter.html I would want to do it from Clojure, though, not through XML. Anyone have any experience setting up Jetty filters in Pedestal?

simongray12:11:41

Perhaps using the `:context-configurator` key in the Jetty configuration and code similar to this? https://www.programcreek.com/java-api-examples/?class=org.eclipse.jetty.servlet.ServletContextHandler&amp;method=addFilter I have no idea how to use `:context-configurator` though. The Pedestal documentation simply says: > “A function called with the `org.eclipse.jetty.servlet.ServletContextHandler` instance. Use when advanced customization is required.”

simongray12:11:03

Scratch that, I got confused. `:context-configurator` is a just function you define taking one arg (the current org.eclipse.jetty.servlet.ServletContextHandler) and obviously methods can then be called on it in that context.

souenzzo12:11:58

Here how I use context-configurator

(defn context-configurator
  [^ServletContextHandler context]
  (let [gzip-handler (GzipHandler.)]
    (.setExcludedAgentPatterns gzip-handler (make-array String 0))
    (.setGzipHandler context gzip-handler))
  context)

... ::http/container-options {:h2c?                 true
                              :context-configurator context-configurator}

👍 3
simongray12:11:15

Thanks. Pretty simple stuff.