Skip to content

Instantly share code, notes, and snippets.

View tolitius's full-sized avatar

Anatoly tolitius

View GitHub Profile
@tolitius
tolitius / bootcamp.factorial.clj
Created February 2, 2012 04:36
clojure bootcamp: loop/recur vs. reduce vs. recursion
(ns bootcamp.factorial)
(defn fast-factorial [number]
(loop [n number factorial 1]
(if (zero? n)
factorial
(recur (- n 1) (* factorial n)))))
(defn fast-no-loop-factorial
([number] (fast-no-loop-factorial number 1))
@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 / 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
@tolitius
tolitius / server-with-reitit-routes.md
Created July 3, 2020 22:00
an example of starting a server with reitit routes

separate namespace for routes:

(ns hubble.routes
  (:require [reitit..]))

(defn make-routes [config]
  [["/api/hubble" {:swagger {:tags ["ops"]}}
    ;; ....
@tolitius
tolitius / Calculator.java
Created August 12, 2014 15:27
polymorphic calculator
public class Calculator {
public interface Operation {
public Long perform( Long a, Long b );
}
// example of anonymous class(es)
public static Operation add = new Operation() { public Long perform( Long a, Long b ) { return a + b; } };
public static Operation subtract = new Operation() { public Long perform( Long a, Long b ) { return a - b; } };
public static Operation divide = new Operation() { public Long perform( Long a, Long b ) { return a / b; } };
@tolitius
tolitius / zunionstore.py
Created February 27, 2012 06:20
Redis 'zunionstore' example for Chariot Day
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.zadd('votes:east', **{'riak': 1, 'couchdb': 1, 'cassandra': 1})
r.zadd('votes:north', **{'redis': 1, 'onetick': 1, 'couchdb': 1})
r.zadd('votes:west', **{'redis': 1, 'riak': 1, 'couchdb': 1})
r.zadd('votes:south', **{'voltdb': 1, 'mongodb': 1, 'hazelcast': 1})
r.zadd('votes:north:east', **{'hazelcast': 1, 'riak': 1, 'redis': 1})
r.zadd('votes:north:west', **{'redis': 1, 'cassandra': 1, 'onetick': 1})
@tolitius
tolitius / into-crux.md
Last active August 22, 2020 14:37
(get-in crux [:mindset])

several facts "at once"

=> (crux/submit-tx node [[:crux.tx/put {:crux.db/id :legend/picasso :kind :human}]
                         [:crux.tx/put {:crux.db/id :legend/picasso :planet :earth}]
                         [:crux.tx/put {:crux.db/id :legend/picasso :alive? :always}]
                         [:crux.tx/put {:crux.db/id :legend/picasso :location "Barcelona"}]])
@tolitius
tolitius / src.multi-method.clj
Created January 29, 2012 07:04
clojure bootcamp: "defmulti" example
;; inspired by suggestion in THE doc: http://java.ociweb.com/mark/clojure/article.html
(ns bootcamp.multi-method)
(defn measure-it [size]
(cond
(< size 3) :small
(< size 6) :medium
(< size 12) :large
:else :hard-to-measure ))
@tolitius
tolitius / jwks-repl.clj
Created March 30, 2020 23:23
how to use jwks with buddy
(require '[jsonista.core :as json]
'[org.httpkit.client :as http]
'[buddy.core.keys :as keys]
'[buddy.sign.jwt :as jwt])
(def mapper (json/object-mapper {:decode-key-fn keyword}))
;; if done fo real => check for http/get error
(defn jwks->pubkey [jwks-url]
(-> @(http/get jwks-url)
@tolitius
tolitius / 0. README.md
Last active March 17, 2020 18:05
kafka: tail last N messages

expects

  • kafka installed
  • zookeeper hostname
  • jq installed