3636import org .apache .http .nio .protocol .HttpAsyncRequestProducer ;
3737import org .apache .http .protocol .HttpContext ;
3838
39- import javax .annotation .Nullable ;
4039import java .io .IOException ;
4140
4241public class HttpAsyncRequestProducerWrapper implements HttpAsyncRequestProducer , Recyclable {
4342 private final ApacheHttpAsyncClientHelperImpl helper ;
44- private HttpAsyncRequestProducer delegate ;
45- private @ Nullable HttpRequest request ;
46- private volatile Span span ;
43+ private volatile HttpAsyncRequestProducer delegate ;
44+ private Span span ;
4745
4846 HttpAsyncRequestProducerWrapper (ApacheHttpAsyncClientHelperImpl helper ) {
4947 this .helper = helper ;
5048 }
5149
5250 public HttpAsyncRequestProducerWrapper with (HttpAsyncRequestProducer delegate , Span span ) {
53- // Order is important due to visibility - write to span last on this (initiating) thread
54- this .delegate = delegate ;
51+ // Order is important due to visibility - write to delegate last on this (initiating) thread
5552 this .span = span ;
53+ this .delegate = delegate ;
5654 return this ;
5755 }
5856
@@ -63,27 +61,26 @@ public HttpHost getTarget() {
6361
6462 @ Override
6563 public HttpRequest generateRequest () throws IOException , HttpException {
66- // first read the volatile span
67- final Span localSpan = this .span ;
68- request = delegate .generateRequest ();
64+ // first read the volatile, span will become visible as a result
65+ HttpRequest request = delegate .generateRequest ();
6966
7067 if (request != null ) {
7168 RequestLine requestLine = request .getRequestLine ();
7269 if (requestLine != null ) {
7370 String method = requestLine .getMethod ();
74- localSpan .withName (method ).appendToName (" " );
71+ span .withName (method ).appendToName (" " );
7572 span .getContext ().getHttp ().withMethod (method ).withUrl (requestLine .getUri ());
7673 }
7774
78- request .setHeader (TraceContext .TRACE_PARENT_HEADER , localSpan .getTraceContext ().getOutgoingTraceParentHeader ().toString ());
75+ request .setHeader (TraceContext .TRACE_PARENT_HEADER , span .getTraceContext ().getOutgoingTraceParentHeader ().toString ());
7976 }
8077
8178 HttpHost host = getTarget ();
8279 //noinspection ConstantConditions
8380 if (host != null ) {
8481 String hostname = host .getHostName ();
8582 if (hostname != null ) {
86- localSpan .appendToName (hostname );
83+ span .appendToName (hostname );
8784 }
8885 }
8986
@@ -118,14 +115,14 @@ public void resetRequest() throws IOException {
118115
119116 @ Override
120117 public void close () throws IOException {
121- helper .recycle (this );
122118 delegate .close ();
119+ helper .recycle (this );
123120 }
124121
125122 @ Override
126123 public void resetState () {
127- request = null ;
128- delegate = null ;
124+ // Order is important due to visibility - write to delegate last
129125 span = null ;
126+ delegate = null ;
130127 }
131128}
0 commit comments