/
FlushStrategies.java
97 lines (88 loc) · 4.13 KB
/
FlushStrategies.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
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
*
* 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 io.servicetalk.transport.netty.internal;
import io.servicetalk.concurrent.api.Publisher;
import io.servicetalk.transport.netty.internal.FlushStrategy.FlushSender;
import io.servicetalk.transport.netty.internal.FlushStrategy.WriteEventsListener;
import static io.servicetalk.transport.netty.internal.FlushOnEach.FLUSH_ON_EACH;
import static io.servicetalk.transport.netty.internal.FlushOnEnd.FLUSH_ON_END;
import static java.lang.Integer.MAX_VALUE;
/**
* A factory for creating {@link FlushStrategy}.
*/
public final class FlushStrategies {
private FlushStrategies() {
// No instances.
}
/**
* Creates a default {@link FlushStrategy}.
*
* @return Default {@link FlushStrategy}.
*/
public static FlushStrategy defaultFlushStrategy() {
return flushOnEach();
}
/**
* Creates a {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} on each call to
* the returned {@link WriteEventsListener#itemWritten(Object)} from {@link FlushStrategy#apply(FlushSender)}.
*
* @return A {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} on each call to the returned
* {@link WriteEventsListener#itemWritten(Object)} from {@link FlushStrategy#apply(FlushSender)}.
*/
public static FlushStrategy flushOnEach() {
return FLUSH_ON_EACH;
}
/**
* Creates a {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} in a batch of
* {@code batchSize} or on expiration of a batch duration i.e. when an item is emitted from
* {@code durationBoundaries}.
*
* @param batchSize Number of items in each batch which needs flushing.
* @param durationBoundaries Batch durations. Every time an item is emitted on this {@link Publisher}, the returned
* {@link FlushStrategy} will {@link FlushSender#flush() flush writes}.
* @return A {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} in a batch of
* {@code batchSize} or on expiration of a batch duration i.e. when an item is emitted from
* {@code durationBoundaries}.
*/
public static FlushStrategy batchFlush(int batchSize, Publisher<?> durationBoundaries) {
return new BatchFlush(durationBoundaries, batchSize);
}
/**
* Creates a {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} when an item is emitted from
* {@code flushBoundaries}.
*
* @param flushBoundaries Flush boundaries. Every time an item is emitted on this {@link Publisher}, the returned
* {@link FlushStrategy} will {@link FlushSender#flush() flush writes}.
*
* @return A {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} when an item is emitted from
* {@code flushBoundaries}.
*/
public static FlushStrategy flushWith(Publisher<?> flushBoundaries) {
return batchFlush(MAX_VALUE, flushBoundaries);
}
/**
* Creates a {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} when
* {@link WriteEventsListener#writeTerminated()} is called on the {@link WriteEventsListener} returned from
* {@link FlushStrategy#apply(FlushSender)}.
*
* @return A {@link FlushStrategy} that will {@link FlushSender#flush() flush writes} when either of
* {@link WriteEventsListener#writeTerminated()} is called on the {@link WriteEventsListener} returned from
* {@link FlushStrategy#apply(FlushSender)}.
*/
public static FlushStrategy flushOnEnd() {
return FLUSH_ON_END;
}
}