Skip to content

Instantly share code, notes, and snippets.

@danveloper
danveloper / Providers.groovy
Created December 11, 2012 22:46
This is how I implement the Provider pattern in Groovy.
package com.danveloper.providers
import com.danveloper.support.ApplicationHolder as AH
import org.hibernate.SessionFactory as SF
public interface Provider<T> {
T get()
}
class Support {
@danveloper
danveloper / overlapcollapse.groovy
Created February 28, 2013 17:37
collapse overlapping ranges in list
List.metaClass.collapse = {->
def start,end
def reverse = delegate[0]?.getAt(0)-delegate[0]?.getAt(-1) > 0
def expanded = delegate.flatten().unique().sort()
def result = (reverse ? expanded.reverse() : expanded).inject([]) { l, n ->
start = start == null ? n : start
end = end == null ? n : end
if ((reverse?n+1:n-1)==end) {
end = n
@danveloper
danveloper / Promises.java
Created April 16, 2013 19:40
Java 8 CompletableFuture (Promise) and Lambda example
public class Promises {
private static class PageMetrics {
Integer visits;
Long avgMsOnPage;
@Override
public String toString() {
return String.format("{ avgMsOnPage=%d, visits=%d }", avgMsOnPage, visits);
}
@danveloper
danveloper / TrampolineLambda.java
Created April 18, 2013 18:27
Trampoline in Java 8 with Lambdas and interface defaults
public class TrampolineLambda {
interface TrampolineFunction<T, R> {
R apply(T...obj);
public default Object trampoline(T...objs) {
Object result = apply(objs);
if (!(result instanceof TrampolineFunction)) {
return result;
} else {
return this;
  • Create a new folder
  • Create a build.gradle file
  • Inside the build.gradle file, put the following:
buildscript {
  repositories {
    jcenter()
  }
 dependencies {

Ratpack "Hello World" on a t2.micro, Ubuntu 14.04, no extra server configuration.

~30,000 req/s

# ./wrk -t4 -c72 -d60s -R35000 http://localhost:5050
Running 1m test @ http://localhost:5050
  4 threads and 72 connections
  Thread calibration: mean lat.: 823.764ms, rate sampling interval: 3041ms
 Thread calibration: mean lat.: 800.518ms, rate sampling interval: 2902ms

Keybase proof

I hereby claim:

  • I am danveloper on github.
  • I am danveloper (https://keybase.io/danveloper) on keybase.
  • I have a public key whose fingerprint is FE5B 4B85 F69A 746A 4485 0EB6 8878 7076 EB89 3653

To claim this, I am signing this object:

@danveloper
danveloper / 0_ConfigSlurperConfigSource.groovy
Created June 27, 2016 05:52
Groovy Script Config Source for Ratpack
package app
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import com.google.common.io.ByteSource
import com.google.common.io.Resources
import ratpack.config.ConfigSource
import ratpack.file.FileSystemBinding
import java.nio.file.Files
@danveloper
danveloper / FibBackoff.groovy
Last active June 23, 2016 04:38
Fibonacci Backoff Strategy
import groovy.transform.Memoized
class FibBackoff {
int tryNum
void backoff() {
tryNum = fib(tryNum)
sleep(tryNum * 1000)
}
@danveloper
danveloper / a.md
Last active April 14, 2016 08:45
Ratpack 0.9.13 Performance on EC2 c4.8xlarge

starting the app

* app starts with `-Dratpack.epoll=true` to enable the Netty `EpollEventLoop`
# ./wrk -t72 -c3000 -d30s http://localhost:5050
Running 30s test @ http://localhost:5050
  72 threads and 3000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.82ms    9.03ms 229.22ms   95.25%
    Req/Sec     9.58k     3.52k   34.54k    70.33%