-
Notifications
You must be signed in to change notification settings - Fork 3
/
StreamSupervisor.scala
179 lines (160 loc) · 8.17 KB
/
StreamSupervisor.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
/*
* Copyright 2022 Valdemar Grange
*
* 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 gql.interpreter
import cats.data._
import cats.effect._
import cats.implicits._
import cats.effect.std._
import fs2.{Chunk, Stream}
trait StreamSupervisor[F[_], A] {
def acquireAwait(stream: Stream[F, A]): F[(StreamToken, Either[Throwable, A])]
def release(token: Set[StreamToken]): F[Unit]
def freeUnused(token: StreamToken, resource: ResourceToken): F[Unit]
def changes: Stream[F, NonEmptyList[(StreamToken, ResourceToken, Either[Throwable, A])]]
}
object StreamSupervisor {
final case class State[F[_]](
unsubscribe: F[Unit],
allocatedResources: Vector[(ResourceToken, F[Unit])]
)
def apply[F[_], A](openTail: Boolean)(implicit F: Async[F]): Stream[F, StreamSupervisor[F, A]] = {
def removeStates(xs: List[State[F]]) = {
xs.traverse_(_.unsubscribe) >>
xs.traverse_(_.allocatedResources.toList.traverse_ { case (_, fa) => fa })
}
type StateMap = Map[StreamToken, State[F]]
fs2.Stream.resource(Supervisor[F]).flatMap { sup =>
fs2.Stream
.bracket(F.ref(Map.empty[StreamToken, State[F]]))(_.get.flatMap(xs => removeStates(xs.values.toList)))
.flatMap { (state: Ref[F, StateMap]) =>
fs2.Stream.eval(Queue.bounded[F, Chunk[(StreamToken, ResourceToken, Either[Throwable, A])]](1024)).map { q =>
new StreamSupervisor[F, A] {
override def acquireAwait(stream: Stream[F, A]): F[(Unique.Token, Either[Throwable, A])] =
F.uncancelable { _ =>
for {
token <- F.unique
head <- F.deferred[Either[Throwable, A]]
start <- F.deferred[Unit]
// close <- {
// def go(stream: fs2.Stream[F, (Either[Throwable, A], Long)]): fs2.Pull[F, Nothing, Unit] =
// stream.pull.uncons1
// .flatMap {
// case None => fs2.Pull.done
// case Some(((hd, i), tl)) =>
// fs2.Pull.eval(F.deferred[Either[Throwable, Unit]]).flatMap { die =>
// fs2.Pull
// .extendScopeTo(fs2.Stream.never[F].interruptWhen(die))
// .evalMap(back => sup.supervise(back.compile.drain))
// .flatMap { resourceFiber =>
// fs2.Pull.eval(F.unique).flatMap { resourceToken =>
// val updateStateF = state.modify { m =>
// m.get(token) match {
// // We are closed for business
// case None => (m, resourceFiber.cancel)
// case Some(state) =>
// val resourceEntry = (resourceToken, resourceFiber.cancel)
// val newEntry = state.copy(
// allocatedResources = state.allocatedResources :+ resourceEntry
// )
// val publish =
// if (i == 0) head.complete(hd).void
// else if (openTail) q.offer(Chunk((token, resourceToken, hd)))
// else F.unit
// (m + (token -> newEntry), publish)
// }
// }.flatten
// fs2.Pull.eval(updateStateF) >> go(tl)
// }
// }
// }
// }
// sup.supervise {
// start.get >> go(stream.attempt.zipWithIndex).stream.as(()).compile.drain
// }
// }.map(_.cancel)
// TODO find a more elegant way of holding fs2 Leases
// start <- F.deferred[Unit]
// Hold fs2 leases by:
// 1. allocate a token for the resource and a killSignal / releaseSignal
// 2. do data publishing
// 3. await one killSignal for every stream element/resource scope
close <- sup
.supervise {
{
fs2.Stream.eval(start.get) >> {
stream.attempt.zipWithIndex
.evalMap { case (a, i) =>
F.deferred[Unit].flatMap { killSignal =>
F.unique.flatMap { resourceToken =>
state.update { m =>
m.get(token) match {
// We are closed for business
case None => m
case Some(state) =>
val resourceEntry = (resourceToken, killSignal.complete(()).void)
val newEntry = state.copy(
allocatedResources = state.allocatedResources :+ resourceEntry
)
(m + (token -> newEntry))
}
} >> {
if (i == 0) head.complete(a).void
else if (openTail) q.offer(Chunk((token, resourceToken, a)))
else F.unit
} as killSignal.get
}
}
}
.map(fs2.Stream.eval(_))
.parJoinUnbounded
}
}.compile.drain
}
.map(_.cancel)
_ <- state.update(_ + (token -> State(close, Vector.empty)))
_ <- start.complete(())
} yield head.get tupleLeft token
}.flatten
override def release(tokens: Set[StreamToken]): F[Unit] =
F.uncancelable { _ =>
state
.modify(m => (m -- tokens, tokens.toList.flatMap(m.get(_).toList)))
.flatMap(removeStates)
}
override def freeUnused(token: StreamToken, resource: ResourceToken): F[Unit] =
F.uncancelable { _ =>
state.modify { s =>
s.get(token) match {
case None => (s, F.unit)
case Some(state) =>
val (release, keep) = state.allocatedResources.span { case (k, _) => k != resource }
val newState = state.copy(allocatedResources = keep)
(s + (token -> newState), release.traverse_ { case (_, fa) => fa })
}
}.flatten
}
override def changes: Stream[F, NonEmptyList[(StreamToken, ResourceToken, Either[Throwable, A])]] =
Stream
.fromQueueUnterminatedChunk(q)
.chunks
.map(_.toNel)
.unNone
}
}
}
}
}
}