|
10 | 10 | [io.netty.channel.nio NioEventLoopGroup]
|
11 | 11 | [io.netty.channel.socket.nio
|
12 | 12 | 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))) |
15 | 17 |
|
16 | 18 | (defn to-channel-option
|
17 | 19 | ([co]
|
|
67 | 69 | :else
|
68 | 70 | (append-handlers->pipeline pipeline [h]))))))))
|
69 | 71 |
|
| 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 | + |
70 | 82 | (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)) |
73 | 85 | bootstrap (or (:bootstrap options) (ServerBootstrap.))
|
74 | 86 |
|
75 | 87 | channel-initializer (channel-init handlers)
|
76 | 88 |
|
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."))) |
78 | 92 | parent-options (get options false)
|
79 | 93 | child-options (map #(vector (keyword (subs (name (% 0)) 6)) (% 1)) (get options true))]
|
80 | 94 | (doto bootstrap
|
|
113 | 127 | "Allow multiple server instance share the same eventloop:
|
114 | 128 | Just use the result of this function as option in `tcp-server`"
|
115 | 129 | []
|
116 |
| - {:boss-group (NioEventLoopGroup.) |
117 |
| - :worker-group (NioEventLoopGroup.) |
| 130 | + {:boss-group (default-nio-boss-group) |
| 131 | + :worker-group (default-nio-worker-group) |
118 | 132 | :boostrap (ServerBootstrap.)})
|
119 | 133 |
|
120 | 134 | (defn tcp-client-factory [handlers
|
121 | 135 | & {:keys [options]
|
122 | 136 | :or {options {}}}]
|
123 |
| - (let [worker-group (NioEventLoopGroup.) |
| 137 | + (let [worker-group (or (:worker-group options) (default-nio-worker-group)) |
124 | 138 | bootstrap (Bootstrap.)
|
125 | 139 | handlers (cond
|
126 | 140 | (fn? handlers) handlers
|
127 | 141 | (sequential? handlers) handlers
|
128 | 142 | :else [handlers])
|
129 | 143 |
|
130 | 144 | channel-initializer (channel-init handlers)
|
131 |
| - options (into [] options)] |
| 145 | + options (->> (dissoc options :worker-group) |
| 146 | + (into []))] |
132 | 147 |
|
133 | 148 | (doto bootstrap
|
134 | 149 | (.group worker-group)
|
135 | 150 | (.channel NioSocketChannel)
|
136 | 151 | (.handler channel-initializer))
|
137 |
| - (doseq [op (into [] options)] |
| 152 | + (doseq [op options] |
138 | 153 | (let [op (flatten op)]
|
139 | 154 | (.option bootstrap (apply to-channel-option (butlast op)) (last op))))
|
140 | 155 |
|
|
0 commit comments