-
Notifications
You must be signed in to change notification settings - Fork 9
/
specs.clj
294 lines (257 loc) · 10.2 KB
/
specs.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
(ns goose.specs
(:require
[goose.broker :as b]
[goose.client :as c]
[goose.cron.parsing :as cron-parsing]
[goose.defaults :as d]
[goose.metrics :as m]
[goose.utils :as u]
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[clojure.string :as str]
[taoensso.carmine.connections :refer [IConnectionPool]])
(:import
(java.time Instant ZoneId)
(java.util Arrays)))
;;; ========== Qualified Function Symbols ==============
(s/def ::fn-sym (s/and qualified-symbol? resolve #(fn? @(resolve %))))
;;; ========== Redis ==============
(s/def :goose.specs.redis/url string?)
(s/def :goose.specs.redis/pool-opts
(s/or :nil nil?
:none #{:none}
:map map?
:iconn-pool #(satisfies? IConnectionPool %)))
(s/def ::redis-conn-opts
(s/keys :req-un [:goose.specs.redis/url]
:opt-un [:goose.specs.redis/pool-opts]))
(s/def ::redis-scheduler-polling-interval-sec (s/int-in 1 61))
;;; ========== RabbitMQ ==============
(s/def :goose.specs.rmq/uri string?)
(s/def :goose.specs.rmq/host string?)
(s/def :goose.specs.rmq/port int?)
(s/def :goose.specs.rmq/username string?)
(s/def :goose.specs.rmq/password string?)
(s/def :goose.specs.rmq/vhost string?)
;;; A non-exhaustive list of RabbitMQ settings.
;;; Full list of settings can be found here:
;;; http://clojurerabbitmq.info/articles/connecting.html
(s/def :goose.specs.rmq/settings
(s/keys :opt-un [:goose.specs.rmq/uri
:goose.specs.rmq/host
:goose.specs.rmq/port
:goose.specs.rmq/username
:goose.specs.rmq/password
:goose.specs.rmq/vhost]))
(s/def :goose.specs.sync/strategy #{d/sync-confirms})
(s/def ::timeout-ms pos-int?)
(s/def ::retry-delay-ms pos-int?)
(s/def ::sync-strategy
(s/keys :req-un [:goose.specs.sync/strategy ::timeout-ms]
:opt-un [::max-retries ::retry-delay-ms]))
(s/def :goose.specs.async/strategy #{d/async-confirms})
(s/def ::ack-handler fn?)
(s/def ::nack-handler fn?)
(s/def ::async-strategy
(s/keys :req-un [:goose.specs.async/strategy ::ack-handler ::nack-handler]))
(s/def :goose.specs.classic/type #{d/rmq-classic-queue})
(s/def ::classic-queue
(s/keys :req-un [:goose.specs.classic/type]))
(s/def :goose.specs.quorum/type #{d/rmq-quorum-queue})
(s/def ::replication-factor pos-int?)
(s/def ::quorum-queue
(s/keys :req-un [:goose.specs.quorum/type ::replication-factor]))
(s/def :goose.specs.rmq/queue-type
(s/or :classic ::classic-queue
:quorum ::quorum-queue))
(s/def ::publisher-confirms
(s/or :sync ::sync-strategy
:async ::async-strategy))
(s/def ::return-listener fn?)
(s/def ::shutdown-listener fn?)
(s/def ::rmq
(s/keys :req-un [:goose.specs.rmq/settings
:goose.specs.rmq/queue-type
::publisher-confirms
::return-listener
::shutdown-listener]))
;;; ============== Brokers ==============
(s/def ::broker #(satisfies? b/Broker %))
;;; ============== Queue ==============
(defn- unprefixed? [queue] (not (str/starts-with? queue d/queue-prefix)))
(defn- not-protected? [queue] (not (str/includes? d/protected-queues queue)))
;;; RMQ queue names cannot be longer than 255 bytes.
(s/def ::queue (s/and string? #(< (count %) 200) unprefixed? not-protected?))
;;; ============== Cron Opts ==============
(s/def ::cron-name string?)
(s/def ::cron-schedule (s/and string? cron-parsing/valid-cron?))
(s/def ::timezone (set (ZoneId/getAvailableZoneIds)))
(s/def ::cron-opts
(s/keys :req-un [::cron-name
::cron-schedule]
:opt-un [::timezone]))
;;; ============== Batch ==============
(s/def ::callback-fn-sym
(s/and ::fn-sym #(some #{2} (u/arities %))))
(s/def ::linger-sec nat-int?)
(s/def ::batch-opts
(s/keys :req-un [::callback-fn-sym ::linger-sec]))
(s/def ::batch-args (s/and sequential? #(every? sequential? %) (s/* ::args-serializable?)))
;;; ============== Retry Opts ==============
(s/def ::max-retries nat-int?)
(s/def ::retry-queue (s/nilable ::queue))
(s/def ::retry-delay-sec-fn-sym
(s/and ::fn-sym #(some #{1} (u/arities %))))
(s/def ::error-handler-fn-sym
(s/and ::fn-sym #(some #{3} (u/arities %))))
(s/def ::death-handler-fn-sym
(s/and ::fn-sym #(some #{3} (u/arities %))))
(s/def ::skip-dead-queue boolean?)
(s/def ::retry-opts
(s/and
(s/map-of #{:max-retries
:retry-delay-sec-fn-sym
:retry-queue
:error-handler-fn-sym
:death-handler-fn-sym
:skip-dead-queue}
any?)
(s/keys :req-un [::max-retries
::retry-delay-sec-fn-sym
::error-handler-fn-sym
::death-handler-fn-sym
::skip-dead-queue]
:opt-un [::retry-queue])))
;;; ============== StatsD Metrics ==============
(s/def :goose.specs.statsd/enabled? boolean?)
(s/def :goose.specs.statsd/host string?)
(s/def :goose.specs.statsd/port pos-int?)
(s/def :goose.specs.statsd/prefix string?)
(s/def :goose.specs.statsd/sample-rate double?)
(s/def :goose.specs.statsd/tags map?)
(s/def ::statsd-opts
(s/keys :req-un [:goose.specs.statsd/enabled?]
:opt-un [:goose.specs.statsd/host
:goose.specs.statsd/port
:goose.specs.statsd/prefix
:goose.specs.statsd/tags
:goose.specs.statsd/sample-rate]))
;;; ============== Client ==============
(s/def ::args-serializable?
;; Serializability of args is determined by consistency in encoding.
;; If this spec fails, serialize your custom data type as follows:
;; https://github.com/nilenso/goose/wiki/Serializing-Custom-data-types
#(try (let [encoding (u/encode %)
re-encoding (u/encode (u/decode encoding))]
(Arrays/equals ^"[B" encoding ^"[B" re-encoding))
(catch Exception _ false)))
(s/def ::instant #(instance? Instant %))
(s/def ::client-opts (s/keys :req-un [::broker ::queue ::retry-opts]))
;;; ============== Worker ==============
(s/def ::threads pos-int?)
(s/def ::graceful-shutdown-sec pos-int?)
(s/def ::metrics-plugin #(satisfies? m/Metrics %))
(s/def ::middlewares fn?)
(s/def ::error-service-config any?) ; This varies by error services.
(s/def ::worker-opts
(s/keys :req-un [::broker
::threads
::queue
::graceful-shutdown-sec]
:opt-un [::middlewares
::error-service-config
::metrics-plugin]))
;;; ============== FDEFs ==============
(s/fdef c/perform-async
:args (s/cat :opts ::client-opts
:execute-fn-sym ::fn-sym
:args (s/* ::args-serializable?))
:ret map?)
(s/fdef c/perform-at
:args (s/cat :opts ::client-opts
:instant ::instant
:execute-fn-sym ::fn-sym
:args (s/* ::args-serializable?))
:ret map?)
(s/fdef c/perform-in-sec
:args (s/cat :opts ::client-opts
:sec int?
:execute-fn-sym ::fn-sym
:args (s/* ::args-serializable?))
:ret map?)
(s/fdef c/perform-every
:args (s/cat :opts ::client-opts
:cron-opts ::cron-opts
:execute-fn-sym ::fn-sym
:args (s/* ::args-serializable?))
:ret map?)
(s/fdef c/perform-batch
:args (s/cat :opts ::client-opts
:batch-opts ::batch-opts
:execute-fn-sym ::fn-sym
:args ::batch-args)
:ret map?)
(def ^:private fns-with-specs
[`c/perform-async
`c/perform-at
`c/perform-in-sec
`c/perform-every
`c/perform-batch])
(defn instrument
"Instruments frequently-called functions.\\
By default, Instrumentation is disabled.\\
It is recommended to enable Specs in Development & Staging.\\
Disabling Specs in Production has a 40% performance improvement.\\
Only disable Specs in Production after thorough testing in Staging."
[]
(st/instrument fns-with-specs))
(defn unstrument
"Disables instrumentation of frequently-called functions."
[]
(st/unstrument fns-with-specs))
(defn- assert-specs
[ns-fn spec x]
(when-let [fail (s/explain-data spec x)]
(throw (ex-info (format "Call to %s did not conform to spec." ns-fn) fail))))
;;; Single-use functions have manual assertions.
;;; (s/fdef) is declared redundantly for
;;; the purpose of `clojure.repl/doc`.
(s/fdef goose.brokers.redis.broker/new-producer
:args (s/cat :redis ::redis-conn-opts)
:ret ::broker)
(defn ^:no-doc assert-redis-producer [conn-opts]
(assert-specs 'goose.brokers.redis.broker/new-producer ::redis-conn-opts conn-opts))
(s/fdef goose.brokers.redis.broker/new-consumer
:args (s/alt :one (s/cat :redis ::redis-conn-opts)
:two (s/cat :redis ::redis-conn-opts
:scheduler-polling-interval-sec ::redis-scheduler-polling-interval-sec))
:ret ::broker)
(defn ^:no-doc assert-redis-consumer
[conn-opts scheduler-polling-interval-sec]
(assert-specs 'goose.brokers.redis.broker/new-consumer ::redis-conn-opts conn-opts)
(assert-specs 'goose.brokers.redis.broker/new-consumer
::redis-scheduler-polling-interval-sec
scheduler-polling-interval-sec))
(s/fdef goose.brokers.rmq.broker/new-producer
:args (s/alt :one (s/cat :opts ::rmq)
:two (s/cat :opts ::rmq
:channels pos-int?))
:ret ::broker)
(defn ^:no-doc assert-rmq-producer
[opts channels]
(assert-specs 'goose.brokers.rmq.broker/new-producer ::rmq opts)
(assert-specs 'goose.brokers.rmq.broker/new-producer pos-int? channels))
(s/fdef goose.brokers.rmq.broker/new-consumer
:args (s/cat :opts ::rmq)
:ret ::broker)
(defn ^:no-doc assert-rmq-consumer [opts]
(assert-specs 'goose.brokers.rmq.broker/new-consumer ::rmq opts))
(s/fdef goose.metrics.statsd/new
:args (s/cat :opts ::statsd-opts)
:ret ::metrics-plugin)
(defn ^:no-doc assert-statsd [opts]
(assert-specs 'goose.metrics.statsd/new ::statsd-opts opts))
(s/fdef goose.worker/start
:args (s/cat :opts ::worker-opts))
(defn ^:no-doc assert-worker [opts]
(assert-specs 'goose.worker/start ::worker-opts opts))