Skip to content

Instantly share code, notes, and snippets.

@tobnee
tobnee / Recursion.scala
Created March 7, 2011 08:56
Scala script which demonstrates how tail recursion can be applied in Scala
/*
* Scala script which demonstrates how tail recursion can be applied in
* Scala and what the value of tail recursion is in terms of applicability
* The functions in this example add all a values (which can be divided by 3)
* up to a given max.
*/
// help function to find the first value which can be divided by 3
def findStart(upTo:Int):Int = {
if (upTo%3==0 || upTo<0) upTo
@tobnee
tobnee / pimpmyjava.scala
Created March 7, 2011 09:12
Easy ways to pimp you Java with Scala while being fully compatible with the Java ecosystem
import scala.reflect.BeanProperty
// Easy ways to pimp you Java with Scala while being fully compatible with the Java ecosystem
// java bean scala
case class Person(@BeanProperty var name:String,
@BeanProperty var alter:Int) {
import System.{currentTimeMillis => currTime}
protected val creationTime = currTime
def lifeTime = currTime - creationTime
@tobnee
tobnee / gist:3675523
Created September 8, 2012 14:40
Basic wrapping of guava cache in Scala
import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache
import com.google.common.cache.Cache
object CacheUtil {
implicit def functionToCacheLoader[F, T](f: F => T) = {
new CacheLoader[F, T] {
def load(key: F) = f(key)
}
@tobnee
tobnee / gist:3808506
Created September 30, 2012 21:33
ChemVector Integration
<!-- knockout js bind -->
<div data-bind="visible: searchable">
<div class="well">
<strong class="control-label">Name:</strong>
<p id="selectstype" data-bind="text: selectedProduct().name"> </p>
<strong class="control-label">CAS:</strong>
<p id="selectc" data-bind="text: selectedProduct().cas"> </p>
<div class="cell" id="resize-cell">
<div class="structure" id="resize-structure">
<object width="300" height="150" class="chemvector-object" id="resize-object">
@tobnee
tobnee / gist:3820858
Created October 2, 2012 16:40
Render ChemVector (Jquery/Knockout)
<!DOCTYPE html>
<html lang="en">
<head>
<title>mols</title>
<script src="/assets/javascripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/assets/javascripts/jquery-ui-1.8.20.custom.min.js" type="text/javascript"></script>
<script src="/assets/javascripts/knockout-2.1.0.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/assets/stylesheets/chemvector.css" />
<link rel="stylesheet/chemical" type="application/json" href="/assets/stylesheets/chemistry.json" />
<script src="/assets/javascripts/chemvector.js"></script>
@tobnee
tobnee / CNPlay.scala
Last active December 11, 2015 11:28
Play2 based content negotiation
import play.api.mvc._
import play.api.mvc.Results._
import play.api.http.MimeTypes._
def resource() = Action { implicit request =>
negotiate(
HTML ->
(() => Ok("html")),
JSON ->
(() => Ok("json")),
import akka.actor.{ActorSystem, Props, Actor}
import collection.mutable
import play.api.cache.Cache
import concurrent.Future
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._
import play.api.libs.concurrent.Akka._
object AsyncCache {
trait Github {
def login(cred:Credentials) = {
val response: Future[Response] = WS
.url(models.github.baseUrl+"/user")
.withAuth(cred.username, cred.password, AuthScheme.BASIC)
.head()
response.onSuccess{ case response =>
Cache.set(cred.password+""+"passwd", response)
}
@tobnee
tobnee / PimpAcceptHeader.scala
Created September 26, 2013 20:42
Change the Accept header based on a query parameter in Play. (http://mysite/stuff/1?as=json)
import play.api.http.{ HeaderNames, MimeTypes }
import play.api.mvc.{ Headers, Handler, RequestHeader }
import scala.collection.mutable.ArrayBuffer
import play.api.GlobalSettings
object Global extends GlobalSettings {
override def onRequestReceived(request: RequestHeader): (RequestHeader, Handler) = {
val defaultHeader = super.onRequestReceived(request)
val (header, handler) = defaultHeader

Phil Calçado lernte ich bereits vor einiger Zeit auf einem Event in Berlin kennen und hatte daher hohe Erwartungen an seinen Vortrag APIs: The Problems with Eating your Own Dog food. Ich sollte nicht enttäuscht werden. Nachdem die Frage: "Was ist Soundcloud?" und die damit verbundenen Anforderungen an Performanz geklärt waren, wurde dargestellt, wie sich die Soundcloud Architektur über die Zeit entwickelt hat.

Während dieser Evolution wurde zeitweise das Rendern der Webseite auf den Browser verlagert, was zu ungefähr 159 API-Anfragen pro Seitenanfrage führte. Nicht nur dem Publikum machte diese Zahl Angst sondern auch der Soundcloud Crew, jedoch war man zuversichtlich dass durch zusätzliche Server diese Last zu schaffen sei. Schnell lernte man, dass nach dem Zusammenbruch des HAProxy weitere Komponenten (memcached/Rails/MySQL) der Masse an Anfragen nicht gewachsen waren.