-
Notifications
You must be signed in to change notification settings - Fork 595
/
Topic.scala
145 lines (128 loc) · 5.53 KB
/
Topic.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
/*
* 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 concurrent
import cats.effect._
import cats.syntax.all._
import cats.effect.std.{Queue => Q}
import scala.collection.immutable.LongMap
/** Topic allows you to distribute `A`s published by an arbitrary
* number of publishers to an arbitrary number of subscribers.
*
* Topic has built-in back-pressure support implemented as the maximum
* number of elements (`maxQueued`) that a subscriber is allowed to enqueue.
*
* Once that bound is hit, any publishing action will semantically
* block until the lagging subscriber consumes some of its queued
* elements.
*/
abstract class Topic[F[_], A] { self =>
/** Publishes elements from source of `A` to this topic.
* [[Pipe]] equivalent of `publish1`.
*/
def publish: Pipe[F, A, Nothing]
/** Publishes one `A` to topic.
*
* This operation does not complete until after the given element
* has been enqued on all subscribers, which means that if any
* subscriber is at its `maxQueued` limit, `publish1` will
* semantically block until that subscriber consumes an element.
*
* A semantically blocked publication can be interrupted, but there is
* no guarantee of atomicity, and it could result in the `A` being
* received by some subscribers only.
*
* Note: if `publish1` is called concurrently by multiple producers,
* different subscribers may receive messages from different producers
* in a different order.
*/
def publish1(a: A): F[Unit]
/** Subscribes for `A` values that are published to this topic.
*
* Pulling on the returned stream opens a "subscription", which allows up to
* `maxQueued` elements to be enqueued as a result of publication.
*
* If at any point, the queue backing the subscription has `maxQueued` elements in it,
* any further publications semantically block until elements are dequeued from the
* subscription queue.
*
* @param maxQueued maximum number of elements to enqueue to the subscription
* queue before blocking publishers
*/
def subscribe(maxQueued: Int): Stream[F, A]
/** Like `subscribe`, but represents the subscription explicitly as
* a `Resource` which returns after the subscriber is subscribed,
* but before it has started pulling elements.
*/
def subscribeAwait(maxQueued: Int): Resource[F, Stream[F, A]]
/** Signal of current active subscribers.
*/
def subscribers: Stream[F, Int]
/** Returns an alternate view of this `Topic` where its elements are of type `B`,
* given two functions, `A => B` and `B => A`.
*/
def imap[B](f: A => B)(g: B => A): Topic[F, B] =
new Topic[F, B] {
def publish: Pipe[F, B, Nothing] = sfb => self.publish(sfb.map(g))
def publish1(b: B): F[Unit] = self.publish1(g(b))
def subscribe(maxQueued: Int): Stream[F, B] =
self.subscribe(maxQueued).map(f)
def subscribeAwait(maxQueued: Int): Resource[F, Stream[F, B]] =
self.subscribeAwait(maxQueued).map(_.map(f))
def subscribers: Stream[F, Int] = self.subscribers
}
}
object Topic {
/** Constructs a Topic */
def apply[F[_], A](implicit F: Concurrent[F]): F[Topic[F, A]] =
F.ref(LongMap.empty[Q[F, A]] -> 1L)
.product(SignallingRef[F, Int](0))
.map { case (state, subscriberCount) =>
new Topic[F, A] {
def publish1(a: A): F[Unit] =
state.get.flatMap { case (subs, _) =>
subs.foldLeft(F.unit) { case (op, (_, q)) =>
op >> q.offer(a)
}
}
def subscribeAwait(maxQueued: Int): Resource[F, Stream[F, A]] =
Resource
.eval(Q.bounded[F, A](maxQueued))
.flatMap { q =>
val subscribe = state.modify { case (subs, id) =>
(subs.updated(id, q), id + 1) -> id
} <* subscriberCount.update(_ + 1)
def unsubscribe(id: Long) =
state.update { case (subs, nextId) =>
(subs - id, nextId)
} >> subscriberCount.update(_ - 1)
Resource
.make(subscribe)(unsubscribe)
.as(Stream.fromQueueUnterminated(q))
}
def publish: Pipe[F, A, Nothing] =
_.evalMap(publish1).drain
def subscribe(maxQueued: Int): Stream[F, A] =
Stream.resource(subscribeAwait(maxQueued)).flatten
def subscribers: Stream[F, Int] = subscriberCount.discrete
}
}
}