-
Notifications
You must be signed in to change notification settings - Fork 0
/
native.clj
88 lines (82 loc) · 2.69 KB
/
native.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(ns iny.native
(:require [clojure.tools.logging :as log])
(:import [io.netty.util
Version]
[io.netty.util.internal
PlatformDependent]
[io.netty.channel
MultithreadEventLoopGroup]
; [io.netty.channel.epoll
; Epoll
; EpollEventLoopGroup
; EpollServerSocketChannel]
; [io.netty.channel.kqueue
; KQueue
; KQueueEventLoopGroup
; KQueueServerSocketChannel]
[io.netty.channel.nio
NioEventLoopGroup]
[io.netty.channel.socket.nio
NioServerSocketChannel]))
(defn version-of
[netty-pkg]
(when-let [version ^Version (get (Version/identify) netty-pkg)]
(.artifactVersion version)))
(def epoll?
(memoize
(fn epoll? []
(when (version-of "netty-transport-native-epoll")
;; it might be explicitly turned off
(eval `(io.netty.channel.epoll.Epoll/isAvailable))))))
(def kqueue?
(memoize
(fn epoll? []
(when (version-of "netty-transport-native-kqueue")
;; it might be explicitly turned off
(eval `(io.netty.channel.kqueue.KQueue/isAvailable))))))
(defn suggest-package
([]
(let [os (PlatformDependent/normalizedOs)
arch (PlatformDependent/normalizedArch)
clsfn #(when (% arch) (str os "-" arch))]
(case os
"linux"
(when-not (epoll?)
(let [classifier (clsfn #{"x86_64" "aarch64"})]
(suggest-package "epoll" classifier)))
("darwin" "mac")
(when-not (kqueue?)
(let [classifier (clsfn #{"x86_64"})]
(suggest-package "kqueue" classifier)))
("bsd")
(when-not (kqueue?)
(suggest-package "kqueue" nil))
nil)))
([name classifier]
(let [netty-version (version-of "netty-transport")]
(log/debug
(str "Your system likely supports '" name "'. "
"For better native performance, try adding "
"`[io.netty/netty-transport-native-" name " \"" netty-version "\""
(when classifier (str " :classifier \"" classifier "\""))
"]` to your dependencies.")))))
(defn ^MultithreadEventLoopGroup event-loop
[^long thread-count]
(cond
(epoll?)
(eval `(io.netty.channel.epoll.EpollEventLoopGroup. ~thread-count))
(kqueue?)
(eval `(io.netty.channel.kqueue.KQueueEventLoopGroup. ~thread-count))
:nio?
(NioEventLoopGroup. thread-count)))
(defn ^Class socket-chan
[]
(when-not (or (epoll?) (kqueue?))
(suggest-package))
(cond
(epoll?)
(eval `io.netty.channel.epoll.EpollServerSocketChannel)
(kqueue?)
(eval `io.netty.channel.kqueue.KQueueServerSocketChannel)
:nio?
NioServerSocketChannel))