2015-01-15 35 views
1

“每个进程有100个线程的限制,所以当我超过这个限制时,我得到:java.lang.OutOfMemoryError:无法创建新的本地线程 Compojure正在使用码头环适配器,是否将服务器的配置设置为一次只接受一百个线程到servlet?“Compojure OutOfMemoryError:无法创建新的本机线程

似乎这里是一个简洁的解决方案,但它似乎已经过时:

Limiting the number of threads Compojure spawns

我把这个在我的project.clj:

:dependencies [[org.clojure/clojure "1.6.0"] [compojure "1.1.8"] [ring/ring-json "0.3.1"] [c3p0/c3p0 "0.9.1.2"] [org.clojure/java.jdbc "0.2.3"] [cheshire "5.4.0"] [ring/ring-jetty-adapter "1.1.8"] [environ "0.2.1"] [hiccup "1.0.0"] [com.novemberain/monger "2.0.0"] [org.clojure/data.json "0.2.5"]]

,我把这个在我控制器文件:

(:import [org.mortbay.thread.QueuedThreadPool])

...

(defn -main [] (run-jetty app {:port (if (nil? (System/getenv "PORT")) 8000 ; localhost or heroku? (Integer/parseInt (System/getenv "PORT"))) :configurator #(.setThreadPool % (QueuedThreadPool. 100)) }))

当我尝试,我得到:

异常在线程“主” java.lang.IllegalArgumentException异常:无法解析的类名:QueuedThreadPool,编译:

能否请您给我的建议什么clojure插件注入我的图书馆,以避免惊吓Heroku每100进程限制100线程?

+0

我不相信这是在Heroku 100线程限制。在1X dyno上有256个进程/线程/打开文件限制:https://devcenter.heroku.com/articles/limits#processes-threads – codefinger

回答

1

您似乎在使用过时版本的ring/ring-jetty-adapter(1.1.8),最新版本是1.3.2。

在最新版本中,您可以简单地指定:ring.adapter.jetty/run-jetty选项中的max-threads。

所以要限制由码头使用的线程的最大数量,你可以做到以下几点:

(defn -main [] (run-jetty #'app 
          {:port (Integer/parseInt (or (System/getenv "PORT") "8000")) 
          :max-threads 100})) 

有可能,当然,存在其他线程在你的JVM,这意味着你可能不得不降低分配给码头的线程数量为了不超过总体Heroku限制。

如果你还是想用QueuedThreadPool,那么你就需要使用:的

(:import [org.eclipse.jetty.util.thread QueuedThreadPool]) 

代替

(:import [org.mortbay.thread.QueuedThreadPool]) 
+1

重要的是要注意clojure和jvm跨越10多个线程,所以拥有80码头的线程池可能是合理的。 – ordnungswidrig

相关问题