Fork me on GitHub
#pedestal
<
2023-07-31
>
Shekhar11:07:05

How can I configure Pedestal to prevent active HTTP connections from being terminated during shutdown? Any suggestions would be greatly appreciated.

rolt13:07:21

i'm surprised jetty does no perform a graceful shutdown by default. You could try setting the stopTimeout (setStopTimeout here: https://github.com/pedestal/pedestal/blob/9b02e4c4a9b87c718ed7f3f7bcdab032e84336a7/jetty/src/io/pedestal/http/jetty.clj#L195) to see if that helps ? https://eclipse.dev/jetty/documentation/jetty-9/index.html#jetty-connectors

rolt13:07:20

To perform a graceful shutdown of Jetty, the stats https://eclipse.dev/jetty/documentation/jetty-9/index.html#startup-modules must be enabled apparently you need both the stats module and a positive value for stopTimeout (and a recent version of jetty 9.4) stopTimeout should be 30s by default, so maybe just adding the stats module would fix the issue ? (it's hard to distinguish what applies to the embedded version or to the application version, seems like it's null by default) edit: from the doc

// === jetty-stats.xml ===
        StatisticsHandler stats = new StatisticsHandler();
        stats.setHandler(server.getHandler());
        server.setHandler(stats);
        server.addBeanToAllConnectors(new ConnectionStatistics());

Shekhar15:08:26

Thanks @U02F0C62TC1 I will try this

rolt12:08:14

I tried it and simply adding stats made it work. So no need to set stopTimeout if the default value is good enough for you

(defn add-stats
  [m]
  (let [^org.eclipse.jetty.server.Server server (::server/server m)
        stats (new org.eclipse.jetty.server.handler.StatisticsHandler)]
    (.setHandler stats (.getHandler server))
    (.setHandler server stats)
    m))

rolt13:08:23

also: I did not manage to make the stopTimeout value work so I don't know how to properly set it. Or maybe it can't be lower than other timeout values (connection, socket, etc) perhaps it will be clearer jetty 10/11 (i tried to set it on the ServerConnector instances in create-server, no errors but my server did not stop early if I set a low timeout value, it still waited for my handler to respond)

Shekhar13:08:07

Are you invoking the add-stats function after starting the server?

rolt13:08:37

after creating the server, before starting it

rolt13:08:16

from the pedestal-service template:

(-> [...] 
     server/create-server
      add-stats
      server/start)

Shekhar13:08:22

Thank you so much

Shekhar08:08:15

I did manage to increase or decrease timeout @U02F0C62TC1

(defn add-stats
  [{:keys [::server/server] :as service-map}]
  (let [stats (StatisticsHandler.)]
    (.setStopTimeout server 20000)
    (.setHandler stats (.getHandler server))
    (.setHandler server stats)
    service-map))

Shekhar08:08:58

The statistic handler can be also configured via container-options like

(defn jetty-configurator
  "Add statistics handler in Jetty server via `container-options` of the
  pedestal."
  [server]
  (let [stats (StatisticsHandler.)]
    (.setStopTimeout server 60000)
    (.setHandler stats (.getHandler server))
    (.setHandler server stats))
  server)
And from pedestal-service template:
(-> service-map
    (assoc ::http/container-options {:configurator jetty-configurator})
    http/create-server
    http/start)

rolt08:08:21

nice that's way cleaner

rolt08:08:27

it's so weird because in the doc they write about the stopTimeout in the server connector section, not the server one