-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/rate limiter publisher #672
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
242 changes: 242 additions & 0 deletions
242
rsocket-core/src/main/java/io/rsocket/internal/RateLimitableRequestPublisher.java
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,242 @@ | ||
/* | ||
* Copyright 2015-2018 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 io.rsocket.internal; | ||
|
||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; | ||
import javax.annotation.Nullable; | ||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Operators; | ||
|
||
/** */ | ||
public class RateLimitableRequestPublisher<T> extends Flux<T> implements Subscription { | ||
|
||
private static final int NOT_CANCELED_STATE = 0; | ||
private static final int CANCELED_STATE = 1; | ||
|
||
private final Publisher<T> source; | ||
|
||
private volatile int canceled; | ||
private static final AtomicIntegerFieldUpdater<RateLimitableRequestPublisher> CANCELED = | ||
AtomicIntegerFieldUpdater.newUpdater(RateLimitableRequestPublisher.class, "canceled"); | ||
|
||
private final long prefetch; | ||
private final long limit; | ||
|
||
private long externalRequested; // need sync | ||
private int pendingToFulfil; // need sync since should be checked/zerroed in onNext | ||
// and increased in request | ||
private int deliveredElements; // no need to sync since increased zerroed only in | ||
// the request method | ||
|
||
private boolean subscribed; | ||
|
||
private @Nullable Subscription internalSubscription; | ||
|
||
private RateLimitableRequestPublisher(Publisher<T> source, long prefetch) { | ||
this.source = source; | ||
this.prefetch = prefetch; | ||
this.limit = prefetch == Integer.MAX_VALUE ? Integer.MAX_VALUE : (prefetch - (prefetch >> 2)); | ||
} | ||
|
||
public static <T> RateLimitableRequestPublisher<T> wrap(Publisher<T> source, long prefetch) { | ||
return new RateLimitableRequestPublisher<>(source, prefetch); | ||
} | ||
|
||
@Override | ||
public void subscribe(CoreSubscriber<? super T> destination) { | ||
synchronized (this) { | ||
if (subscribed) { | ||
throw new IllegalStateException("only one subscriber at a time"); | ||
} | ||
|
||
subscribed = true; | ||
} | ||
final InnerOperator s = new InnerOperator(destination); | ||
|
||
source.subscribe(s); | ||
destination.onSubscribe(s); | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
synchronized (this) { | ||
long requested = externalRequested; | ||
if (requested == Long.MAX_VALUE) { | ||
return; | ||
} | ||
externalRequested = Operators.addCap(n, requested); | ||
} | ||
|
||
requestN(); | ||
} | ||
|
||
private void requestN() { | ||
final long r; | ||
final Subscription s; | ||
|
||
synchronized (this) { | ||
s = internalSubscription; | ||
if (s == null) { | ||
return; | ||
} | ||
|
||
final long er = externalRequested; | ||
final long p = prefetch; | ||
final int pendingFulfil = pendingToFulfil; | ||
|
||
if (er != Long.MAX_VALUE || p != Integer.MAX_VALUE) { | ||
// shortcut | ||
if (pendingFulfil == p) { | ||
return; | ||
} | ||
|
||
r = Math.min(p - pendingFulfil, er); | ||
if (er != Long.MAX_VALUE) { | ||
externalRequested -= r; | ||
} | ||
if (p != Integer.MAX_VALUE) { | ||
pendingToFulfil += r; | ||
} | ||
} else { | ||
r = Long.MAX_VALUE; | ||
} | ||
} | ||
|
||
if (r > 0) { | ||
s.request(r); | ||
} | ||
} | ||
|
||
public void cancel() { | ||
if (!isCanceled() && CANCELED.compareAndSet(this, NOT_CANCELED_STATE, CANCELED_STATE)) { | ||
Subscription s; | ||
|
||
synchronized (this) { | ||
s = internalSubscription; | ||
internalSubscription = null; | ||
subscribed = false; | ||
} | ||
|
||
if (s != null) { | ||
s.cancel(); | ||
} | ||
} | ||
} | ||
|
||
private boolean isCanceled() { | ||
return canceled == CANCELED_STATE; | ||
} | ||
|
||
private class InnerOperator implements CoreSubscriber<T>, Subscription { | ||
final Subscriber<? super T> destination; | ||
|
||
private InnerOperator(Subscriber<? super T> destination) { | ||
this.destination = destination; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
synchronized (RateLimitableRequestPublisher.this) { | ||
RateLimitableRequestPublisher.this.internalSubscription = s; | ||
|
||
if (isCanceled()) { | ||
s.cancel(); | ||
subscribed = false; | ||
RateLimitableRequestPublisher.this.internalSubscription = null; | ||
} | ||
} | ||
|
||
requestN(); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
try { | ||
destination.onNext(t); | ||
|
||
if (prefetch == Integer.MAX_VALUE) { | ||
return; | ||
} | ||
|
||
final long l = limit; | ||
int d = deliveredElements + 1; | ||
|
||
if (d == l) { | ||
d = 0; | ||
final long r; | ||
final Subscription s; | ||
|
||
synchronized (RateLimitableRequestPublisher.this) { | ||
long er = externalRequested; | ||
s = internalSubscription; | ||
|
||
if (s == null) { | ||
return; | ||
} | ||
|
||
if (er >= l) { | ||
er -= l; | ||
// keep pendingToFulfil as is since it is eq to prefetch | ||
r = l; | ||
} else { | ||
pendingToFulfil -= l; | ||
if (er > 0) { | ||
r = er; | ||
er = 0; | ||
pendingToFulfil += r; | ||
} else { | ||
r = 0; | ||
} | ||
} | ||
|
||
externalRequested = er; | ||
} | ||
|
||
if (r > 0) { | ||
s.request(r); | ||
} | ||
} | ||
|
||
deliveredElements = d; | ||
} catch (Throwable e) { | ||
onError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
destination.onError(t); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
destination.onComplete(); | ||
} | ||
|
||
@Override | ||
public void request(long n) {} | ||
|
||
@Override | ||
public void cancel() { | ||
RateLimitableRequestPublisher.this.cancel(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't appear to match the logic on line 56. Integer vs Long MAX_VALUE. It's hard to intuit whether that's intentional because there are multiple fields. Maybe some comments to clarify what max values are used.