2424/**
2525 * An HTTP forwarder that delivers DogStatsD HTTP payloads to a remote endpoint.
2626 *
27- * <p>Payloads are enqueued via {@link #send(byte[])} and delivered asynchronously by a background
28- * thread. Failed requests are retried with exponential back-off up to {@code maxTries} attempts
29- * before being discarded.
27+ * <p>Payloads are enqueued via {@link #send(URI, byte[])} and delivered asynchronously by a
28+ * background thread. Failed requests are retried with exponential back-off up to {@code maxTries}
29+ * attempts before being discarded.
3030 */
3131public class Forwarder extends Thread {
3232 static final Logger logger = Logger .getLogger (Forwarder .class .getName ());
3333 final BoundedQueue queue ;
34- final URI url ;
3534 final HttpClient client ;
3635 final Duration requestTimeout ;
3736 final Random rng = new Random ();
@@ -42,9 +41,8 @@ public class Forwarder extends Thread {
4241 final Telemetry telemetry ;
4342
4443 /**
45- * Creates a new forwarder targeting the given URL .
44+ * Creates a new forwarder.
4645 *
47- * @param url the remote HTTP endpoint to POST payloads to
4846 * @param maxRequestsBytes maximum total size of buffered payloads, in bytes
4947 * @param maxTries maximum number of delivery attempts per payload
5048 * @param whenFull action to take when the queue is at capacity
@@ -53,13 +51,11 @@ public class Forwarder extends Thread {
5351 * {@code null} disables the request timeout
5452 */
5553 public Forwarder (
56- URI url ,
5754 long maxRequestsBytes ,
5855 long maxTries ,
5956 WhenFull whenFull ,
6057 Duration connectTimeout ,
6158 Duration requestTimeout ) {
62- this .url = url ;
6359 this .telemetry = new Telemetry ();
6460 this .queue = new BoundedQueue (maxRequestsBytes , maxTries , whenFull , this .telemetry );
6561 this .requestTimeout = requestTimeout ;
@@ -91,26 +87,30 @@ public void run() {
9187 }
9288
9389 /**
94- * Enqueues a payload for delivery to the remote endpoint.
90+ * Enqueues a payload for delivery to the given endpoint.
9591 *
9692 * <p>If the queue is full, behaviour is determined by the {@link WhenFull} policy supplied at
9793 * construction time.
9894 *
95+ * @param url the remote HTTP endpoint to POST the payload to
9996 * @param payload the raw bytes to deliver
10097 * @throws InterruptedException if the calling thread is interrupted while waiting for space
10198 * ({@link WhenFull#BLOCK} mode only)
10299 */
103- public void send (byte [] payload ) throws InterruptedException {
104- queue .add (payload );
100+ public void send (URI url , byte [] payload ) throws InterruptedException {
101+ queue .add (new Payload ( url , payload ) );
105102 telemetry .onEnqueue (payload .length );
106103 }
107104
108- void runOnce (Map .Entry <BoundedQueue .Key , byte []> item ) throws InterruptedException {
109- byte [] payload = item .getValue ();
110- logger .log (Level .INFO , "sending {0} bytes" , payload .length );
105+ void runOnce (Map .Entry <BoundedQueue .Key , Payload > item ) throws InterruptedException {
106+ Payload payload = item .getValue ();
107+ logger .log (
108+ Level .INFO ,
109+ "sending {0} bytes to {1}" ,
110+ new Object [] {payload .bytes .length , payload .url });
111111
112112 HttpRequest .Builder builder =
113- HttpRequest .newBuilder (url ).POST (BodyPublishers .ofByteArray (payload ));
113+ HttpRequest .newBuilder (payload . url ).POST (BodyPublishers .ofByteArray (payload . bytes ));
114114 if (requestTimeout != null ) {
115115 builder .timeout (requestTimeout );
116116 }
@@ -138,9 +138,9 @@ void runOnce(Map.Entry<BoundedQueue.Key, byte[]> item) throws InterruptedExcepti
138138 backoff ();
139139 }
140140
141- void handleResponse (int code , Map .Entry <BoundedQueue .Key , byte [] > item )
141+ void handleResponse (int code , Map .Entry <BoundedQueue .Key , Payload > item )
142142 throws InterruptedException {
143- int len = item .getValue ().length ;
143+ int len = item .getValue ().bytes . length ;
144144 switch (code ) {
145145 case 400 :
146146 telemetry .onResponse (code , len , false );
@@ -158,9 +158,9 @@ void handleResponse(int code, Map.Entry<BoundedQueue.Key, byte[]> item)
158158 }
159159 }
160160
161- void handleTransportError (Map .Entry <BoundedQueue .Key , byte [] > item )
161+ void handleTransportError (Map .Entry <BoundedQueue .Key , Payload > item )
162162 throws InterruptedException {
163- telemetry .onTransportError (item .getValue ().length );
163+ telemetry .onTransportError (item .getValue ().bytes . length );
164164 increaseBackoff ();
165165 queue .requeue (item );
166166 }
0 commit comments