-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a way to run service code out of I/O event loops (#5233)
Motivation: When service devs are not very familiar with asynchronous programming, it is very easy to drive Armeria's core event loops into havoc by blocking them. Framework devs may want to make sure Armeria at least handle I/O and function normally for a certain set of core services, such as `PrometheusExpositionService` or `HealthCheckService`, by isolating other non-core services from the I/O event loops. Modifications: * Add the `serviceWorkerGroup()` builder methods to `ServerBuilder` and `ServiceConfigSetters` so a user can specify the service worker groups as shown in the above example. * If `serviceWorkerGroup` is not specified, the `workerGroup` is used by default. * Change how Armeria assigns an event loop to a `ServiceRequestContext`. * If the `workerGroup` is different from `serviceWorkerGroup`, then an event loop from `serviceWorkerGroup` is used. * Otherwise, the IO event loop is used for executing services * Modified the constructor of `DefaultServiceRequestContext` so it accepts an `EventLoop`. * Modified `HttpServerHandler.handleRequest()`, so that `HttpService#serve` is executed from the `serviceWorkerGroup` * Modified so that we can guarantee that pending `RequestLogFuture`s are always scheduled from the context's event loop Result: - Closes #4099. - Users can add per-service/virtual host/server `serviceWorkerGroup` property that makes a service use a different `EventLoopGroup` than `ServerBuilder.workerGroup`. --------- Co-authored-by: kezhenxu94 <[email protected]>
- Loading branch information
1 parent
d9703ed
commit a54f418
Showing
32 changed files
with
1,019 additions
and
112 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
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
117 changes: 117 additions & 0 deletions
117
core/src/main/java/com/linecorp/armeria/common/stream/SubscribeOnStreamMessage.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,117 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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 com.linecorp.armeria.common.stream; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
|
||
import io.netty.util.concurrent.EventExecutor; | ||
|
||
final class SubscribeOnStreamMessage<T> implements StreamMessage<T> { | ||
|
||
private final StreamMessage<T> upstream; | ||
private final EventExecutor upstreamExecutor; | ||
|
||
SubscribeOnStreamMessage(StreamMessage<T> upstream, EventExecutor upstreamExecutor) { | ||
this.upstream = upstream; | ||
this.upstreamExecutor = upstreamExecutor; | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return upstream.isOpen(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return upstream.isEmpty(); | ||
} | ||
|
||
@Override | ||
public long demand() { | ||
return upstream.demand(); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> whenComplete() { | ||
return upstream.whenComplete(); | ||
} | ||
|
||
@Override | ||
public EventExecutor defaultSubscriberExecutor() { | ||
return upstreamExecutor; | ||
} | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super T> subscriber, EventExecutor downstreamExecutor, | ||
SubscriptionOption... options) { | ||
final Subscriber<? super T> subscriber0; | ||
if (upstreamExecutor == downstreamExecutor) { | ||
subscriber0 = subscriber; | ||
} else { | ||
subscriber0 = new SchedulingSubscriber<>(downstreamExecutor, subscriber); | ||
} | ||
if (upstreamExecutor.inEventLoop()) { | ||
upstream.subscribe(subscriber0, downstreamExecutor, options); | ||
} else { | ||
upstreamExecutor.execute(() -> upstream.subscribe(subscriber0, upstreamExecutor, options)); | ||
} | ||
} | ||
|
||
@Override | ||
public void abort() { | ||
upstream.abort(); | ||
} | ||
|
||
@Override | ||
public void abort(Throwable cause) { | ||
upstream.abort(cause); | ||
} | ||
|
||
static class SchedulingSubscriber<T> implements Subscriber<T> { | ||
|
||
private final Subscriber<? super T> downstream; | ||
private final EventExecutor downstreamExecutor; | ||
|
||
SchedulingSubscriber(EventExecutor downstreamExecutor, Subscriber<? super T> downstream) { | ||
this.downstream = downstream; | ||
this.downstreamExecutor = downstreamExecutor; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
downstreamExecutor.execute(() -> downstream.onSubscribe(s)); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
downstreamExecutor.execute(() -> downstream.onNext(t)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
downstreamExecutor.execute(() -> downstream.onError(t)); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
downstreamExecutor.execute(downstream::onComplete); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.