|
35 | 35 | import com.fasterxml.jackson.databind.ObjectMapper;
|
36 | 36 | import com.google.common.collect.ImmutableMap;
|
37 | 37 | import com.google.common.collect.Lists;
|
| 38 | +import io.netty.handler.codec.http.HttpHeaderNames; |
38 | 39 | import it.unimi.dsi.fastutil.doubles.Double2LongMap;
|
39 | 40 | import net.sf.oval.constraint.Min;
|
40 | 41 | import net.sf.oval.constraint.NotNull;
|
| 42 | +import org.asynchttpclient.AsyncHttpClient; |
| 43 | +import org.asynchttpclient.RequestBuilder; |
| 44 | +import org.asynchttpclient.util.HttpConstants; |
41 | 45 |
|
42 | 46 | import java.io.ByteArrayOutputStream;
|
43 | 47 | import java.io.IOException;
|
|
49 | 53 | import java.util.Map;
|
50 | 54 | import java.util.Optional;
|
51 | 55 | import java.util.concurrent.atomic.LongAdder;
|
| 56 | +import java.util.zip.GZIPOutputStream; |
52 | 57 |
|
53 | 58 | /**
|
54 | 59 | * Publishes to a KairosDbSink endpoint. This class is thread safe.
|
@@ -155,6 +160,33 @@ protected Collection<SerializedDatum> serialize(final PeriodicData periodicData)
|
155 | 160 | return completeChunks;
|
156 | 161 | }
|
157 | 162 |
|
| 163 | + @Override |
| 164 | + protected RequestInfo createRequest(final AsyncHttpClient client, final byte[] serializedData) { |
| 165 | + if (this.getEnableCompression()) { |
| 166 | + final byte[] bodyData; |
| 167 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 168 | + GZIPOutputStream gzipStream = new GZIPOutputStream(bos)) { |
| 169 | + |
| 170 | + gzipStream.write(serializedData); |
| 171 | + gzipStream.flush(); |
| 172 | + bodyData = bos.toByteArray(); |
| 173 | + } catch (final IOException e) { |
| 174 | + throw new RuntimeException(e); |
| 175 | + } |
| 176 | + |
| 177 | + final RequestBuilder requestBuilder = new RequestBuilder() |
| 178 | + .setUri(getAysncHttpClientUri()) |
| 179 | + .setHeader(HttpHeaderNames.CONTENT_TYPE, "application/gzip") |
| 180 | + .setBody(bodyData) |
| 181 | + .setMethod(HttpConstants.Methods.POST); |
| 182 | + |
| 183 | + return new RequestInfo(requestBuilder.build(), serializedData.length, bodyData.length); |
| 184 | + |
| 185 | + } else { |
| 186 | + return super.createRequest(client, serializedData); |
| 187 | + } |
| 188 | + } |
| 189 | + |
158 | 190 | private void addChunk(
|
159 | 191 | final ByteArrayOutputStream chunkStream,
|
160 | 192 | final ByteBuffer currentChunk,
|
|
0 commit comments