Fixes #2429 - Review HttpClient backpressure semantic.
Fixes after merge. Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
55a90fcecf
commit
c93dddec30
|
@ -21,6 +21,7 @@ package org.eclipse.jetty.client.api;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.util.EventListener;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Flow;
|
||||
import java.util.function.LongConsumer;
|
||||
|
||||
import org.eclipse.jetty.client.util.BufferingResponseListener;
|
||||
|
@ -195,7 +196,7 @@ public interface Response
|
|||
* The {@code callback} object should be succeeded to signal that the
|
||||
* {@code content} buffer has been consumed.
|
||||
* The {@code demand} object should be used to demand more content,
|
||||
* similarly to ReactiveStreams's {@code Subscription#request(long)}.
|
||||
* similarly to {@link Flow.Subscription#request(long)}.
|
||||
*
|
||||
* @param response the response containing the response line data and the headers
|
||||
* @param demand the object that allows to demand content buffers
|
||||
|
|
|
@ -1,183 +0,0 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A callback to be used by driver code that needs to know whether the callback has been
|
||||
* succeeded or failed (that is, completed) just after the asynchronous operation or not,
|
||||
* typically because further processing depends on the callback being completed.
|
||||
* The driver code competes with the asynchronous operation to complete the callback.
|
||||
* </p>
|
||||
* <p>
|
||||
* If the callback is already completed, the driver code continues the processing,
|
||||
* otherwise it suspends it. If it is suspended, the callback will be completed some time
|
||||
* later, and {@link #resume()} or {@link #abort(Throwable)} will be called to allow the
|
||||
* application to resume the processing.
|
||||
* </p>
|
||||
* Typical usage:
|
||||
* <pre>
|
||||
* CompletableCallback callback = new CompletableCallback()
|
||||
* {
|
||||
* @Override
|
||||
* public void resume()
|
||||
* {
|
||||
* // continue processing
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public void abort(Throwable failure)
|
||||
* {
|
||||
* // abort processing
|
||||
* }
|
||||
* }
|
||||
* asyncOperation(callback);
|
||||
* boolean completed = callback.tryComplete();
|
||||
* if (completed)
|
||||
* // suspend processing, async operation not done yet
|
||||
* else
|
||||
* // continue processing, async operation already done
|
||||
* </pre>
|
||||
*
|
||||
* @deprecated not used anymore
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class CompletableCallback implements Callback
|
||||
{
|
||||
private final AtomicReference<State> state = new AtomicReference<>(State.IDLE);
|
||||
|
||||
@Override
|
||||
public void succeeded()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
State current = state.get();
|
||||
switch (current)
|
||||
{
|
||||
case IDLE:
|
||||
{
|
||||
if (state.compareAndSet(current, State.SUCCEEDED))
|
||||
return;
|
||||
break;
|
||||
}
|
||||
case COMPLETED:
|
||||
{
|
||||
if (state.compareAndSet(current, State.SUCCEEDED))
|
||||
{
|
||||
resume();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FAILED:
|
||||
{
|
||||
return;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new IllegalStateException(current.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable x)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
State current = state.get();
|
||||
switch (current)
|
||||
{
|
||||
case IDLE:
|
||||
case COMPLETED:
|
||||
{
|
||||
if (state.compareAndSet(current, State.FAILED))
|
||||
{
|
||||
abort(x);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FAILED:
|
||||
{
|
||||
return;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new IllegalStateException(current.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback method invoked when this callback is succeeded
|
||||
* <em>after</em> a first call to {@link #tryComplete()}.
|
||||
*/
|
||||
public abstract void resume();
|
||||
|
||||
/**
|
||||
* Callback method invoked when this callback is failed.
|
||||
*
|
||||
* @param failure the throwable reprsenting the callback failure
|
||||
*/
|
||||
public abstract void abort(Throwable failure);
|
||||
|
||||
/**
|
||||
* Tries to complete this callback; driver code should call
|
||||
* this method once <em>after</em> the asynchronous operation
|
||||
* to detect whether the asynchronous operation has already
|
||||
* completed or not.
|
||||
*
|
||||
* @return whether the attempt to complete was successful.
|
||||
*/
|
||||
public boolean tryComplete()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
State current = state.get();
|
||||
switch (current)
|
||||
{
|
||||
case IDLE:
|
||||
{
|
||||
if (state.compareAndSet(current, State.COMPLETED))
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
case SUCCEEDED:
|
||||
case FAILED:
|
||||
{
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new IllegalStateException(current.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private enum State
|
||||
{
|
||||
IDLE, SUCCEEDED, FAILED, COMPLETED
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue