-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Atomically prepare and validate connections during acquisition.
We now ensure that no cancel signals interrupt preparation and connection acquisition by using the discardOnCancel(…) operator. [#140] Signed-off-by: Mark Paluch <[email protected]>
- Loading branch information
Showing
5 changed files
with
268 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.r2dbc.pool; | ||
|
||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.publisher.MonoOperator; | ||
import reactor.core.publisher.Operators; | ||
import reactor.util.Logger; | ||
import reactor.util.Loggers; | ||
import reactor.util.context.Context; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* A decorating operator that replays signals from its source to a {@link Subscriber} and drains the source upon {@link Subscription#cancel() cancel} and drops data signals until termination. | ||
* Draining data is required to complete a particular request/response window and clear the protocol state as client code expects to start a request/response conversation without any previous | ||
* response state. | ||
*/ | ||
class MonoDiscardOnCancel<T> extends MonoOperator<T, T> { | ||
|
||
private static final Logger logger = Loggers.getLogger(MonoDiscardOnCancel.class); | ||
|
||
private final Runnable cancelConsumer; | ||
|
||
MonoDiscardOnCancel(Mono<? extends T> source, Runnable cancelConsumer) { | ||
super(source); | ||
this.cancelConsumer = cancelConsumer; | ||
} | ||
|
||
@Override | ||
public void subscribe(CoreSubscriber<? super T> actual) { | ||
this.source.subscribe(new MonoDiscardOnCancelSubscriber<>(actual, this.cancelConsumer)); | ||
} | ||
|
||
static class MonoDiscardOnCancelSubscriber<T> extends AtomicBoolean implements CoreSubscriber<T>, Subscription { | ||
|
||
final CoreSubscriber<T> actual; | ||
|
||
final Context ctx; | ||
|
||
final Runnable cancelConsumer; | ||
|
||
Subscription s; | ||
|
||
MonoDiscardOnCancelSubscriber(CoreSubscriber<T> actual, Runnable cancelConsumer) { | ||
|
||
this.actual = actual; | ||
this.ctx = actual.currentContext(); | ||
this.cancelConsumer = cancelConsumer; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
|
||
if (Operators.validate(this.s, s)) { | ||
this.s = s; | ||
this.actual.onSubscribe(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
|
||
if (this.get()) { | ||
Operators.onDiscard(t, this.ctx); | ||
return; | ||
} | ||
|
||
this.actual.onNext(t); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
if (!this.get()) { | ||
this.actual.onError(t); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
if (!this.get()) { | ||
this.actual.onComplete(); | ||
} | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
this.s.request(n); | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
|
||
if (compareAndSet(false, true)) { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("received cancel signal"); | ||
} | ||
try { | ||
this.cancelConsumer.run(); | ||
} catch (Exception e) { | ||
Operators.onErrorDropped(e, this.ctx); | ||
} | ||
this.s.request(Long.MAX_VALUE); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.r2dbc.pool; | ||
|
||
import org.reactivestreams.Subscription; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Operator utility. | ||
* | ||
* @since 0.8.8 | ||
*/ | ||
final class Operators { | ||
|
||
private Operators() { | ||
} | ||
|
||
/** | ||
* Replay signals from {@link Mono the source} until cancellation. Drains the source for data signals if the subscriber cancels the subscription. | ||
* <p> | ||
* Draining data is required to complete a particular request/response window and clear the protocol state as client code expects to start a request/response conversation without leaving | ||
* previous frames on the stack. | ||
* | ||
* @param source the source to decorate. | ||
* @param <T> The type of values in both source and output sequences. | ||
* @return decorated {@link Mono}. | ||
*/ | ||
public static <T> Mono<T> discardOnCancel(Mono<? extends T> source) { | ||
return new MonoDiscardOnCancel<>(source, () -> { | ||
}); | ||
} | ||
|
||
/** | ||
* Replay signals from {@link Mono the source} until cancellation. Drains the source for data signals if the subscriber cancels the subscription. | ||
* <p> | ||
* Draining data is required to complete a particular request/response window and clear the protocol state as client code expects to start a request/response conversation without leaving | ||
* previous frames on the stack. | ||
* <p>Propagate the {@link Subscription#cancel()} signal to a {@link Runnable consumer}. | ||
* | ||
* @param source the source to decorate. | ||
* @param cancelConsumer {@link Runnable} notified when the resulting {@link Mono} receives a {@link Subscription#cancel() cancel} signal. | ||
* @param <T> The type of values in both source and output sequences. | ||
* @return decorated {@link Mono}. | ||
*/ | ||
public static <T> Mono<T> discardOnCancel(Mono<? extends T> source, Runnable cancelConsumer) { | ||
return new MonoDiscardOnCancel<>(source, cancelConsumer); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters