-
Notifications
You must be signed in to change notification settings - Fork 595
/
StreamSubscriber.scala
229 lines (200 loc) · 8.32 KB
/
StreamSubscriber.scala
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package fs2
package interop
package reactivestreams
import cats._
import cats.effect.kernel.Async
import cats.effect.std.Dispatcher
import cats.syntax.all._
import org.reactivestreams._
import java.util.concurrent.atomic.AtomicReference
/** Implementation of a `org.reactivestreams.Subscriber`.
*
* This is used to obtain a `fs2.Stream` from an upstream reactivestreams system.
*
* @see [[https://github.com/reactive-streams/reactive-streams-jvm#2-subscriber-code]]
*/
final class StreamSubscriber[F[_], A](
val sub: StreamSubscriber.FSM[F, A]
)(implicit
F: ApplicativeError[F, Throwable]
) extends Subscriber[A] {
@deprecated("Use a constructor without dispatcher instead", "3.1.4")
def this(sub: StreamSubscriber.FSM[F, A], dispatcher: Dispatcher[F])(implicit
F: ApplicativeError[F, Throwable]
) = this(sub)
/** Called by an upstream reactivestreams system */
def onSubscribe(s: Subscription): Unit = {
nonNull(s)
sub.onSubscribe(s)
}
/** Called by an upstream reactivestreams system */
def onNext(a: A): Unit = {
nonNull(a)
sub.onNext(a)
}
/** Called by an upstream reactivestreams system */
def onComplete(): Unit = sub.onComplete()
/** Called by an upstream reactivestreams system */
def onError(t: Throwable): Unit = {
nonNull(t)
sub.onError(t)
}
def stream(subscribe: F[Unit]): Stream[F, A] = sub.stream(subscribe)
private def nonNull[B](b: B): Unit = if (b == null) throw new NullPointerException()
}
object StreamSubscriber {
def apply[F[_]: Async, A](bufferSize: Int): F[StreamSubscriber[F, A]] =
fsm[F, A](bufferSize).map(new StreamSubscriber(_))
@deprecated(
"Use apply method with a buffer size. Use a buffer size of 1 to keep the same behavior.",
"3.1.4"
)
def apply[F[_]: Async, A]: F[StreamSubscriber[F, A]] =
apply(bufferSize = 1)
@deprecated("Use apply method without dispatcher instead", "3.1.4")
def apply[F[_]: Async, A](dispatcher: Dispatcher[F]): F[StreamSubscriber[F, A]] =
apply[F, A]
/** A finite state machine describing the subscriber */
private[reactivestreams] trait FSM[F[_], A] {
/** receives a subscription from upstream */
def onSubscribe(s: Subscription): Unit
/** receives next record from upstream */
def onNext(a: A): Unit
/** receives error from upstream */
def onError(t: Throwable): Unit
/** called when upstream has finished sending records */
def onComplete(): Unit
/** called when downstream has finished consuming records */
def onFinalize: F[Unit]
/** producer for downstream */
def dequeue1: F[Either[Throwable, Option[Chunk[A]]]]
/** downstream stream */
def stream(subscribe: F[Unit])(implicit ev: ApplicativeError[F, Throwable]): Stream[F, A] =
Stream.bracket(subscribe)(_ => onFinalize) >> Stream
.eval(dequeue1)
.repeat
.rethrow
.unNoneTerminate
.unchunks
}
private[reactivestreams] def fsm[F[_], A](
bufferSize: Int
)(implicit F: Async[F]): F[FSM[F, A]] = {
type Out = Either[Throwable, Option[Chunk[A]]]
sealed trait Input
case class OnSubscribe(s: Subscription) extends Input
case class OnNext(a: A) extends Input
case class OnError(e: Throwable) extends Input
case object OnComplete extends Input
case object OnFinalize extends Input
case class OnDequeue(response: Out => Unit) extends Input
sealed trait State
case object Uninitialized extends State
case class Idle(sub: Subscription, buffer: Chunk[A]) extends State
case class RequestBeforeSubscription(req: Out => Unit) extends State
case class WaitingOnUpstream(sub: Subscription, buffer: Chunk[A], elementRequest: Out => Unit)
extends State
case object UpstreamCompletion extends State
case object DownstreamCancellation extends State
case class UpstreamError(err: Throwable) extends State
def reportFailure(e: Throwable): Unit =
Thread.getDefaultUncaughtExceptionHandler match {
case null => e.printStackTrace()
case h => h.uncaughtException(Thread.currentThread(), e)
}
def step(in: Input): State => (State, () => Unit) =
in match {
case OnSubscribe(s) => {
case RequestBeforeSubscription(req) =>
WaitingOnUpstream(s, Chunk.empty, req) -> (() => s.request(bufferSize.toLong))
case Uninitialized => Idle(s, Chunk.empty) -> (() => ())
case o =>
val err = new Error(s"received subscription in invalid state [$o]")
o -> { () =>
s.cancel()
reportFailure(err)
}
}
case OnNext(a) => {
case WaitingOnUpstream(s, buffer, r) =>
val newBuffer = buffer ++ Chunk(a)
if (newBuffer.size == bufferSize) {
Idle(s, Chunk.empty) -> (() => r(newBuffer.some.asRight))
} else
WaitingOnUpstream(s, newBuffer, r) -> (() => ())
case DownstreamCancellation => DownstreamCancellation -> (() => ())
case o =>
o -> (() => reportFailure(new Error(s"received record [$a] in invalid state [$o]")))
}
case OnComplete => {
case WaitingOnUpstream(_, buffer, r) =>
if (buffer.nonEmpty) {
UpstreamCompletion -> (() => r(buffer.some.asRight))
} else {
UpstreamCompletion -> (() => r(None.asRight))
}
case _ => UpstreamCompletion -> (() => ())
}
case OnError(e) => {
case WaitingOnUpstream(_, _, r) => UpstreamError(e) -> (() => r(e.asLeft))
case _ => UpstreamError(e) -> (() => ())
}
case OnFinalize => {
case WaitingOnUpstream(sub, _, r) =>
DownstreamCancellation -> { () =>
sub.cancel()
r(None.asRight)
}
case Idle(sub, _) => DownstreamCancellation -> (() => sub.cancel())
case o => o -> (() => ())
}
case OnDequeue(r) => {
case Uninitialized => RequestBeforeSubscription(r) -> (() => ())
case Idle(sub, buffer) =>
WaitingOnUpstream(sub, buffer, r) -> (() => sub.request(bufferSize.toLong))
case err @ UpstreamError(e) => err -> (() => r(e.asLeft))
case UpstreamCompletion => UpstreamCompletion -> (() => r(None.asRight))
case o => o -> (() => r(new Error(s"received request in invalid state [$o]").asLeft))
}
}
F.delay(new AtomicReference[(State, () => Unit)]((Uninitialized, () => ()))).map { ref =>
new FSM[F, A] {
def nextState(in: Input): Unit = {
val (_, effect) = ref.updateAndGet { case (state, _) =>
step(in)(state)
}
effect()
}
def onSubscribe(s: Subscription): Unit = nextState(OnSubscribe(s))
def onNext(a: A): Unit = nextState(OnNext(a))
def onError(t: Throwable): Unit = nextState(OnError(t))
def onComplete(): Unit = nextState(OnComplete)
def onFinalize: F[Unit] = F.delay(nextState(OnFinalize))
def dequeue1: F[Either[Throwable, Option[Chunk[A]]]] =
F.async_[Either[Throwable, Option[Chunk[A]]]] { cb =>
nextState(OnDequeue(out => cb(Right(out))))
}
}
}
}
}