-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BoltChannelAutoReadLimiter.java
109 lines (91 loc) · 3.43 KB
/
BoltChannelAutoReadLimiter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.bolt.v1.runtime;
import io.netty.channel.Channel;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import org.neo4j.logging.Log;
import static java.util.Objects.requireNonNull;
public class BoltChannelAutoReadLimiter implements BoltWorkerQueueMonitor
{
private static final int defaultLowWatermark = Integer.getInteger( "org.neo4j.bolt.autoread.loWatermark", 100 );
private static final int defaultHighWatermark = Integer.getInteger( "org.neo4j.bolt.autoread.hiWatermark", 300 );
private final AtomicInteger queueSize = new AtomicInteger( 0 );
private final Channel channel;
private final Log log;
private final int lowWatermark;
private final int highWatermark;
public BoltChannelAutoReadLimiter( Channel channel, Log log )
{
this( channel, log, defaultLowWatermark, defaultHighWatermark );
}
public BoltChannelAutoReadLimiter( Channel channel, Log log, int lowWatermark, int highWatermark)
{
if ( highWatermark <= 0 )
{
throw new IllegalArgumentException( "invalid highWatermark value" );
}
if ( lowWatermark < 0 || lowWatermark >= highWatermark )
{
throw new IllegalArgumentException( "invalid lowWatermark value" );
}
this.channel = requireNonNull( channel );
this.log = log;
this.lowWatermark = lowWatermark;
this.highWatermark = highWatermark;
}
@Override
public void enqueued( Job job )
{
checkLimitsOnEnqueue( queueSize.incrementAndGet() );
}
@Override
public void dequeued( Job job )
{
checkLimitsOnDequeue( queueSize.decrementAndGet() );
}
@Override
public void drained( Collection<Job> jobs )
{
checkLimitsOnDequeue( queueSize.addAndGet( -jobs.size() ) );
}
private void checkLimitsOnEnqueue( int currentSize )
{
if (currentSize > highWatermark && channel.config().isAutoRead())
{
if (log != null)
{
log.warn( "Channel [%s]: client produced %d messages on the worker queue, auto-read is being disabled.", channel.id(), currentSize );
}
channel.config().setAutoRead( false );
}
}
private void checkLimitsOnDequeue( int currentSize )
{
if (currentSize <= lowWatermark && !channel.config().isAutoRead())
{
if (log != null)
{
log.warn( "Channel [%s]: consumed messages on the worker queue below %d, auto-read is being enabled.", channel.id(), currentSize );
}
channel.config().setAutoRead( true );
}
}
}