Skip to content

Instantly share code, notes, and snippets.

View JohnMurray's full-sized avatar

John Murray JohnMurray

View GitHub Profile
@JohnMurray
JohnMurray / good-wait.scala
Last active January 16, 2018 21:41
A proper way to make a Future-wait (sleep) with Akka
import akka.actor.ActorSystem
import akka.pattern.Patterns.after
import scala.concurrent.duration._
val as = ActorSystem()
val x = after(2.seconds, as.scheduler, global, Future.successful(1)).flatMap { _ =>
Future {
// long operation that _starts_ after the delayed future completes
1
@JohnMurray
JohnMurray / bad-wait.scala
Created January 15, 2018 00:15
How _not_ to make a future wait.
Future {
Thread.sleep(1000)
}
@JohnMurray
JohnMurray / retry-with-backoff.scala
Last active January 15, 2018 08:43
Simple retries with future-based networking code in Scala (with back-off)
def networkRequestWithRetries()(implicit as: ActorSystem): Future[String] = {
networkRequest().recoverWith {
case NetworkException =>
println("retrying")
after(2.seconds, as.scheduler, global, Future.successful(1)).flatMap { _ =>
networkRequestWithRetries()
}
case t: Throwable => throw t
}
@JohnMurray
JohnMurray / retry.scala
Created January 14, 2018 10:21
Simple retries with future-based networking code in Scala (without back-off)
def networkRequestWithRetries(): Future[String] = {
networkRequest().recoverWith {
case NetworkException =>
println("retrying")
networkRequestWithRetries()
case t: Throwable => throw t
}
}
@JohnMurray
JohnMurray / net.scala
Created January 14, 2018 10:12
Mock networking API to produce errors
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Random
object NetworkException extends Throwable
def networkRequest(): Future[String] = {
// let's assume a high rate of failure to make our examples
// more interesting
if ((Random.nextFloat * 100) < 80)
#include <google/protobuf/arena.h>
using namespace google::protobuf;
void main() {
Arena arena;
auto msg = Arena::CreateMessage<MyProto>(&arena);
setup_myproto(msg, 1);
push_to_queue(msg);
#include <google/protobuf/arena.h>
using namespace google::protobuf;
void main() {
Arena arena;
auto msg = Arena::CreateMessage<MyProto>(&arena);
setup_myproto(msg);
push_to_queue(msg);
}
@JohnMurray
JohnMurray / challenge-2.md
Created August 10, 2017 21:28
Challenge 2 of Go lunch-and-learn

Web Server

Another strong component of Go is the standard library. It is structured much like the Unix/Linux operating system. There are lots of small components that can be combined together for more complex tasks. There are also indespensible, modern primitives such as the ability to create a networking server.

Goal

Create a web-server that exposes a typical AppNexus "status" endpoint. The web-server will support

@JohnMurray
JohnMurray / challenge-1.md
Last active August 16, 2017 07:41
Challenge 1 of go lunch-and-learn

Ping Pong

A big reason people love Go is the concurrency primitives. Let's try them out with a coding challenge.

Goal

Launch 2 go-routines that send messages to each other over a channel. One go-routine will send "Ping" and the other will send "Pong." Each go-routine can only respond after they've received a message from the other go-routine. The exception is that the go-routine sending "Ping" can send an initial message

@JohnMurray
JohnMurray / install-go.md
Created August 10, 2017 19:44
Instructions for setting up GoLang locally

Installing Go

Installation

Change Directory

cd ${HOME}

Install a binary distribution