Last active
August 9, 2018 00:19
-
-
Save carl-mastrangelo/2091b2d714926e005cf4200321403d5c to your computer and use it in GitHub Desktop.
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
public class DelayedForwardingClientCall<ReqT, RespT> | |
extends ForwardingClientCall<ReqT, RespT>{ | |
private final ClientCall<ReqT, RespT> delegate; | |
private final Queue<Runnable> calls = new ArrayDeque<Runnable>(); | |
private boolean started; | |
protected DelayedForwardingClientCall(ClientCall<ReqT, RespT> delegate) { | |
this.delegate = delegate; | |
} | |
@Override | |
protected ClientCall<ReqT, RespT> delegate() { | |
return delegate; | |
} | |
@Override | |
public synchronized void start(Listener<RespT> responseListener, Metadata headers) { | |
delegate().start(responseListener, headers); | |
for (Runnable call : calls) { | |
call.run(); | |
} | |
calls.clear(); | |
started = true; | |
} | |
@Override | |
public synchronized void cancel( | |
@Nullable final String message, @Nullable final Throwable cause) { | |
Runnable call = new Runnable() { | |
@Override | |
public void run() { | |
delegate().cancel(message, cause); | |
} | |
}; | |
if (started) { | |
call.run(); | |
} else { | |
calls.add(call); | |
} | |
} | |
@Override | |
public synchronized void sendMessage(final ReqT message) { | |
Runnable call = new Runnable() { | |
@Override | |
public void run() { | |
delegate().sendMessage(message); | |
} | |
}; | |
if (started) { | |
call.run(); | |
} else { | |
calls.add(call); | |
} | |
} | |
@Override | |
public synchronized void halfClose() { | |
Runnable call = new Runnable() { | |
@Override | |
public void run() { | |
delegate().halfClose(); | |
} | |
}; | |
if (started) { | |
call.run(); | |
} else { | |
calls.add(call); | |
} | |
} | |
@Override | |
public synchronized void request(final int n) { | |
Runnable call = new Runnable() { | |
@Override | |
public void run() { | |
delegate().request(n); | |
} | |
}; | |
if (started) { | |
call.run(); | |
} else { | |
calls.add(call); | |
} | |
} | |
@Override | |
public synchronized Attributes getAttributes() { | |
return delegate().getAttributes(); | |
} | |
@Override | |
public synchronized boolean isReady() { | |
if (started) { | |
return delegate().isReady(); | |
} | |
return false; | |
} | |
@Override | |
public synchronized void setMessageCompression(final boolean enabled) { | |
Runnable call = new Runnable() { | |
@Override | |
public void run() { | |
delegate().setMessageCompression(enabled); | |
} | |
}; | |
if (started) { | |
call.run(); | |
} else { | |
calls.add(call); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment