Skip to content

Instantly share code, notes, and snippets.

View tolitius's full-sized avatar

Anatoly tolitius

View GitHub Profile
package main
import (
"context"
"fmt"
"time"
)
func consume(ctx context.Context, partition int) {
@tolitius
tolitius / nginx.conf
Last active June 24, 2023 10:03
openresty (nginx + lua): redis connection pooling
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
init_worker_by_lua_block {
redis = require("resty.redis")
}
server {
@tolitius
tolitius / nested-values.clj
Created January 7, 2017 09:00
specter: find nested values in a map
$ boot repl
boot.user=> (set-env! :dependencies '[[com.rpl/specter "0.13.2"]])
boot.user=> (require '[com.rpl.specter :as s])
boot.user=> (def m {:a 34 :b 28 :c {:bingo 40 :d 23 :e {:bingo 41}} :bingo 42})
#'boot.user/m
boot.user=> (s/select [(s/walker #(and (coll? %)
(= :bingo (first %))))
(s/view second)] m)

data store tools

These tools in reality would probably live behind a protocol for a polymorphic runtime dispatch (i.e. so they can be added to varous store types)

(defn select [conn query]
  (println "reading from" conn "query:" query)
  [1 2 3])

(defn insert [conn query]
@tolitius
tolitius / with-mount.md
Created December 29, 2016 04:47
starting / stopping an http server

this is the snippet that manages the http server lifecycle:

(defstate web-server :start (start-www config)
                     :stop (.stop web-server))

Trying it

@tolitius
tolitius / 1. pcheck.clj
Last active December 12, 2016 17:33
rules via lua's pcall
(defn pcall [f & args]
(try [true (apply f args)]
(catch Exception e
[false e])))
(defn matched? [[status value]]
(if status
(when (and value
(not= value :nginx-null))
value)
@tolitius
tolitius / spec-workshop-feedback.md
Last active April 8, 2018 19:51
clojure conj 2016: clojure.spec workshop feedback

While I have not used spec for work before, I followed the hype for several months, and really like the idea.

Overall this workshop, while was the first one of its kind, really "moved" me to actually apply spec to my current Clojure work. In both areas: open source and products for customers.

I believe a "needs improvement" feedback is the most useful one, so don't take it as something I did not like, but rather something others can benefit from in the future attending this workshop.

All that follows is definitely connected to the way I learn things, perceive and digest the information.

repl time

$ boot repl

boot.user=> (require :reload '[pipeline :refer [engine]]
                             '[mount.core :as mount])
"|| mounting... #'pipeline/engine"

starting it

@tolitius
tolitius / 1. cchange.clj
Last active November 1, 2016 18:25
restart on zoo / consul / etc .. config changes
(ns cchange
(:require [mount.core :as mount]))
(defprotocol ChangeListener
(add-watcher [this ks watcher])
(on-change [this k]))
(deftype RestartListener [watchers]
ChangeListener
@tolitius
tolitius / 0. multiple-kafka-consumer-threads.md
Last active April 8, 2023 17:36
event listener with multiple kafka consumer threads

starting multiple kafka consumer threads

  • Example below is based on gregor, but it could use other Kafka bindings.
  • Long CamelCased things come from Java
  • defstate comes from mount