                                  (get-server-thread-pool-queue server)))
         "Unexpected default for 'queue-max-size'")))
 
-(def threads-per-connector
+(defn required-threads-for-sized-threadpool
+  [threadpool-size]
   "The total number of threads needed per attached connector."
-  (+ acceptor-thread-count selector-thread-count))
+  (+ (reserved-thread-count threadpool-size)
+     (selector-thread-count threadpool-size)
+     acceptor-thread-count))
 
 (defn calculate-minimum-required-threads
-  "Calculate the minimum number threads that a single Jetty Server instance
-  needs.  See: https://github.com/eclipse/jetty.project/blob/jetty-9.4.1.v20170120/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java#L384-L414"
-  [connectors]
-  (+ 1 (* connectors threads-per-connector)))
+  "Jetty calculates the minimum number of required threads based on the the
+  size of the machine it runs on and the number of threads in its thread pool.
+  The smaller we shrink the thread pool the fewer threads it will request.
+  This logic doesn't scale as the thread pool grows beyond 10 threads but
+  allows us to come up with a number that works for the max threads that isn't
+  self referential."
+  [num-connectors]
+  (+ 2 (* num-connectors 2)))
 
 (deftest default-min-threads-settings-test
   ;; This test just exists to validate the advice we give for the bare

  ;; at most one encrypted port connector.  Because of this, the test only
  ;; validates the min-threads behavior for a server that has either one or
  ;; two connectors.
  (letfn [(get-server [max-threads connectors]
            (let [server (Server. (QueuedThreadPool. max-threads))]
              (dotimes [_ connectors]
                (.addConnector server (ServerConnector. server)))
              server))
          (insufficient-threads-msg [server]
            (let [connectors (count (.getConnectors server))]
              (re-pattern (str "Insufficient threads: max="
                               (get-max-threads-for-server server)
                               " < needed\\(acceptors="
                               (* acceptor-thread-count connectors)
                               " \\+ selectors="
                               (* selector-thread-count connectors)
                               " \\+ request=1\\)"))))]
    (dotimes [x 2]
      (let [connectors       (inc x)
            required-threads (calculate-minimum-required-threads connectors)]
        (testing (str "server with too few threads for " connectors " connector(s) "
                      "fail(s) to start with expected error")
          (let [server (-> required-threads
                           dec
                           (get-server connectors))]
            (is (thrown-with-msg? IllegalStateException
                                  (insufficient-threads-msg server)
                                  (tk-log-testutils/with-test-logging
                                   (.start server))))))
        (testing (str "server with minimum required threads for " connectors
                      "connector(s) start(s) successfully")
          (let [server (get-server required-threads connectors)]
            (try
              (.start server)
              (is (.isStarted server))
              (finally
                (.stop server)))))))))

          "Unexpected default for proxy 'idle-timeout'")
      (.stop client))))

(defn selector-thread-count
  [max-threads]
  "The number of selectors and selecto threads that should be allocated per
  connector.
   https://github.com/eclipse/jetty.project/blob/jetty-9.4.11.v20180605/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java#L70-L74"
  (max 1
       (min (int (/ max-threads 16))
            (int (/ (ks/num-cpus) 2)))))

(def acceptor-thread-count
  "The number of acceptor threads that should be allocated per connector.  See:
   https://github.com/eclipse/jetty.project/blob/jetty-9.4.11.v20180605/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java#L202"
  (max 1 (min 4 (int (/ (ks/num-cpus) 8)))))

(defn reserved-thread-count
  [max-threads]
  (max 1
       (min (ks/num-cpus)
            (int (/ max-threads 10)))))

(deftest default-connector-settings-test
  (let [connector (ServerConnector. (Server.))]
