Skip to content

Commit 47d9082

Browse files
authored
Merge pull request #46 from clojure-link/fix/group-config
去掉使用 boss-group 和 worker-group 时的 warning
2 parents 6df7854 + 417fa13 commit 47d9082

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

project.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(def netty-version "4.1.32.Final")
1+
(def netty-version "4.1.34.Final")
22

33
(def example-base-command
44
["trampoline" "with-profile" "default,example" "run" "-m"])
@@ -8,7 +8,7 @@
88
:url "http://github.com/sunng87/link"
99
:license {:name "Eclipse Public License - v 1.0"
1010
:url "http://www.eclipse.org/legal/epl-v10.html"}
11-
:dependencies [[org.clojure/clojure "1.9.0"]
11+
:dependencies [[org.clojure/clojure "1.10.0"]
1212
[io.netty/netty-buffer ~netty-version]
1313
[io.netty/netty-codec-http ~netty-version]
1414
[io.netty/netty-codec-http2 ~netty-version]

src/link/tcp.clj

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
[io.netty.channel.nio NioEventLoopGroup]
1111
[io.netty.channel.socket.nio
1212
NioServerSocketChannel NioSocketChannel]
13-
[io.netty.util.concurrent EventExecutorGroup]
14-
[link.core ClientSocketChannel]))
13+
[io.netty.util.concurrent EventExecutorGroup DefaultThreadFactory]
14+
[link.core ClientSocketChannel]
15+
(io.netty.util.internal SystemPropertyUtil)
16+
(io.netty.util NettyRuntime)))
1517

1618
(defn to-channel-option
1719
([co]
@@ -67,14 +69,26 @@
6769
:else
6870
(append-handlers->pipeline pipeline [h]))))))))
6971

72+
(defn- default-nio-boss-group []
73+
(let [group (NioEventLoopGroup. 1 (DefaultThreadFactory. "link-nio-boss-group"))]
74+
(.setIoRatio group 100)
75+
group))
76+
77+
(defn- default-nio-worker-group []
78+
(NioEventLoopGroup. (max 1 (SystemPropertyUtil/getInt "io.netty.eventLoopThreads"
79+
(* 2 (NettyRuntime/availableProcessors))))
80+
(DefaultThreadFactory. "link-nio-worker-group")))
81+
7082
(defn- start-tcp-server [host port handlers options]
71-
(let [boss-group (or (:boss-group options) (NioEventLoopGroup.))
72-
worker-group (or (:worker-group options) (NioEventLoopGroup.))
83+
(let [boss-group (or (:boss-group options) (default-nio-boss-group))
84+
worker-group (or (:worker-group options) (default-nio-worker-group))
7385
bootstrap (or (:bootstrap options) (ServerBootstrap.))
7486

7587
channel-initializer (channel-init handlers)
7688

77-
options (group-by #(.startsWith (name (% 0)) "child.") (into [] options))
89+
options (->> (dissoc options :boss-group :worker-group :bootstrap)
90+
(into [])
91+
(group-by #(.startsWith (name (% 0)) "child.")))
7892
parent-options (get options false)
7993
child-options (map #(vector (keyword (subs (name (% 0)) 6)) (% 1)) (get options true))]
8094
(doto bootstrap
@@ -113,28 +127,29 @@
113127
"Allow multiple server instance share the same eventloop:
114128
Just use the result of this function as option in `tcp-server`"
115129
[]
116-
{:boss-group (NioEventLoopGroup.)
117-
:worker-group (NioEventLoopGroup.)
130+
{:boss-group (default-nio-boss-group)
131+
:worker-group (default-nio-worker-group)
118132
:boostrap (ServerBootstrap.)})
119133

120134
(defn tcp-client-factory [handlers
121135
& {:keys [options]
122136
:or {options {}}}]
123-
(let [worker-group (NioEventLoopGroup.)
137+
(let [worker-group (or (:worker-group options) (default-nio-worker-group))
124138
bootstrap (Bootstrap.)
125139
handlers (cond
126140
(fn? handlers) handlers
127141
(sequential? handlers) handlers
128142
:else [handlers])
129143

130144
channel-initializer (channel-init handlers)
131-
options (into [] options)]
145+
options (->> (dissoc options :worker-group)
146+
(into []))]
132147

133148
(doto bootstrap
134149
(.group worker-group)
135150
(.channel NioSocketChannel)
136151
(.handler channel-initializer))
137-
(doseq [op (into [] options)]
152+
(doseq [op options]
138153
(let [op (flatten op)]
139154
(.option bootstrap (apply to-channel-option (butlast op)) (last op))))
140155

src/link/threads.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
([threads] (DefaultEventExecutorGroup. threads))
77
([threads thread-factory] (DefaultEventExecutorGroup. threads thread-factory)))
88

9-
(defn prefix-thread-factory [name-prefix]
9+
(defn ^ThreadFactory prefix-thread-factory [name-prefix]
1010
(let [counter (atom 0)]
1111
(reify ThreadFactory
1212
(newThread [this r]

0 commit comments

Comments
 (0)