Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make brotli and zstd optional again #723

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,7 @@
io.netty/netty-resolver {:mvn/version "4.1.108.Final"},
io.netty/netty-resolver-dns {:mvn/version "4.1.108.Final"},
metosin/malli
{:mvn/version "0.15.0", :exclusions [org.clojure/clojure]},
com.aayushatharva.brotli4j/brotli4j {:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/service {:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-linux-aarch64
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-linux-armv7
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-linux-x86_64
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-osx-aarch64
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-osx-x86_64 {:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-windows-x86_64
{:mvn/version "1.16.0"},
com.github.luben/zstd-jni {:mvn/version "1.5.6-2"}},
{:mvn/version "0.15.0", :exclusions [org.clojure/clojure]}},
:aliases
{:lein2deps
{:deps
Expand Down
24 changes: 12 additions & 12 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@
[io.netty/netty-handler-proxy ~netty-version]
[io.netty/netty-resolver ~netty-version]
[io.netty/netty-resolver-dns ~netty-version]
[metosin/malli "0.15.0" :exclusions [org.clojure/clojure]]
;;[com.aayushatharva.brotli4j/all ~brotli-version]
[com.aayushatharva.brotli4j/brotli4j ~brotli-version]
[com.aayushatharva.brotli4j/service ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-armv7 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-windows-x86_64 ~brotli-version]
[com.github.luben/zstd-jni "1.5.6-2"]]
[metosin/malli "0.15.0" :exclusions [org.clojure/clojure]]]
:profiles {:dev {:dependencies [[criterium "0.4.6"]
[cheshire "5.13.0"]
[org.slf4j/slf4j-simple "2.0.12"]
Expand All @@ -47,7 +37,17 @@
[org.bouncycastle/bcprov-jdk18on "1.77"]
[org.bouncycastle/bcpkix-jdk18on "1.77"]
;;[org.bouncycastle/bctls-jdk18on "1.75"]
[io.netty/netty-tcnative-boringssl-static "2.0.65.Final"]]
[io.netty/netty-tcnative-boringssl-static "2.0.65.Final"]
;;[com.aayushatharva.brotli4j/all ~brotli-version]
[com.aayushatharva.brotli4j/brotli4j ~brotli-version]
[com.aayushatharva.brotli4j/service ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-armv7 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-windows-x86_64 ~brotli-version]
[com.github.luben/zstd-jni "1.5.6-2"]]
:jvm-opts ["-Dorg.slf4j.simpleLogger.defaultLogLevel=debug"
"-Dorg.slf4j.simpleLogger.showThreadName=false"
"-Dorg.slf4j.simpleLogger.showThreadId=true"
Expand Down
42 changes: 42 additions & 0 deletions src-java/aleph/http/AlephCompressionOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package aleph.http;

import io.netty.handler.codec.compression.BrotliOptions;
import io.netty.handler.codec.compression.DeflateOptions;
import io.netty.handler.codec.compression.GzipOptions;
import io.netty.handler.codec.compression.SnappyOptions;
import io.netty.handler.codec.compression.StandardCompressionOptions;
import io.netty.handler.codec.compression.ZstdOptions;

/**
* {@link AlephCompressionOptions} exists because the Clojure compiler cannot
* distinguish between static fields and static methods without reflection.
*
* This is a problem when using Netty's StandardCompressionOptions, because
* reflection triggers a load of all the methods referencing optional classes,
* which may not exist in the classpath, resulting in a ClassNotFoundException.
*/
public class AlephCompressionOptions {
private AlephCompressionOptions() {
// Prevent outside initialization
}

public static BrotliOptions brotli() {
return StandardCompressionOptions.brotli();
}

public static ZstdOptions zstd() {
return StandardCompressionOptions.zstd();
}

public static SnappyOptions snappy() {
return StandardCompressionOptions.snappy();
}

public static GzipOptions gzip() {
return StandardCompressionOptions.gzip();
}

public static DeflateOptions deflate() {
return StandardCompressionOptions.deflate();
}
}
48 changes: 27 additions & 21 deletions src/aleph/http/compression.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
(ns ^:no-doc aleph.http.compression
"Currently focused on HTTP/2, since Netty offers better support for
compression in its HTTP/1 code.
"Currently only for HTTP/2, since Netty offers better support for
compression in HTTP/1 code.

Best supported compression codecs on the web are Brotli, gzip, and deflate.

Snappy is primarily an internal Google codec, but is supported by some
open-source databases. It's not on track to be a web standard, but is
well-supported by Netty.

Zstd is a promising Facebook codec that is registered with IANA, but is not
yet widely available. (See https://caniuse.com/zstd).
Zstd is a Facebook codec that is registered with IANA, but is not yet
widely available. (See https://caniuse.com/zstd).

See https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding"
(:require
[aleph.netty :as netty]
[clj-commons.primitive-math :as p]
[clojure.tools.logging :as log])
(:import
(aleph.http AlephCompressionOptions)
(io.netty.channel ChannelHandler)
(io.netty.handler.codec.compression
Brotli
Expand All @@ -25,9 +26,7 @@
DeflateOptions
GzipOptions
SnappyOptions
StandardCompressionOptions
Zstd
ZstdOptions)
Zstd ZstdOptions)
(io.netty.handler.codec.http HttpHeaderNames)
(io.netty.handler.codec.http2 Http2HeadersFrame)
(io.netty.util AsciiString)
Expand All @@ -38,10 +37,6 @@
(def ^:private ^AsciiString head-method (AsciiString. "HEAD"))
(def ^:private ^AsciiString connect-method (AsciiString. "CONNECT"))

;; From 0.7.0-rc2 on, Brotli and Zstd should be available by default
(Brotli/ensureAvailability)
(Zstd/ensureAvailability)

(defn- contains-class?
"Returns true if the class is in the array"
[^"[Lio.netty.handler.codec.compression.CompressionOptions;" a ^Class klazz]
Expand All @@ -57,11 +52,11 @@
available-compressor-options
"A Java array of all available compressor options"
(into-array CompressionOptions
[(StandardCompressionOptions/brotli)
(StandardCompressionOptions/deflate)
(StandardCompressionOptions/gzip)
(StandardCompressionOptions/zstd)
(StandardCompressionOptions/snappy)]))
(cond-> [(AlephCompressionOptions/deflate)
(AlephCompressionOptions/gzip)
(AlephCompressionOptions/snappy)]
(Brotli/isAvailable) (conj (AlephCompressionOptions/brotli))
(Zstd/isAvailable) (conj (AlephCompressionOptions/zstd)))))


(defn- qvalue
Expand All @@ -82,6 +77,16 @@

(defrecord Qvals [^double star ^double br ^double snappy ^double zstd ^double gzip ^double deflate])

;; We can't reference these options classes directly in `choose-codec` since the compiler would try
;; to initialize them even when the necessary dependencies aren't available, resulting in an
;; error. Indirectly referencing them like this works, though. See
;; https://github.com/clj-commons/aleph/issues/703
(def brotli-options-class
BrotliOptions)

(def zstd-options-class
ZstdOptions)
DerGuteMoritz marked this conversation as resolved.
Show resolved Hide resolved

(defn choose-codec
"Based on Netty's algorithm, which only compares with the next-best option.
E.g., Brotli's q-value is only compared with zstd, not gzip or deflate.
Expand All @@ -98,12 +103,12 @@
;; some encodings were listed
(cond (and (p/not== br -1.0)
(p/>= br zstd)
(contains-class? compressor-options BrotliOptions))
(contains-class? compressor-options brotli-options-class))
"br"

(and (p/not== zstd -1.0)
(p/>= zstd snappy)
(contains-class? compressor-options ZstdOptions))
(contains-class? compressor-options zstd-options-class))
"zstd"

(and (p/not== snappy -1.0)
Expand All @@ -122,10 +127,12 @@

;; no named encodings were listed, so we'll apply *'s qval to unset ones
(p/> star 0.0)
(cond (p/== br -1.0)
(cond (and (p/== br -1.0)
(Brotli/isAvailable))
"br"

(p/== zstd -1.0)
(and (p/== zstd -1.0)
(Zstd/isAvailable))
"zstd"

(p/== snappy -1.0)
Expand Down Expand Up @@ -219,7 +226,6 @@
(p/== 204 status)
(p/== 304 status)))
(log/debug "Setting content-encoding to:" @encoding)
;; TODO: add "vary" header
(.set headers HttpHeaderNames/CONTENT_ENCODING chosen-encoding))))))

(.write ctx msg promise))))))