Skip to content

Instantly share code, notes, and snippets.

@chocotan
Last active February 4, 2026 10:13
Show Gist options
  • Select an option

  • Save chocotan/8d501e99a6ef1160077c1d59c43e2733 to your computer and use it in GitHub Desktop.

Select an option

Save chocotan/8d501e99a6ef1160077c1d59c43e2733 to your computer and use it in GitHub Desktop.
import org.reactivestreams.Subscription;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoOperator;
import reactor.util.context.Context;
public class NewCatFluxFilter implements WebFilter, Ordered {
public NewCatFluxFilter() {
}
@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return new MonoWebFilterTrace(chain.filter(exchange), exchange, this);
}
private static class MonoWebFilterTrace extends MonoOperator<Void, Void> {
final ServerWebExchange exchange;
final NewCatFluxFilter parent;
MonoWebFilterTrace(Mono<? extends Void> source, ServerWebExchange exchange,
NewCatFluxFilter parent) {
super(source);
this.exchange = exchange;
this.parent = parent;
}
@Override
public void subscribe(CoreSubscriber<? super Void> subscriber) {
WebFilterTraceSubscriber sub = new WebFilterTraceSubscriber(subscriber, subscriber.currentContext(), this);
this.source.doOnCancel(() -> {
sub.onCancel();
}).subscribe(sub);
}
static final class WebFilterTraceSubscriber implements CoreSubscriber<Void> {
final CoreSubscriber<? super Void> actual;
final ServerWebExchange exchange;
WebFilterTraceSubscriber(CoreSubscriber<? super Void> actual,
Context context, MonoWebFilterTrace parent) {
this.actual = actual;
this.exchange = parent.exchange;
}
@Override
public void onSubscribe(Subscription s) {
this.actual.onSubscribe(s);
}
@Override
public void onNext(Void unused) {
this.actual.onNext(unused);
}
@Override
public void onError(Throwable e) {
// TODO 这里加日志记录
this.doFinish();
this.actual.onError(e);
}
@Override
public void onComplete() {
this.doFinish();
this.actual.onComplete();
}
public void doFinish() {
}
public void onCancel() {
// TODO 这里加日志记录
this.onComplete();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment