File tree Expand file tree Collapse file tree 2 files changed +12
-14
lines changed
driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal Expand file tree Collapse file tree 2 files changed +12
-14
lines changed Original file line number Diff line number Diff line change 21
21
22
22
import java .util .List ;
23
23
24
+ import static com .mongodb .reactivestreams .client .internal .MongoOperationPublisher .sinkToCallback ;
25
+
24
26
/**
25
27
* <p>This class is not part of the public API and may be removed or changed at any time</p>
26
28
*/
27
29
public class BatchCursor <T > implements AutoCloseable {
28
30
29
31
private final AsyncBatchCursor <T > wrapped ;
30
- private volatile boolean cursorClosed = false ;
31
32
32
33
public BatchCursor (final AsyncBatchCursor <T > wrapped ) {
33
34
this .wrapped = wrapped ;
34
35
}
35
36
36
37
public Publisher <List <T >> next () {
37
- return Mono .create (sink -> wrapped .next (
38
- (result , t ) -> {
39
- if (t != null && !cursorClosed ) {
40
- sink .error (t );
41
- } else if (result == null ) {
42
- sink .success ();
43
- } else {
44
- sink .success (result );
45
- }
46
- }));
38
+ return Mono .create (sink -> wrapped .next (sinkToCallback (sink )));
47
39
}
48
40
49
41
public void setBatchSize (final int batchSize ) {
@@ -59,7 +51,6 @@ public boolean isClosed() {
59
51
}
60
52
61
53
public void close () {
62
- cursorClosed = true ;
63
54
wrapped .close ();
64
55
}
65
56
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ class BatchCursorFlux<T> implements Publisher<T> {
32
32
private final BatchCursorPublisher <T > batchCursorPublisher ;
33
33
private final AtomicBoolean inProgress = new AtomicBoolean (false );
34
34
private final AtomicLong demandDelta = new AtomicLong (0 );
35
+ private final AtomicBoolean fluxCancelled = new AtomicBoolean (false );
35
36
private volatile BatchCursor <T > batchCursor ;
36
37
private FluxSink <T > sink ;
37
38
@@ -72,6 +73,11 @@ public void subscribe(final Subscriber<? super T> subscriber) {
72
73
.subscribe (subscriber );
73
74
}
74
75
76
+ private void cancelled () {
77
+ fluxCancelled .set (true );
78
+ closeCursor ();
79
+ }
80
+
75
81
private void closeCursor () {
76
82
if (batchCursor != null ) {
77
83
batchCursor .close ();
@@ -85,12 +91,13 @@ private void recurseCursor(){
85
91
} else {
86
92
batchCursor .setBatchSize (calculateBatchSize (sink .requestedFromDownstream ()));
87
93
Mono .from (batchCursor .next ())
88
- .doOnCancel (this ::closeCursor )
89
94
.doOnError ((e ) -> {
90
95
try {
91
96
closeCursor ();
92
97
} finally {
93
- sink .error (e );
98
+ if (!fluxCancelled .get ()) {
99
+ sink .error (e );
100
+ }
94
101
}
95
102
})
96
103
.doOnSuccess (results -> {
You can’t perform that action at this time.
0 commit comments