Skip to content

Commit ffa0499

Browse files
mp911deavinash-anand
authored andcommitted
Polishing
Reorder methods. Add Javadoc. [pgjdbc#292]
1 parent 647c5dd commit ffa0499

File tree

1 file changed

+89
-63
lines changed

1 file changed

+89
-63
lines changed

src/main/java/io/r2dbc/postgresql/client/ReactorNettyClient.java

+89-63
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ private static boolean isSslException(Throwable throwable) {
251251
* Consume a {@link BackendMessage}. This method can either fully consume the message or it can signal by returning {@literal false} that the method wasn't able to fully consume the message and
252252
* that the message needs to be passed to an active {@link Conversation}.
253253
*
254-
* @param message
255-
* @return {@literal false} if the message could not be fully consumed and should be propagated to the active {@link Conversation}.
254+
* @param message the {@link BackendMessage} to handle
255+
* @return {@literal false} if the message could not be fully consumed and should be propagated to the active {@link Conversation}
256256
*/
257257
private boolean consumeMessage(BackendMessage message) {
258258

@@ -658,15 +658,15 @@ private Conversation(Predicate<BackendMessage> takeUntil, FluxSink<BackendMessag
658658
this.takeUntil = takeUntil;
659659
}
660660

661-
private long decrementDemand() {
662-
return Operators.addCap(DEMAND_UPDATER, this, -1);
661+
private void decrementDemand() {
662+
Operators.addCap(DEMAND_UPDATER, this, -1);
663663
}
664664

665665
/**
666666
* Check whether the {@link BackendMessage} can complete the conversation.
667667
*
668-
* @param item
669-
* @return
668+
* @param item the message to test whether it can complete the current conversation
669+
* @return whether the {@link BackendMessage} can complete the current conversation
670670
*/
671671
public boolean canComplete(BackendMessage item) {
672672
return this.takeUntil.test(item);
@@ -675,8 +675,7 @@ public boolean canComplete(BackendMessage item) {
675675
/**
676676
* Complete the conversation.
677677
*
678-
* @param item
679-
* @return
678+
* @param item the message completing the conversation
680679
*/
681680
public void complete(BackendMessage item) {
682681

@@ -689,8 +688,7 @@ public void complete(BackendMessage item) {
689688
/**
690689
* Emit a {@link BackendMessage}.
691690
*
692-
* @param item
693-
* @return
691+
* @param item the item to emit
694692
*/
695693
public void emit(BackendMessage item) {
696694

@@ -705,7 +703,7 @@ public void emit(BackendMessage item) {
705703
/**
706704
* Notify the conversation about an error. Drops errors silently if the conversation is finished.
707705
*
708-
* @param throwable
706+
* @param throwable the error signal
709707
*/
710708
public void onError(Throwable throwable) {
711709

@@ -779,31 +777,35 @@ public Flux<BackendMessage> addConversation(Predicate<BackendMessage> takeUntil,
779777
});
780778
}
781779

780+
/**
781+
* {@link Subscription#request(long)} callback. Request more for a {@link Conversation}. Potentially, demands also more upstream elements.
782+
*
783+
* @param conversation the conversation subject
784+
* @param n number of requested elements
785+
*/
782786
public void onRequest(Conversation conversation, long n) {
783787
conversation.incrementDemand(n);
784788
demandMore();
785789
tryDrainLoop();
786790
}
787791

788-
private void demandMore() {
789-
if (!hasBufferedItems() && this.demand.compareAndSet(0, DEMAND)) {
790-
this.upstream.request(DEMAND);
791-
}
792-
}
793-
792+
/**
793+
* {@link Subscriber#onSubscribe(Subscription)} callback. Registers the {@link Subscription} and potentially requests more upstream elements.
794+
*
795+
* @param s the subscription
796+
*/
794797
@Override
795798
public void onSubscribe(Subscription s) {
796799
this.upstream = s;
797-
this.demandMore();
798-
}
799-
800-
private boolean hasDownstreamDemand() {
801-
802-
Conversation conversation = this.conversations.peek();
803-
804-
return conversation != null && conversation.hasDemand();
800+
demandMore();
805801
}
806802

803+
/**
804+
* {@link Subscriber#onNext(Object)} callback. Decrements upstream demand and attempts to emit {@link BackendMessage} to an active {@link Conversation}. If a conversation has no demand, it
805+
* will be buffered.
806+
*
807+
* @param message the message to emit
808+
*/
807809
@Override
808810
public void onNext(BackendMessage message) {
809811

@@ -836,14 +838,63 @@ public void onNext(BackendMessage message) {
836838
tryDrainLoop();
837839
}
838840

841+
/**
842+
* {@link Subscriber#onError(Throwable)} callback.
843+
*
844+
* @param throwable the error to emit
845+
*/
846+
@Override
847+
public void onError(Throwable throwable) {
848+
849+
if (this.terminated) {
850+
Operators.onErrorDropped(throwable, currentContext());
851+
return;
852+
}
853+
854+
handleConnectionError(throwable);
855+
ReactorNettyClient.this.requestProcessor.onComplete();
856+
this.terminated = true;
857+
858+
if (isSslException(throwable)) {
859+
logger.debug("Connection Error", throwable);
860+
} else {
861+
logger.error("Connection Error", throwable);
862+
}
863+
864+
ReactorNettyClient.this.close().subscribe();
865+
}
866+
867+
/**
868+
* {@link Subscriber#onComplete()} callback.
869+
*/
870+
@Override
871+
public void onComplete() {
872+
this.terminated = true;
873+
ReactorNettyClient.this.handleClose();
874+
}
875+
876+
/**
877+
* Context propagation from an active {@link Conversation}.
878+
*/
879+
@Override
880+
public Context currentContext() {
881+
Conversation receiver = this.conversations.peek();
882+
return receiver != null ? receiver.sink.currentContext() : Context.empty();
883+
}
884+
839885
private void tryDrainLoop() {
840886
while (hasBufferedItems() && hasDownstreamDemand()) {
841-
if (!this.drainLoop()) {
887+
if (!drainLoop()) {
842888
return;
843889
}
844890
}
845891
}
846892

893+
/**
894+
* Drains the buffer. Guarded for single-thread access.
895+
*
896+
* @return {@code true} if the drain loop was entered successfully. {@code false} otherwise (i.e. a different thread already works on the drain loop).
897+
*/
847898
private boolean drainLoop() {
848899

849900
if (!this.drain.compareAndSet(false, true)) {
@@ -883,12 +934,11 @@ private boolean drainLoop() {
883934
potentiallyDemandMore(lastConversation);
884935

885936
return true;
886-
887937
}
888938

889939
private void potentiallyDemandMore(@Nullable Conversation lastConversation) {
890940
if (lastConversation == null || lastConversation.hasDemand() || lastConversation.isCancelled()) {
891-
this.demandMore();
941+
demandMore();
892942
}
893943
}
894944

@@ -901,51 +951,27 @@ private void emit(Conversation conversation, BackendMessage item) {
901951
}
902952
}
903953

904-
private boolean hasBufferedItems() {
905-
return !this.buffer.isEmpty();
906-
}
907-
908-
@Override
909-
public void onError(Throwable throwable) {
910-
911-
if (this.terminated) {
912-
Operators.onErrorDropped(throwable, currentContext());
913-
return;
954+
private void demandMore() {
955+
if (!hasBufferedItems() && this.demand.compareAndSet(0, DEMAND)) {
956+
this.upstream.request(DEMAND);
914957
}
958+
}
915959

916-
handleConnectionError(throwable);
917-
ReactorNettyClient.this.requestProcessor.onComplete();
918-
this.terminated = true;
919-
920-
if (isSslException(throwable)) {
921-
logger.debug("Connection Error", throwable);
922-
} else {
923-
logger.error("Connection Error", throwable);
924-
}
960+
private boolean hasDownstreamDemand() {
925961

926-
ReactorNettyClient.this.close().subscribe();
927-
}
962+
Conversation conversation = this.conversations.peek();
928963

929-
@Override
930-
public void onComplete() {
931-
this.terminated = true;
932-
ReactorNettyClient.this.handleClose();
964+
return conversation != null && conversation.hasDemand();
933965
}
934966

935-
@Override
936-
public Context currentContext() {
937-
Conversation receiver = this.conversations.peek();
938-
if (receiver != null) {
939-
return receiver.sink.currentContext();
940-
} else {
941-
return Context.empty();
942-
}
967+
private boolean hasBufferedItems() {
968+
return !this.buffer.isEmpty();
943969
}
944970

945971
/**
946-
* Cleanup the subscriber by terminating all {@link Conversation}s and purging the data buffer.
972+
* Cleanup the subscriber by terminating all {@link Conversation}s and purging the data buffer. All conversations are completed with an error signal provided by {@code supplier}.
947973
*
948-
* @param supplier
974+
* @param supplier the error supplier
949975
*/
950976
public void close(Supplier<? extends Throwable> supplier) {
951977

0 commit comments

Comments
 (0)