Skip to content

Instantly share code, notes, and snippets.

View danielhopkins's full-sized avatar

Dan Hopkins danielhopkins

View GitHub Profile
@danielhopkins
danielhopkins / description
Created January 4, 2012 03:22
Full chunked sinatra
Got this working under jruby 1.6.5 with trinidad as a webserver (webbrick doesn't seem to work)
class TryIt
def []=(k,v) return "I'm a return value, if I'm not returned where do I go?" end
end
t = TryIt.new
puts(t['blah'] = "It would be slightly surprising if I were returned, right?")
#=> "It would be slightly surprising if I were returned, right?"
@danielhopkins
danielhopkins / InstallCert.java
Created May 6, 2012 15:44
Connecting to an untrusted certificate in java
/*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
@danielhopkins
danielhopkins / nio.scala
Created December 15, 2012 22:59
Exploration of nio java
import java.io._
import java.nio._
import java.nio.channels._
/*
Initially I wanted to explore the nio api for file access and I ended up coming across a SO article
saying that nio isn't faster than just plain 'ol FileOutputStream. Curious about this I came up with
a benchmark suite and quickly found that is indeed true. After a couple of iterations I was able to
beat the default, but clearly it's not easy or obvious.
@danielhopkins
danielhopkins / t.scala
Last active December 13, 2015 19:48
Timing out futures using Scala promises
// We're trying to express a future that can timeout but I don't
// want to use Await and block the current thread. I need to return
// a future that is either timed out or the value of the completed future
import concurrent.ExecutionContext.Implicits._
import concurrent.Future
import concurrent._
import concurrent.duration._
import akka.pattern.after
import scala.util.{Try, Success, Failure}
@danielhopkins
danielhopkins / gist:5162770
Created March 14, 2013 16:22
git push mirror
# from .git_functions
function git_helpers_clean_remote() {
remote=$1
git ls-remote -h $remote |
git show-ref --heads --exclude-existing |
cut -f 2 | xargs git push --delete $remote
}
# from .gitconfig
[alias]
@danielhopkins
danielhopkins / timeout.scala
Created March 17, 2013 20:58
Timeouts for actors
/*
The general idea is to handle timeouts as a message instead of using callbacks or worse yet, blocking
the actor (as I do below)
*/
val system = ActorSystem()
val myactor = system.actorOf(Props[MyActor])
val someotheractor = system.actorOf(Props[SomeotherActor])
class MyActor extends Actor with ActorLogging {
class EchoActor extends Actor with ActorLogging {
val cluster = Cluster(context.system)
override def preStart() { cluster.subscribe(self, classOf[LeaderChanged]) }
override def postStop() { cluster.unsubscribe(self) }
def receive = {
case state: CurrentClusterState => log.info(s"State is $state")
case LeaderChanged(leader) => log.info(s"Leader is $leader")
case msg => {

This link