@@ -609,7 +609,7 @@ private void scheduleSendJobOnConnected(final ContextualChannel chan) {
609
609
LettuceAssert .assertState (chan .eventLoop ().inEventLoop (), "must be called in event loop thread" );
610
610
611
611
// Schedule directly
612
- scheduleSendJobInEventLoopIfNeeded (chan );
612
+ scheduleSendJobInEventLoopIfNeeded (chan , false );
613
613
}
614
614
615
615
private void scheduleSendJobIfNeeded (final ContextualChannel chan ) {
@@ -618,11 +618,6 @@ private void scheduleSendJobIfNeeded(final ContextualChannel chan) {
618
618
}
619
619
620
620
final EventLoop eventLoop = chan .eventLoop ();
621
- if (eventLoop .inEventLoop ()) {
622
- scheduleSendJobInEventLoopIfNeeded (chan );
623
- return ;
624
- }
625
-
626
621
if (chan .context .batchFlushEndPointContext .hasOngoingSendLoop .tryEnterSafeGetVolatile ()) {
627
622
// Benchmark result of using tryEnterSafeGetVolatile() or not (1 thread, async get):
628
623
// 1. uses tryEnterSafeGetVolatile() to avoid unnecessary eventLoop.execute() calls
@@ -631,7 +626,7 @@ private void scheduleSendJobIfNeeded(final ContextualChannel chan) {
631
626
// 2. uses eventLoop.execute() directly
632
627
// Avg latency: 3.2677197021496998s
633
628
// Avg QPS: 476925.0751855796/s
634
- eventLoop .execute (() -> scheduleSendJobInEventLoopIfNeeded (chan ));
629
+ eventLoop .execute (() -> scheduleSendJobInEventLoopIfNeeded (chan , true ));
635
630
}
636
631
637
632
// Otherwise:
@@ -642,10 +637,13 @@ private void scheduleSendJobIfNeeded(final ContextualChannel chan) {
642
637
// second loopSend0(), which will call poll()
643
638
}
644
639
645
- private void scheduleSendJobInEventLoopIfNeeded (final ContextualChannel chan ) {
640
+ private void scheduleSendJobInEventLoopIfNeeded (final ContextualChannel chan , boolean enteredSafe ) {
646
641
// Guarantee only 1 send loop.
647
- if (chan .context .batchFlushEndPointContext .hasOngoingSendLoop .tryEnterUnsafe ()) {
642
+ BatchFlushEndPointContext .HasOngoingSendLoop hasOngoingSendLoop = chan .context .batchFlushEndPointContext .hasOngoingSendLoop ;
643
+ if (hasOngoingSendLoop .tryEnterUnsafe ()) {
648
644
loopSend (chan );
645
+ } else if (enteredSafe ) {
646
+ hasOngoingSendLoop .exitSafe ();
649
647
}
650
648
}
651
649
@@ -691,6 +689,8 @@ private void loopSend0(final BatchFlushEndPointContext batchFlushEndPointContext
691
689
batchFlushEndPointContext .hasOngoingSendLoop .exitSafe ();
692
690
// // Guarantee thread-safety: no dangling tasks in the queue.
693
691
loopSend0 (batchFlushEndPointContext , chan , remainingSpinnCount , false );
692
+ // chan.eventLoop().schedule(() -> loopSend0(batchFlushEndPointContext, chan, writeSpinCount, false), 100,
693
+ // TimeUnit.NANOSECONDS);
694
694
} else {
695
695
// The send loop will be triggered later when a new task is added,
696
696
batchFlushEndPointContext .hasOngoingSendLoop .exitUnsafe ();
0 commit comments