reactor.core.publisher.SinkEmptyMulticast Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/*
* Copyright (c) 2020-2023 VMware Inc. or its affiliates, All Rights Reserved.
*
* 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 reactor.core.publisher;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.stream.Stream;
import reactor.core.CoreSubscriber;
import reactor.core.Scannable;
import reactor.core.publisher.Sinks.EmitResult;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
//intentionally not final
class SinkEmptyMulticast extends Mono implements InternalEmptySink {
volatile Inner[] subscribers;
@SuppressWarnings("rawtypes")
static final AtomicReferenceFieldUpdater SUBSCRIBERS =
AtomicReferenceFieldUpdater.newUpdater(SinkEmptyMulticast.class, Inner[].class, "subscribers");
@SuppressWarnings("rawtypes")
static final Inner[] EMPTY = new Inner[0];
@SuppressWarnings("rawtypes")
static final Inner[] TERMINATED_EMPTY = new Inner[0];
@SuppressWarnings("rawtypes")
static final Inner[] TERMINATED_ERROR = new Inner[0];
static final int STATE_ADDED = 0;
static final int STATE_ERROR = -1;
static final int STATE_EMPTY = -2;
@Nullable
Throwable error;
SinkEmptyMulticast() {
SUBSCRIBERS.lazySet(this, EMPTY);
}
@Override
public int currentSubscriberCount() {
return subscribers.length;
}
@Override
public Mono asMono() {
return this;
}
boolean isTerminated(Inner>[] array) {
return array == TERMINATED_EMPTY || array == TERMINATED_ERROR;
}
@Override
public EmitResult tryEmitEmpty() {
Inner[] array;
for (;;) {
array = this.subscribers;
if (isTerminated(array)) {
return EmitResult.FAIL_TERMINATED;
}
if (SUBSCRIBERS.compareAndSet(this, array, TERMINATED_EMPTY)) {
break;
}
}
for (Inner> as : array) {
as.complete();
}
return EmitResult.OK;
}
@Override
@SuppressWarnings("unchecked")
public EmitResult tryEmitError(Throwable cause) {
Objects.requireNonNull(cause, "onError cannot be null");
Inner[] prevSubscribers = this.subscribers;
if (isTerminated(prevSubscribers)) {
return EmitResult.FAIL_TERMINATED;
}
error = cause;
for (;;) {
if (SUBSCRIBERS.compareAndSet(this, prevSubscribers, TERMINATED_ERROR)) {
break;
}
prevSubscribers = this.subscribers;
if (isTerminated(prevSubscribers)) {
return EmitResult.FAIL_TERMINATED;
}
}
for (Inner as : prevSubscribers) {
as.error(cause);
}
return EmitResult.OK;
}
@Override
public Object scanUnsafe(Attr key) {
if (key == Attr.TERMINATED) return isTerminated(subscribers);
if (key == Attr.ERROR) return subscribers == TERMINATED_ERROR ? error : null;
if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
if (key == InternalProducerAttr.INSTANCE) return true;
return null;
}
@Override
public Context currentContext() {
return Operators.multiSubscribersContext(subscribers);
}
int add(Inner ps) {
for (; ; ) {
Inner[] a = subscribers;
if (a == TERMINATED_EMPTY) {
return STATE_EMPTY;
}
if (a == TERMINATED_ERROR) {
return STATE_ERROR;
}
int n = a.length;
@SuppressWarnings("unchecked") Inner[] b = new Inner[n + 1];
System.arraycopy(a, 0, b, 0, n);
b[n] = ps;
if (SUBSCRIBERS.compareAndSet(this, a, b)) {
return STATE_ADDED;
}
}
}
@SuppressWarnings("unchecked")
void remove(Inner ps) {
for (; ; ) {
Inner[] a = subscribers;
int n = a.length;
if (n == 0) {
return;
}
int j = -1;
for (int i = 0; i < n; i++) {
if (a[i] == ps) {
j = i;
break;
}
}
if (j < 0) {
return;
}
Inner[] b;
if (n == 1) {
b = EMPTY;
}
else {
b = new Inner[n - 1];
System.arraycopy(a, 0, b, 0, j);
System.arraycopy(a, j + 1, b, j, n - j - 1);
}
if (SUBSCRIBERS.compareAndSet(this, a, b)) {
return;
}
}
}
//redefined in SinkOneMulticast
@Override
public void subscribe(final CoreSubscriber super T> actual) {
CoreSubscriber super T> wrapped =
Operators.restoreContextOnSubscriberIfAutoCPEnabled(this, actual);
Inner as = new VoidInner<>(wrapped, this);
wrapped.onSubscribe(as);
final int addedState = add(as);
if (addedState == STATE_ADDED) {
if (as.isCancelled()) {
remove(as);
}
}
else if (addedState == STATE_ERROR) {
Throwable ex = error;
wrapped.onError(ex);
}
else {
as.complete();
}
}
@Override
public Stream extends Scannable> inners() {
return Stream.of(subscribers);
}
static interface Inner extends InnerProducer {
//API must be compatible with Operators.MonoInnerProducerBase
void error(Throwable t);
void complete(T value);
void complete();
boolean isCancelled();
}
//VoidInner is optimized for not storing request / value
final static class VoidInner extends AtomicBoolean implements Inner {
final SinkEmptyMulticast parent;
final CoreSubscriber super T> actual;
VoidInner(CoreSubscriber super T> actual, SinkEmptyMulticast parent) {
this.actual = actual;
this.parent = parent;
}
@Override
public void cancel() {
if (getAndSet(true)) {
return;
}
parent.remove(this);
}
@Override
public boolean isCancelled() {
return get();
}
@Override
public void request(long l) {
Operators.validate(l);
}
@Override
public void complete(T value) {
//NO-OP
}
@Override
public void complete() {
if (get()) {
return;
}
actual.onComplete();
}
@Override
public void error(Throwable t) {
if (get()) {
Operators.onOperatorError(t, actual.currentContext());
return;
}
actual.onError(t);
}
@Override
public CoreSubscriber super T> actual() {
return actual;
}
@Override
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) {
return parent;
}
if (key == Attr.CANCELLED) {
return get();
}
if (key == Attr.RUN_STYLE) {
return Attr.RunStyle.SYNC;
}
return Inner.super.scanUnsafe(key);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy