Added cancellation logic to RequestBodyPublisher.

This commit is contained in:
Arjen Poutsma
2015-09-10 11:16:21 +02:00
parent 5bbeb9c204
commit 0b19fca73a
4 changed files with 195 additions and 35 deletions
@@ -1,33 +0,0 @@
/*
* Copyright 2002-2015 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
*
* http://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 org.springframework.reactive.util;
/**
* @author Arjen Poutsma
*/
interface Signal<T> {
boolean isOnNext();
T next();
boolean isOnError();
Throwable error();
boolean isComplete();
}
@@ -49,19 +49,30 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
private boolean stalled;
private boolean cancelled;
public RequestBodyPublisher(AsyncContextSynchronizer synchronizer, int bufferSize) {
this.synchronizer = synchronizer;
this.buffer = new byte[bufferSize];
}
@Override
public void subscribe(Subscriber<? super byte[]> s) {
this.subscriber = s;
public void subscribe(Subscriber<? super byte[]> subscriber) {
if (subscriber == null) {
throw new NullPointerException();
}
else if (this.subscriber != null) {
subscriber.onError(new IllegalStateException("Only one subscriber allowed"));
}
this.subscriber = subscriber;
this.subscriber.onSubscribe(new RequestBodySubscription());
}
@Override
public void onDataAvailable() throws IOException {
if (cancelled) {
return;
}
ServletInputStream input = this.synchronizer.getInputStream();
logger.debug("onDataAvailable: " + input);
@@ -100,6 +111,9 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
@Override
public void onAllDataRead() throws IOException {
if (cancelled) {
return;
}
logger.debug("All data read");
this.synchronizer.readComplete();
if (this.subscriber != null) {
@@ -109,7 +123,11 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
@Override
public void onError(Throwable t) {
if (cancelled) {
return;
}
logger.error("RequestBodyPublisher Error", t);
this.synchronizer.readComplete();
if (this.subscriber != null) {
this.subscriber.onError(t);
}
@@ -119,6 +137,9 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
@Override
public void request(long n) {
if (cancelled) {
return;
}
logger.debug("Updating demand " + demand + " by " + n);
demand.increase(n);
@@ -138,6 +159,10 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
@Override
public void cancel() {
if (cancelled) {
return;
}
cancelled = true;
synchronizer.readComplete();
demand.reset();
}