Skip to content

Instantly share code, notes, and snippets.

@hcoa
hcoa / CachedResource-Blog.md
Created December 16, 2021 15:55 — forked from Daenyth/CachedResource-Blog.md
CachedResource for cats-effect

Concurrent resource caching for cats

Motivation

cats-effect Resource is extremely handy for managing the lifecycle of stateful resources, for example database or queue connections. It gives a main interface of:

trait Resource[F[_], A] {
  /** - Acquire resource
    * - Run f
 * - guarantee that if acquire ran, release will run, even if `use` is cancelled or `f` fails
@hcoa
hcoa / .vimrc
Created September 1, 2021 11:34
set autoread " reload on external file changes
set backspace=indent,eol,start " backspace behaviour
set clipboard=unnamed,unnamedplus " enable clipboard
set encoding=utf8 " enable utf8 support
set hidden " hide buffers, don't close
set mouse=a " enable mouse support
set nowrap " disable wrapping
set number " show line numbers
set term=xterm-256color " terminal type
set wildmenu wildmode=longest:full,full " wildmode settings
@hcoa
hcoa / di-in-fp.md
Created May 25, 2021 08:56 — forked from gvolpe/di-in-fp.md
Dependency Injection in Functional Programming

Dependency Injection in Functional Programming

There exist several DI frameworks / libraries in the Scala ecosystem. But the more functional code you write the more you'll realize there's no need to use any of them.

A few of the most claimed benefits are the following:

  • Dependency Injection.
  • Life cycle management.
  • Dependency graph rewriting.
@hcoa
hcoa / LockFreeRateLimiter.scala
Created May 12, 2021 13:36
Scala lock-free rate limiter example
import java.util.concurrent.atomic.{AtomicLong, AtomicReference}
import scala.concurrent.duration._
trait Clock {
def now: Long
}
class SystemClock extends Clock {
def now: Long = System.nanoTime()
}
@hcoa
hcoa / NaiveRateLimiter.scala
Last active May 10, 2021 13:51
simple, thread unsafe rate-limiter, and simple token bucket rate-limiter
import java.time.Clock
import scala.annotation.tailrec
import scala.collection.mutable
case class Issue(tokens: Long, timestampMillis: Long)
class NaiveRateLimiter private (capacity: Long, periodMs: Long, clock: Clock) {
private var availableCapacity = capacity
private val issuedTokens = mutable.MutableList[Issue]()
@hcoa
hcoa / PY0101EN-5-1-Numpy1D.ipynb
Created June 23, 2020 09:39 — forked from shreya4599/PY0101EN-5-1-Numpy1D.ipynb
Created on Skills Network Labs
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hcoa
hcoa / latency.txt
Created January 13, 2017 12:38 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
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
@hcoa
hcoa / latency.markdown
Created October 23, 2016 13:25 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@hcoa
hcoa / gist:8c51a5b45860a46800af6528409e8db0
Created September 23, 2016 13:54 — forked from krmaxwell/gist:6431994
Hunting Trips: network traffic log analysis

originally written in Dec 2011, needs updating

Log analysis has always struck me as one of those things that gets too much superficial attention without enough attention to detail. That is, we know that we need to do it, but we don’t talk about how we need to do it. At best, we talk about making sure we collect and archive logs. Analysis plays second fiddle, even though in reality logs without analysis provide almost no value to an organization. And you’ll find greatest value in discovery of the earliest stages of an incident rather than in hindsight to understand what went wrong. Unfortunately, less than 1% of data breach investigations in the 2011 Verizon DBIR started with log analysis and review!

The analysis ideas I present below don’t even begin to represent a comprehensive view. And of course every network is different, so you will need to think about your specific needs. But this may get