Skip to content

Commit f0e274a

Browse files
committed
No need for an AtomicReference here (no CAS ops)
1 parent aba50f2 commit f0e274a

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

client/src/main/java/org/asynchttpclient/request/body/generator/ReactiveStreamsBodyGenerator.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.io.IOException;
1818
import java.nio.ByteBuffer;
1919
import java.util.concurrent.atomic.AtomicBoolean;
20-
import java.util.concurrent.atomic.AtomicReference;
2120

2221
import org.asynchttpclient.request.body.Body;
2322
import org.reactivestreams.Publisher;
@@ -31,7 +30,7 @@ public class ReactiveStreamsBodyGenerator implements FeedableBodyGenerator {
3130

3231
private final Publisher<ByteBuffer> publisher;
3332
private final FeedableBodyGenerator feedableBodyGenerator;
34-
private final AtomicReference<FeedListener> feedListener = new AtomicReference<>(null);
33+
private volatile FeedListener feedListener;
3534

3635
public ReactiveStreamsBodyGenerator(Publisher<ByteBuffer> publisher) {
3736
this.publisher = publisher;
@@ -49,7 +48,7 @@ public boolean feed(ByteBuffer buffer, boolean isLast) throws Exception {
4948

5049
@Override
5150
public void setListener(FeedListener listener) {
52-
feedListener.set(listener);
51+
feedListener = listener;
5352
feedableBodyGenerator.setListener(listener);
5453
}
5554

@@ -81,7 +80,7 @@ public long getContentLength() {
8180

8281
@Override
8382
public BodyState transferTo(ByteBuf target) throws IOException {
84-
if(initialized.compareAndSet(false, true))
83+
if (initialized.compareAndSet(false, true))
8584
publisher.subscribe(subscriber);
8685

8786
return body.transferTo(target);
@@ -101,21 +100,22 @@ public SimpleSubscriber(FeedableBodyGenerator feeder) {
101100

102101
@Override
103102
public void onSubscribe(Subscription s) {
104-
if (s == null) throw null;
103+
if (s == null)
104+
throw null;
105105

106106
// If someone has made a mistake and added this Subscriber multiple times, let's handle it gracefully
107-
if (this.subscription != null) {
107+
if (this.subscription != null) {
108108
s.cancel(); // Cancel the additional subscription
109-
}
110-
else {
111-
subscription = s;
112-
subscription.request(Long.MAX_VALUE);
109+
} else {
110+
subscription = s;
111+
subscription.request(Long.MAX_VALUE);
113112
}
114113
}
115114

116115
@Override
117116
public void onNext(ByteBuffer t) {
118-
if (t == null) throw null;
117+
if (t == null)
118+
throw null;
119119
try {
120120
feeder.feed(t, false);
121121
} catch (Exception e) {
@@ -126,18 +126,19 @@ public void onNext(ByteBuffer t) {
126126

127127
@Override
128128
public void onError(Throwable t) {
129-
if (t == null) throw null;
129+
if (t == null)
130+
throw null;
130131
LOGGER.debug("Error occurred while consuming body stream.", t);
131-
FeedListener listener = feedListener.get();
132-
if(listener != null) listener.onError(t);
132+
FeedListener listener = feedListener;
133+
if (listener != null)
134+
listener.onError(t);
133135
}
134136

135137
@Override
136138
public void onComplete() {
137139
try {
138-
feeder.feed(EMPTY, true);
139-
}
140-
catch (Exception e) {
140+
feeder.feed(EMPTY, true);
141+
} catch (Exception e) {
141142
LOGGER.info("Ignoring exception occurred while completing stream processing.", e);
142143
this.subscription.cancel();
143144
}

0 commit comments

Comments
 (0)