-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathGoRoutineAndChannel.scala
268 lines (216 loc) · 8.46 KB
/
GoRoutineAndChannel.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package app.impl.go
import java.util.UUID
import org.junit.Test
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future, Promise}
import scala.util.{Failure, Success, Try}
//***************************//
// DSL OF LIBRARY //
//***************************//
/**
* This implementation try to emulate how asynchronous programing works in Golang using
* [Go routines] and [channels].
*
* The library use the next component described below.
*/
object GoRoutineAndChannel {
/**
* Channel: ADT(Algebra data type) which contains as constructor a Promise of T defined in the Channel.
*/
case class Channel[T](var promise: Promise[T])
case class NoMoreElements()
case class Panic() extends Exception
/**
* makeChan[T]: Factory method which create an instance of [Channel using the type T]
*/
def makeChan[T]: Channel[T] = {
Channel(Promise[T]())
}
/**
*
* go: Operator make the function that we pass run asynchronously, and with the response of that function, in case
* we pass a Channel it will write the response in there.
*
* As the second argument for channel we use varargs so in case we dont provide a channel, we will make fire & forget
*
* @param func supplier function that it will be executed asynchronously.
* @param channels where it will write the response of the function
* @tparam T Type of the input and output type of the channel
**/
def go[T](func: () => T)(channels: Channel[T]*): Unit = {
Future {
if (channels.nonEmpty) {
channels.head.promise.success(func())
}
}
}
/**
* goCompose: Operator similar to [flatMap] to make the function that we pass run asynchronously and allow composition.
*
* @param func with input [composeChannel] where we will compose a previous channel response with the logic of the new one.
* @param channel where it will write the response of the function
* @param composeChannel previously used in another [go] or [goCompose] operator, and contains some response value to compose.
* @tparam Z Type of the input and output type of the channel
* @tparam T Type of the compose channel
*/
def goCompose[T, Z](func: Channel[Z] => T)(channel: Channel[T], composeChannel: Channel[Z]): Unit = {
Future {
channel.promise.success(func(composeChannel))
}
}
/**
* Operator to read from the channel the response of the asynchronous function.
* Just like in Golang the operator it's blocking until get the response, or the timeout it's reached.
*
* Once we're able to get the response from the future we will refresh the promise from the channel
*
* Just to make it looks like Golang, we return a tuple of possible value to the left, and the error to the right.
* This is something that definitely Scala do better with Monad Either where the Right is always the right effect value.
*/
def <=[T](channel: Channel[T], duration: Duration = 1 seconds): (T, NoMoreElements) = {
Try(Await.result(channel.promise.future, duration)) match {
case Success(response) =>
channel.promise = Promise()
(response, null)
case Failure(_) => (null.asInstanceOf[T], NoMoreElements())
}
}
}
import app.impl.go.GoRoutineAndChannel._
//***************************//
// EXAMPLE USE OF DSL //
//***************************//
class GoRoutineAndChannel {
/**
* In this example we just create the channel using [makeChan] and we pass together with a function
* to the operator [go] which it will make the function run asynchronously. Once we get the response
* we will fulfill the promise with the response of the function.
*
* Then from the channel extended method [<=] we will get the response of the Future generated previously by the promise.
*/
@Test
def asyncStringChannel(): Unit = {
val channel: Channel[String] = makeChan[String]
go(() => {
val newValue = UUID.randomUUID().toString
s"${Thread.currentThread().getName}-${newValue.toUpperCase}"
})(channel)
val (responseFromChannel, error) = <=(channel)
if (error != null) throw Panic()
println(s"Main thread:${Thread.currentThread().getName}")
println(responseFromChannel)
}
/**
* Same example than before but using another Type in the Channel, just to prove the DSL it has and use Strong type system
*/
@Test
def asyncFooChannel(): Unit = {
val channel: Channel[Foo] = makeChan[Foo]
go(() => {
Foo(s"${Thread.currentThread().getName}:I'm Foo type")
})(channel)
val (responseFromChannel, error) = <=(channel)
if (error != null) throw Panic()
println(s"Main thread:${Thread.currentThread().getName}")
println(responseFromChannel)
}
/**
* In this example since we dont pass any channel to the [go] routine operator, it wont be response write in the channel
* and it will be consider a Fire & Forget
*/
@Test
def asyncFireAndForget(): Unit = {
go(() => {
println(s"${Thread.currentThread().getName}-Fire & Forget")
})()
Thread.sleep(1000)
}
/**
* Using [goCompose] operator we're able to compose multiple [Channels] to get same effect than [flatMap]
* multiple monads.
* In this example we create three channels one per type, and we run three process sequentially async each.
* In every process we get the previous value, and it does not matter if the action to get the value it's blocking
* since the action also happens asynchronously
*/
@Test
def asyncCompositionChannel(): Unit = {
val channel1: Channel[String] = makeChan[String]
val channel2: Channel[Foo] = makeChan[Foo]
val channel3: Channel[Option[Foo]] = makeChan[Option[Foo]]
go(() => {
s"${Thread.currentThread().getName}:Hello composition in Scala go"
})(channel1)
goCompose[Foo, String](channel => {
val (previousValue, error) = <=(channel)
if (error != null) throw Panic()
Foo(previousValue)
})(channel2, channel1)
goCompose[Option[Foo], Foo](channel => {
val (previousValue, error) = <=(channel)
if (error != null) throw Panic()
Some(previousValue)
})(channel3, channel2)
val (responseFromChannel, error) = <=(channel3)
if (error != null) throw Panic()
println(s"Main thread:${Thread.currentThread().getName}")
println(responseFromChannel)
}
/**
* We can also specify the timeout in the operator to specify how much we want to wait for the response.
*/
@Test
def asyncChannelWithDuration(): Unit = {
val channel: Channel[Foo] = makeChan[Foo]
go(() => {
Foo(s"${Thread.currentThread().getName}:With timeout")
})(channel)
val (responseFromChannel, error) = <=(channel, 10 seconds)
if (error != null) throw Panic()
println(s"Main thread:${Thread.currentThread().getName}")
println(responseFromChannel)
}
/**
* In this test we prove that once we read a message from the channel the message is gone from
* the channel and if we try to read the message again we receive the Left side of the tuple
* with the [NoMoreElements]
*/
@Test
def asyncMultipleReadsInChannel(): Unit = {
val channel: Channel[String] = makeChan[String]
go(() => {
val newValue = UUID.randomUUID().toString
s"${Thread.currentThread().getName}-${newValue.toUpperCase}"
})(channel)
val responseFromChannel = <=(channel)
val responseFromChannel1 = <=(channel)
val responseFromChannel2 = <=(channel)
println(s"Main thread:${Thread.currentThread().getName}")
println(responseFromChannel)
println(responseFromChannel1)
println(responseFromChannel2)
}
@Test
def asyncMultipleWritesInChannel(): Unit = {
val channel: Channel[String] = makeChan[String]
go(() => {
val newValue = UUID.randomUUID().toString
s"${Thread.currentThread().getName}-${newValue.toUpperCase}"
})(channel)
val responseFromChannel = <=(channel)
println(responseFromChannel)
go(() => {
val newValue = UUID.randomUUID().toString
s"${Thread.currentThread().getName}-${newValue.toUpperCase}"
})(channel)
val responseFromChannel1 = <=(channel)
println(responseFromChannel1)
go(() => {
val newValue = UUID.randomUUID().toString
s"${Thread.currentThread().getName}-${newValue.toUpperCase}"
})(channel)
val responseFromChannel2 = <=(channel)
println(responseFromChannel2)
}
case class Foo(value: String)
}