Browse Source

Colossus: updating to latest release, improving efficiency

Dan Simon 10 years ago
parent
commit
c1c38afafa

+ 4 - 4
frameworks/Scala/colossus/build.sbt

@@ -1,14 +1,14 @@
 name := """colossus-example"""
 
-version := "0.1.0"
+version := "0.2.0"
 
-scalaVersion := "2.11.5"
+scalaVersion := "2.11.6"
 
 com.github.retronym.SbtOneJar.oneJarSettings
 
 mainClass in oneJar := Some("example.Main")
 
 libraryDependencies ++= Seq(
-  "com.tumblr" %% "colossus" % "0.6.1",
+  "com.tumblr" %% "colossus" % "0.6.4-RC1",
   "net.liftweb" %% "lift-json" % "2.6-RC1"
-)
+)

+ 88 - 24
frameworks/Scala/colossus/src/main/scala/example/Main.scala

@@ -3,36 +3,100 @@ package example
 import java.util.Date
 import java.text.SimpleDateFormat
 
-import colossus._
-import service._
-import protocols.http._
-import UrlParsing._
-import HttpMethod._
+import akka.actor.{Actor, ActorRef, Props}
+import akka.util.ByteString
+import scala.concurrent.duration._
+import colossus.IOSystem
+import colossus.core.{ServerRef, ServerSettings}
+import colossus.service._
+import colossus.protocols.http._
 
 import net.liftweb.json._
 import JsonDSL._
 
-object Main extends App {
+object BenchmarkService {
 
-  implicit val io_system = IOSystem()
+  class Timestamp(server: ServerRef) extends Actor {
+        
+    val sdf = new SimpleDateFormat("EEE, MMM d yyyy HH:MM:ss z")
+    case object Tick
+    import context.dispatcher
 
-  Service.become[Http]("sample", 9007) {
-    case request @ Get on Root / "json" => {
-      val json = ("message" -> "Hello, World!")
-      val sdf = new SimpleDateFormat("EEE, MMM d yyyy HH:MM:ss z")
-      val v = request.ok(compact(render(json)))
-        .withHeader("Content-Type", "application/json")
-        .withHeader("Server", "Colossus")
-        .withHeader("Date", sdf.format(new Date()))
-      Callback.successful(v)
+    override def preStart() {
+      self ! Tick
     }
-    case request @ Get on Root / "plaintext" => {
-      val sdf = new SimpleDateFormat("EEE, MMM d yyyy HH:MM:ss z")
-      val res = request.ok("Hello, World!")
-        .withHeader("Content-Type", "text/plain")
-        .withHeader("Server", "Colossus")
-        .withHeader("Date", sdf.format(new Date()))
-      Callback.successful(res)
+
+    def receive = {
+      case Tick => {
+        server.delegatorBroadcast(sdf.format(new Date()))
+        context.system.scheduler.scheduleOnce(1.second, self, Tick)
+      }
+    }
+  }
+  val response          = ByteString("Hello, World!")
+  val plaintextHeader   = ("Content-Type", "text/plain")
+  val jsonHeader        = ("Content-Type", "application/json")
+  val serverHeader      = ("Server", "Colossus")
+
+
+  def start(port: Int)(implicit io: IOSystem) {
+
+    val serverConfig = ServerSettings(
+      port = port,
+      maxConnections = 16384,
+      tcpBacklogSize = Some(1024)
+    )
+    val serviceConfig = ServiceConfig(
+      name = "/sample",
+      requestTimeout = Duration.Inf,
+      requestMetrics = false
+    )
+
+    val server = Service.serve[Http](serverConfig, serviceConfig) { context =>
+
+      ///the ??? is filled in almost immediately by the actor
+      var dateHeader = ("Date", "???")
+
+      context.receive {
+        case ts: String => dateHeader = ("Date", ts)
+      }
+      
+      context.handle { connection =>
+        connection.become{ case request =>
+          if (request.head.url == "/plaintext") {
+            val res = HttpResponse(
+              version  = HttpVersion.`1.1`,
+              code    = HttpCodes.OK,
+              data    = response,
+              headers = Vector(plaintextHeader, serverHeader, dateHeader)
+            )
+            Callback.successful(res)
+          } else if (request.head.url == "/json") {
+            val json = ("message" -> "Hello, World!")
+            val res = HttpResponse(
+              version  = HttpVersion.`1.1`,
+              code    = HttpCodes.OK,
+              data    = compact(render(json)),
+              headers = Vector(jsonHeader, serverHeader, dateHeader)
+            )
+            Callback.successful(res)
+          } else {
+            Callback.successful(request.notFound("invalid path"))
+          }
+        }
+      }
     }
+
+    val timestamp = io.actorSystem.actorOf(Props(classOf[Timestamp], server))
   }
-}
+
+}
+
+
+object Main extends App {
+
+  implicit val io_system = IOSystem()
+
+  BenchmarkService.start(9007)
+
+}