-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for #1102 Detect some blocking APIs inside parallel and single Scheduler
`block`, `blockOptional`, `blockFirst`, `blockLast`, `toIterable` and `toStream` all check if the executing thread is NonBlocking and throw an `IllegalStateException` if it is.
- Loading branch information
1 parent
f39aa25
commit 92df4ab
Showing
14 changed files
with
538 additions
and
87 deletions.
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
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
34 changes: 34 additions & 0 deletions
34
reactor-core/src/main/java/reactor/core/scheduler/NonBlocking.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,34 @@ | ||
/* | ||
* Copyright (c) 2011-2018 Pivotal Software Inc, 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 | ||
* | ||
* 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 reactor.core.scheduler; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
|
||
/** | ||
* A marker interface that is detected on {@link Thread Threads} while executing Reactor | ||
* blocking APIs, resulting in these calls throwing an exception. | ||
* <p> | ||
* Extend {@link AbstractReactorThreadFactory} for a {@link ThreadFactory} that can easily | ||
* create such threads, and optionally name them and further configure them. | ||
* <p> | ||
* See {@link Schedulers#isBlockingCurrentThreadOk()} and | ||
* {@link Schedulers#isBlockingCurrentThreadOk(Thread)} for a check that includes detecting | ||
* this marker interface. | ||
* | ||
* @author Simon Baslé | ||
*/ | ||
public interface NonBlocking { } |
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
98 changes: 98 additions & 0 deletions
98
reactor-core/src/main/java/reactor/core/scheduler/ReactorThreadFactory.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,98 @@ | ||
/* | ||
* Copyright (c) 2011-2018 Pivotal Software Inc, 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 | ||
* | ||
* 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 reactor.core.scheduler; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import reactor.util.annotation.Nullable; | ||
|
||
/** | ||
* The standard Reactor {@link ThreadFactory Thread factories} to be used by {@link Scheduler}, | ||
* creating {@link Thread} with a prefix (which can be retrieved with the {@link #get()} method). | ||
* | ||
* @author Simon Baslé | ||
*/ | ||
class ReactorThreadFactory implements ThreadFactory, | ||
Supplier<String>, | ||
Thread.UncaughtExceptionHandler { | ||
|
||
final private String name; | ||
final private AtomicLong counterReference; | ||
final private boolean daemon; | ||
final private boolean rejectBlocking; | ||
|
||
@Nullable | ||
final private BiConsumer<Thread, Throwable> uncaughtExceptionHandler; | ||
|
||
ReactorThreadFactory(String name, | ||
AtomicLong counterReference, | ||
boolean daemon, | ||
boolean rejectBlocking, | ||
@Nullable BiConsumer<Thread, Throwable> uncaughtExceptionHandler) { | ||
this.name = name; | ||
this.counterReference = counterReference; | ||
this.daemon = daemon; | ||
this.rejectBlocking = rejectBlocking; | ||
this.uncaughtExceptionHandler = uncaughtExceptionHandler; | ||
} | ||
|
||
@Override | ||
public final Thread newThread(@NotNull Runnable runnable) { | ||
String newThreadName = name + "-" + counterReference.incrementAndGet(); | ||
Thread t = rejectBlocking | ||
? new NonBlockingThread(runnable, newThreadName) | ||
: new Thread(runnable, newThreadName); | ||
if (daemon) { | ||
t.setDaemon(true); | ||
} | ||
if (uncaughtExceptionHandler != null) { | ||
t.setUncaughtExceptionHandler(this); | ||
} | ||
return t; | ||
} | ||
|
||
@Override | ||
public void uncaughtException(Thread t, Throwable e) { | ||
if (uncaughtExceptionHandler == null) { | ||
return; | ||
} | ||
|
||
uncaughtExceptionHandler.accept(t,e); | ||
} | ||
|
||
/** | ||
* Get the prefix used for new {@link Thread Threads} created by this {@link ThreadFactory}. | ||
* The factory can also be seen as a {@link Supplier Supplier<String>}. | ||
* | ||
* @return the thread name prefix | ||
*/ | ||
@Override | ||
public final String get() { | ||
return name; | ||
} | ||
|
||
static final class NonBlockingThread extends Thread implements NonBlocking { | ||
|
||
public NonBlockingThread(Runnable target, String name) { | ||
super(target, name); | ||
} | ||
} | ||
} |
Oops, something went wrong.