/
ServiceDSL.scala
358 lines (270 loc) · 11.4 KB
/
ServiceDSL.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package colossus.service
import com.typesafe.config.{Config, ConfigFactory}
import scala.concurrent.duration._
import scala.concurrent.Future
import scala.language.higherKinds
import java.net.InetSocketAddress
import akka.actor.Actor.Receive
import colossus.IOSystem
import colossus.controller.{Codec, Controller, Encoding}
import colossus.core.server.{Initializer, Server}
import colossus.core._
import colossus.metrics.MetricAddress
trait Protocol {
type Request
type Response
}
abstract class HandlerGenerator[T](ctx: InitContext) {
implicit val worker: WorkerRef = ctx.worker
val server: ServerRef = ctx.server
def fullHandler: T => ServerConnectionHandler
}
trait ServiceInitializer[T] extends HandlerGenerator[T] {
type RequestHandlerFactory = ServerContext => T
def onConnect: RequestHandlerFactory
def receive: Receive = Map()
def onShutdown() {}
}
trait ServiceDSL[T, I <: ServiceInitializer[T]] {
def basicInitializer: InitContext => HandlerGenerator[T]
class BridgeInitializer(init: InitContext, val serviceInitializer: I) extends Initializer(init) {
override def onConnect: (ServerContext) => ServerConnectionHandler = { ctx =>
serviceInitializer.fullHandler(serviceInitializer.onConnect(ctx))
}
override def receive: Receive = serviceInitializer.receive
override def onShutdown(): Unit = {
serviceInitializer.onShutdown()
}
}
/**
* Start service with an explicit [[colossus.core.ServerSettings]].
*
* @param name Service name
* @param settings Settings that configure the service
* @param init Initialization context to start the server
* @param io I/O system where work is executed
* @return Reference to the server
*/
def start(name: String, settings: ServerSettings)(init: InitContext => I)(implicit io: IOSystem): ServerRef = {
Server.start(name, settings) { i =>
new BridgeInitializer(i, init(i))
}
}
/**
* Start service with a particular port, but other settings will be loaded from the configuration file.
*
* @param name Service name
* @param port Port to open server on
* @param init Initialization context to start the server
* @param io I/O system where work is executed
* @return Reference to the server
*/
def start(name: String, port: Int)(init: InitContext => I)(implicit io: IOSystem): ServerRef = {
Server.start(name, port) { i =>
new BridgeInitializer(i, init(i))
}
}
/**
* Start service with settings loaded from the configuration file.
*
* @param name Service name
* @param init Initialization context to start the server
* @param io I/O system where work is executed
* @return Reference to the server
*/
def start(name: String)(init: InitContext => I)(implicit io: IOSystem): ServerRef = {
Server.start(name) { i =>
new BridgeInitializer(i, init(i))
}
}
/**
* Start a basic service with a particular port, but other settings will be loaded from the configuration file.
*
* @param name Service name
* @param port Port to open server on
* @param handler Logic that is applied to each request
* @param io I/O system where work is executed
* @return Reference to the server
*/
def basic(name: String, port: Int, handler: ServerContext => T)(implicit io: IOSystem): ServerRef = {
Server.start(name, port) { i =>
new Initializer(i) {
val requestInit = basicInitializer(i)
def onConnect: ServerContext => ServerConnectionHandler = { ctx =>
requestInit.fullHandler(handler(ctx))
}
}
}
}
}
/**
* A one stop shop for a fully working Server DSL without any custom
* functionality. Just provide a codec and you're good to go
*/
trait BasicServiceDSL[P <: Protocol] {
protected def provideCodec(): Codec.Server[P]
protected def errorMessage(reason: ProcessingFailure[P#Request]): P#Response
protected class ServiceHandler(rh: RequestHandler) extends ServiceServer[P](rh) {}
protected class Generator(context: InitContext) extends HandlerGenerator[RequestHandler](context) {
def fullHandler = rh => new PipelineHandler(new Controller(new ServiceHandler(rh), provideCodec()), rh)
}
abstract class Initializer(context: InitContext) extends Generator(context) with ServiceInitializer[RequestHandler]
abstract class RequestHandler(ctx: ServerContext, config: ServiceConfig) extends GenRequestHandler[P](ctx, config) {
def this(ctx: ServerContext) = this(ctx, ServiceConfig.load(ctx.name))
def unhandledError = {
case error => errorMessage(error)
}
}
object Server extends ServiceDSL[RequestHandler, Initializer] {
def basicInitializer = initContext => new Generator(initContext)
def basic(name: String, port: Int)(handler: PartialFunction[P#Request, Callback[P#Response]])(
implicit io: IOSystem) = start(name, port) { initContext =>
new Initializer(initContext) {
def onConnect = serverContext => new RequestHandler(serverContext) { def handle = handler }
}
}
}
}
/**
* This has to be implemented per codec in order to lift generic Sender traits to a type-specific trait
*
* For example this is how we go from ServiceClient[HttpRequest, HttpResponse] to HttpClient[Callback]
*/
trait ClientLifter[P <: Protocol, T[M[_]] <: Sender[P, M]] {
def lift[M[_]](baseClient: Sender[P, M], clientConfig: Option[ClientConfig])(implicit async: Async[M]): T[M]
}
/**
* A generic trait for creating clients. There are several more specialized
* subtypes that make more sense of the type parameters, so this trait should
* generally not be used unless writing very generic code.
*
* Type Parameters:
* * P - the protocol used by the client
* * M[_] - the concurrency wrapper, either Callback or Future
* * T - the type of the returned client
* * E - an implicitly required environment type, WorkerRef for Callback and IOSystem for Future
*/
trait ClientFactory[P <: Protocol, M[_], +T <: Sender[P, M], E] {
def defaultName: String
protected lazy val configDefaults = ConfigFactory.load()
/**
* Load a ServiceClient definition from a Config. Looks into `colossus.clients.clientName` and falls back onto
* `colossus.client-defaults`
* @param clientName The name of the client definition to load
* @param config A config object which contains at the least a `colossus.clients.clientName` and a `colossus.client-defaults`
* @return
*/
def apply(clientName: String, config: Config = configDefaults)(implicit env: E): T = {
apply(ClientConfig.load(clientName, config))
}
/**
* Create a Client from a config source.
*
* @param config A Config object in the shape of `colossus.client-defaults`. It is also expected to have the `address` and `name` fields.
* @return
*/
def apply(config: Config)(implicit env: E): T = {
apply(ClientConfig.load(config))
}
def apply(config: ClientConfig)(implicit env: E): T
def apply(hosts: Seq[InetSocketAddress])(implicit env: E): T = {
// TODO default request timeout should come from config
apply(hosts, 1.second)
}
def apply(host: String, port: Int)(implicit env: E): T = {
// TODO default request timeout should come from config
apply(host, port, 1.second)
}
def apply(host: String, port: Int, requestTimeout: Duration)(implicit env: E): T = {
apply(Seq(new InetSocketAddress(host, port)), requestTimeout)
}
def apply(address: Seq[InetSocketAddress], requestTimeout: Duration)(implicit env: E): T = {
val config = ClientConfig(
address = address,
requestTimeout = requestTimeout,
name = MetricAddress.Root / defaultName
)
apply(config)
}
}
trait ServiceClientFactory[P <: Protocol] extends ClientFactory[P, Callback, Client[P, Callback], WorkerRef] {
implicit def clientTagDecorator: TagDecorator[P]
def connectionHandler(base: ServiceClient[P], codec: Codec[Encoding.Client[P]]): ClientConnectionHandler = {
new UnbindHandler(new Controller[Encoding.Client[P]](base, codec), base)
}
def codecProvider: Codec.Client[P]
def apply(config: ClientConfig)(implicit worker: WorkerRef): Client[P, Callback] = {
new LoadBalancer[P](config, this, new PrinciplePermutationGenerator[Client[P, Callback]](_))
}
def createClient(config: ClientConfig, address: InetSocketAddress)(
implicit worker: WorkerRef): Client[P, Callback] = {
//TODO : binding a client needs to be split up from creating the connection handler
// we should make a method called "create" the abstract method, and have
// this apply call it, then move this to a more generic parent type
val base = new ServiceClient[P](address, config, worker.generateContext())
val handler = connectionHandler(base, codecProvider)
worker.worker ! WorkerCommand.Bind(handler)
base
}
}
object ServiceClientFactory {
def basic[P <: Protocol](name: String,
provider: () => Codec.Client[P],
tagDecorator: TagDecorator[P] = TagDecorator.default[P]) = new ServiceClientFactory[P] {
def defaultName = name
def codecProvider = provider()
override def clientTagDecorator: TagDecorator[P] = tagDecorator
}
}
class FutureClientFactory[P <: Protocol](base: FutureClient.BaseFactory[P])
extends ClientFactory[P, Future, FutureClient[P], IOSystem] {
def defaultName = base.defaultName
def apply(config: ClientConfig)(implicit io: IOSystem) = FutureClient.create(config)(io, base)
}
class CodecClientFactory[P <: Protocol, M[_], T[M[_]] <: Sender[P, M], E](
baseFactory: ClientFactory[P, M, Sender[P, M], E],
lifter: ClientLifter[P, T],
builder: AsyncBuilder[M, E])
extends ClientFactory[P, M, T[M], E] {
def defaultName: String = baseFactory.defaultName
def apply(config: ClientConfig)(implicit env: E): T[M] = {
apply(baseFactory(config), config)
}
def apply(sender: Sender[P, M], clientConfig: ClientConfig)(implicit env: E): T[M] = {
lifter.lift(sender, Some(clientConfig))(builder.build(env))
}
def apply(sender: Sender[P, M])(implicit env: E): T[M] = {
lifter.lift(sender, None)(builder.build(env))
}
}
/**
* Mixed into protocols to provide simple methods for creating clients.
*/
abstract class ClientFactories[P <: Protocol, T[M[_]] <: Sender[P, M]](lifter: ClientLifter[P, T]) {
implicit def clientFactory: FutureClient.BaseFactory[P]
implicit val futureFactory = new FutureClientFactory[P](clientFactory)
val client = new CodecClientFactory[P, Callback, T, WorkerRef](
clientFactory,
lifter,
AsyncBuilder.CallbackAsyncBuilder
)
val futureClient = new CodecClientFactory[P, Future, T, IOSystem](
futureFactory,
lifter,
AsyncBuilder.FutureAsyncBuilder
)
}
trait LiftedClient[P <: Protocol, M[_]] extends Sender[P, M] {
def address: InetSocketAddress = client.address()
def clientConfig: Option[ClientConfig]
def client: Sender[P, M]
implicit val async: Async[M]
override def send(input: P#Request): M[P#Response] = client.send(input)
protected def executeAndMap[T](i: P#Request)(f: P#Response => M[T]) = async.flatMap(send(i))(f)
override def addInterceptor(interceptor: Interceptor[P]): Unit = client.addInterceptor(interceptor)
override def disconnect(): Unit = client.disconnect()
override def update(addresses: Seq[InetSocketAddress]): Unit = client.update(addresses)
}
class BasicLiftedClient[P <: Protocol, M[_]](val client: Sender[P, M], val clientConfig: Option[ClientConfig])(
implicit val async: Async[M])
extends LiftedClient[P, M] {}