Thread pools on the JVM should usually be divided into the following three categories:
- CPU-bound
- Blocking IO
- Non-blocking IO polling
Each of these categories has a different optimal configuration and usage pattern.
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
t0 := time.Now() | |
t1 := t0 |
The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.
In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.
This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.
package transactions | |
import com.twitter.finagle.exp.mysql.{Client, OK, Result} | |
class Db(mysqlClient: Client) { | |
val insertOrder = "INSERT INTO orders (ref) VALUES(?)" | |
val insertOrderItem = "INSERT INTO order_items (order_id, item_id) VALUES(?, ?)" | |
def persistOrderWithItems[T](order: Order)(whenDone: (OK, Seq[Result]) => T): Future[T] = { |
// 1. create a new bookmark on your browser | |
// 2. paste the following code - starting at "javascript:" into the URL part of the add-bookmark menu. | |
// 3. go to http://www2.warnerbros.com/spacejam/movie/jam.htm | |
// 4. click the bookmark for automatic space jam 2 | |
// xoxo j$ | |
javascript:(function(){document.getElementsByTagName("html")[0].innerHTML=document.getElementsByTagName("html")[0].innerHTML.replace(/1996/g,"2014");var images=document.getElementsByTagName("img");var length=images.length;while(length--){if(images[length].src=="http://www2.warnerbros.com/spacejam/movie/img/p-jamlogo.gif"){images[length].src="http://f.cl.ly/items/1a10103W3Z010W192n23/p-jamlogo.png"}}}()); |
One of my favorite past times is to look at the notebooks of famous scientists. Da Vinci's notebook is well known, but there plenty others. Worshipping Da Vinci like no other, I bought a Think/Create/Record journal, used it mostly to keep jot down random thoughts and take notes. This was great in the beginning, but the conformity of lines drove me nuts. Only moleskines made blank notebooks, so I had to buy one.
At the same time I started a freelance project. The project itself is irrelevant, but suffice to say it was very complex and spanned several months. It seemed like a perfect opportunity to use the moleskine. Looking back, all my entries fell under few categories:
Latency Comparison Numbers (~2012) | |
---------------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns 3 us | |
Send 1K bytes over 1 Gbps network 10,000 ns 10 us | |
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD |
#!/bin/bash | |
PROJECT_NAME="$1" | |
SCALA_VERSION="2.9.1" | |
SCALATEST_VERSION="1.6.1" | |
MOCKITO_VERSION="1.8.5" | |
mkdir $PROJECT_NAME | |
cd $PROJECT_NAME | |