4. Information with
Indeterminate/unbounded size
• Lines from a text file
• Bytes from a binary file
• Chunks of data from a TCP connection
• TCP connections
• Data from Kinesis or SQS or SNS or Kafka or…
• Data from an API with paged implementation
12. case
class
Await[+F[_],
A,
+O](
req:
F[A],
rcv:
(EarlyCause
/
A)
=>
Process[F,
O]
)
extends
Process[F,
O]
13. Composition Options
Process1[I,
O]
-‐
Stateful
transducer,
converts
I
=>
O
(with
state)
-‐
Combine
with
“pipe”
Channel[F[_],
I,
O]
-‐
Takes
I
values,
runs
function
I
=>
F[O]
-‐
Combine
with
“through”
or
“observe”.
Sink[F[_],
I]
-‐
Takes
I
values,
runs
function
I
=>
F[Unit]
-‐
Add
with
“to”.
14. Implementing
Server-sent Events (SSE)
This specification defines an API for
opening an HTTP connection for
receiving push notifications from a
server in the form of DOM events.
15. case
class
SSEEvent(eventName:
Option[String],
data:
String)
data:
This
is
the
first
message.
data:
This
is
the
second
message,
it
data:
has
two
lines.
data:
This
is
the
third
message.
event:
add
data:
73857293
event:
remove
data:
2153
event:
add
data:
113411
Example streams
16. We want this type:
Process[Task,
SSEEvent]
“A potentially infinite stream of SSE event messages”
17. async.boundedQueue[A]
• Items added to queue are removed in same order
• Connect different asynchronous domains
• Methods:
def
enqueueOne(a:
A):
Task[Unit]
def
dequeue:
Process[Task,
A]
18. HTTP Client
Implementation
• Use Apache AsyncHTTPClient
• Hook into onBodyPartReceived callback
• Use async.boundedQueue to convert chunks into
stream
25. • Split at line endings
• Convert ByteVector into UTF-8 Strings
• Partition by SSE “tag” (“data”, “id”, “event”, …)
• Emit accumulated SSE data when blank line found
26. • Split at line endings
ByteVector
=>
Seq[ByteVector]
• Convert ByteVector into UTF-8 Strings
ByteVector
=>
String
• Partition by SSE “tag” (“data”, “id”, “event”, …)
String
=>
SSEMessage
• Emit accumulated SSE data when blank line found
SSEMessage
=>
SSEEvent
27. Handling Network Errors
• If a network error occurs:
• Sleep a while
• Set up the connection again and keep going
• Append the same Process definition again!