-
Notifications
You must be signed in to change notification settings - Fork 15
/
lpm.clj
248 lines (212 loc) · 10.7 KB
/
lpm.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
;; Copyright © 2019 State Street Bank and Trust Company. All rights reserved
;;
;; SPDX-License-Identifier: Apache-2.0
(ns protojure.grpc.codec.lpm
"Utility functions for GRPC [length-prefixed-message](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests) encoding."
(:require [clojure.core.async :refer [<! >! go go-loop] :as async]
[promesa.core :as p]
[protojure.protobuf :as pb]
[protojure.grpc.codec.compression :as compression]
[clojure.tools.logging :as log])
(:import (protojure.internal.grpc.io InputStream
OutputStream)
(java.io ByteArrayOutputStream)
(org.apache.commons.io.input BoundedInputStream))
(:refer-clojure :exclude [resolve]))
;;--------------------------------------------------------------------------------------
;; integer serdes used for GRPC framing
;;--------------------------------------------------------------------------------------
(defn- bytes->num
"Deserializes an integer from a byte-array.
Shamelessly borrowed from https://gist.github.com/pingles/1235344"
[data]
(->> data
(map-indexed
(fn [i x]
(bit-shift-left (bit-and x 0x0FF)
(* 8 (- (count data) i 1)))))
(reduce bit-or)))
(defn- num->bytes
"Serializes an integer to a byte-array."
[num]
(byte-array (for [i (range 4)]
(-> (unsigned-bit-shift-right num
(* 8 (- 4 i 1)))
(bit-and 0x0FF)))))
;;======================================================================================
;; GRPC length-prefixed-message (LPM) codec
;;======================================================================================
;;
;; GRPC encodes protobuf messages as:
;;
;; [
;; 1b : compressed? (0 = no, 1 = yes)
;; 4b : length of message
;; Nb : N = length bytes of optionally compressed protobuf
;; <encoded payload>
;; ]
;;
;; Reference:
; https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
;;======================================================================================
;;--------------------------------------------------------------------------------------
;; Decoder
;;--------------------------------------------------------------------------------------
(defn- decoder-stream [is compressed? decompressor]
(if (and compressed? (some? decompressor))
(decompressor is)
is))
(defn- decode-header
"Decodes 5-bytes into a {compressed? len} tuple"
[hdr]
(let [compressed? (first hdr)
lenbuf (rest hdr)]
{:compressed? (pos? compressed?) :len (bytes->num lenbuf)}))
(defn- decode-body
"Decodes a LPM payload based on a previously decoded header (see [[decode-header]])"
[f is {:keys [compressed? len] :as header} options]
(-> (BoundedInputStream. is len)
(decoder-stream compressed? options)
f))
(defn- _decode [f is buf data decompressor]
(let [buf (conj buf data)]
(if (= (count buf) 5)
(let [hdr (decode-header buf)]
[[] (decode-body f is hdr decompressor)])
[buf nil])))
;;--------------------------------------------------------------------------------------------
(defn decode
"
Takes a parsing function, a pair of input/output [core.async channels](https://clojuredocs.org/clojure.core.async/chan), and an optional codec and
decodes a stream of [length-prefixed-message](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests) (LPM).
#### Parameters
| Value | Type | Description |
|-----------------|----------------------|---------------------------------------------------------------------------|
| **f** | _(fn [is])_ | A protobuf decoder function with an arity of 1 that accepts an instance of [InputStream](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html), such as the pb-> functions produced by the protoc-gen-clojure compiler |
| **input** | _core.async/channel_ | A core.async channel that carries LPM encoded bytes, one byte per [(take!)](https://clojuredocs.org/clojure.core.async/take!). Closing the input will shut down the pipeline. |
| **output** | _core.async/channel_ | A core.async channel that carries decoded protobuf records. Will be closed when the pipeline shuts down. |
| **options** | _map_ | See _Options_
##### Options
| Value | Type | Default | Description |
|--------------------|-----------------------------------------------------------------|
| **content-coding** | _string_ | \"identity\" | See _Content-coding_ table |
| **codecs** | _map_ | [[protojure.grpc.codec.compression/builtin-codecs]] | The dictionary of codecs to utilize |
| **tmo** | _unsigned int_ | 5000ms | A timeout, in ms, for receiving the remaining LPM payload bytes once the header has been received. |
###### Example
```
{:content-coding \"gzip\"
:codecs mycodecs
:tmo 1000}
```
##### Content-coding
The value for the **content-coding** option must be one of
| Value | Comment |
|----------------|-------------------------------------------|
| nil | no compression |
| \"identity\" | no compression |
| _other string_ | retrieves the codec from _codecs_ option |
"
[f input output {:keys [codecs content-coding tmo] :or {codecs compression/builtin-codecs tmo 5000} :as options}]
(let [is (InputStream. {:ch input :tmo tmo})
decompressor (when (and (some? content-coding) (not= content-coding "identity"))
(compression/decompressor codecs content-coding))]
(p/promise
(fn [resolve reject]
(go
(try
(loop [acc []]
(if-let [data (<! input)]
(let [[acc msg] (_decode f is acc data decompressor)]
(when (some? msg)
(log/trace "Decoded: " msg)
(>! output msg))
(recur acc))
(resolve :ok)))
(catch Exception e
(reject e))
(finally
(async/close! output))))))))
;;--------------------------------------------------------------------------------------
;; Encoder
;;--------------------------------------------------------------------------------------
(defn- encode-header [os compressed? len]
(.write os (int (if compressed? 1 0)))
(.write os (num->bytes len)))
(defn- encode-uncompressed
([msg os]
(encode-uncompressed msg (pb/length msg) os))
([msg len os]
(encode-header os false len)
(pb/->pb msg os)))
(defn- encode-compressed-buffer [buf len os]
(encode-header os true len)
(.write os buf))
(defn- compress-msg [compressor msg]
(let [os (ByteArrayOutputStream.)
cos (compressor os)]
(pb/->pb msg cos)
(.close cos)
(.toByteArray os)))
(defn- encode-maybe-compressed
"This function will encode the message either with or without compression,
depending on whichever results in the smaller message"
[msg compressor os]
(let [buf (compress-msg compressor msg)
clen (count buf)
len (pb/length msg)]
(if (< clen len)
(encode-compressed-buffer buf clen os)
(encode-uncompressed msg len os))))
;;--------------------------------------------------------------------------------------------
(defn encode
"
Takes an input and output [core.async channel](https://clojuredocs.org/clojure.core.async/chan), along with an optional codec and
encodes a stream of 0 or more [length-prefixed-message](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests)
messages.
#### Parameters
| Value | Type | Description |
|-----------------|----------------------|---------------------------------------------------------------------------|
| **f** | _(fn [init])_ | A protobuf encoder function with an arity of 1 that accepts an init-map, such as the new-XX functions produced by the protoc-gen-clojure compiler |
| **input** | _core.async/channel_ | A core.async channel expected to carry maps that will be transformed to protobuf messages based on _f_ |
| **output** | _core.async/channel_ | A core.async channel that will carry bytes representing the encoded messages. See _max-frame-size_. Will be closed when the pipeline shuts down. |
| **options** | _map_ | See _Options_ |
##### Options
| Value | Type | Default | Description |
|--------------------|---------------------------------------------------------------------------------------------------------------|
| **content-coding** | _String_ | \"identity\" | See the _content-coding_ section of [[decode]] |
| **codecs** | _map_ | [[protojure.grpc.codec.compression/builtin-codecs]] | The dictionary of codecs to utilize |
| **max-frame-size** | _UInt32_ | 0 | Maximum frame-size emitted on _output_ channel. See _Output channel details_ below |
##### Output channel details
The _max-frame-size_ option dictates how bytes are encoded on the _output_ channel:
- **0**: (Default) Indicates 'stream' mode: Bytes are encoded one byte per [(put!)](https://clojuredocs.org/clojure.core.async/put!).
- **>0**: Indicates 'framed' mode: The specified value will dictate the upper bound on byte-array based frames emitted to the output channel.
###### Example
```
{:content-coding \"gzip\"
:codecs mycodecs
:max-frame-size 16384}
```
"
[f input output {:keys [codecs content-coding max-frame-size] :or {codecs compression/builtin-codecs} :as options}]
(let [os (OutputStream. {:ch output :max-frame-size max-frame-size})
compressor (when (and (some? content-coding) (not= content-coding "identity"))
(compression/compressor codecs content-coding))]
(p/promise
(fn [resolve reject]
(go
(try
(loop []
(if-let [_msg (<! input)]
(do
(log/trace "Encoding: " _msg)
(let [msg (f _msg)]
(if (some? compressor)
(encode-maybe-compressed msg compressor os)
(encode-uncompressed msg os))
(.flush os)
(recur)))
(resolve :ok)))
(catch Exception e
(reject e))
(finally
(.close os))))))))