Skip to content

Instantly share code, notes, and snippets.


Alexander Emelin FZambia

  • Limassol, Cyprus
View GitHub Profile
FZambia / pool.go
Created October 15, 2020 16:53
Goroutine (worker) pool for Go language
View pool.go
package gpool
import "context"
// Job represents function to be executed in worker.
type Job func()
type worker struct {
jobs chan Job
stop chan struct{}
FZambia / main.go
Last active January 25, 2023 02:31
Experimenting with QUIC and WebTransport in Go, see original post:
View main.go
package main
import (
FZambia / index.html
Created June 20, 2020 18:05
Centrifuge introduction example source code
View index.html
<!DOCTYPE html>
<html lang="en">
<script type="text/javascript" src=""></script>
<script type="text/javascript">
const centrifuge = new Centrifuge('ws://localhost:8000/websocket');
function drawText(text) {
const div = document.createElement('div');
FZambia / api.swagger.json
Created May 19, 2020 21:02
API Swagger JSON
View api.swagger.json
"swagger": "2.0",
"info": {
"title": "api.proto",
"version": "version not set"
"consumes": [
"produces": [
FZambia / logo.js
Created May 13, 2020 21:45
Logo javascript canvas
View logo.js
(function() {
'use strict';
window.addEventListener('load', function() {
var canvas = document.getElementById('canvas');
if (!canvas || !canvas.getContext) {
return false;
function rand(min, max) {
FZambia / release.txt
Created May 8, 2020 12:38
Centrifuge v0.8.0 release diff vs v0.7.0
View release.txt
$ gorelease -base=v0.7.0 -version=v0.8.0
Incompatible changes:
- (*Client).Send: changed from func( error to func([]byte) error
- (*MemoryEngine).AddHistory: changed from func(string, *, *ChannelOptions) (*, error) to func(string, *, *ChannelOptions) (StreamPosition, bool, error)
- (*Node).Disconnect: changed from func(string, bool) error to func(string, ...DisconnectOption) error
- (*Node).History: changed from func(string) ([]*, error) to func(string, ...HistoryOption) (HistoryResult, error)
- (*Node).Publish: changed from func(string, []byte, ...PublishOption) error to func(string, []byte, ...PublishOption) (PublishResult, error)
- (*Node).Unsubscribe: changed from func(string, string) error to func(string, string, ...UnsubscribeOption
FZambia / functions.js
Created March 3, 2020 18:57
Artillery io Centrifugo
View functions.js
This file has been truncated, but you can view the full file.
module.exports = { subscribe };
var items = [
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI5YmRhNWNlYy02MzRiLTQ2NWItOTNmMC00MTc5MWNmNTAyYzgifQ.DSJ1n4Zrf7CGSX15em3FWrL_4hbJXUFr_lebSQvU7go',
channel: 'notifications#9bda5cec-634b-465b-93f0-41791cf502c8'
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJiYWU3MTQxNy05MmVhLTRjMjMtODNkZi1mMDJlNTA2NTY0NmQifQ.lmfVqgTrGxX3Wy8nPC30852AbWnVUD9iJrWR209Qvw0',
channel: 'notifications#bae71417-92ea-4c23-83df-f02e5065646d'
View main.go
package main
import (

Scaling your API with rate limiters

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.

Request rate limiter

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.

FZambia / patch.diff
Created September 21, 2019 14:04
Klauspost compress library as replacement for std lib in Gorilla Websocket
View patch.diff
diff --git a/compression.go b/compression.go
index 813ffb1..4c492e0 100644
--- a/compression.go
+++ b/compression.go
@@ -41,16 +41,47 @@ func isValidCompressionLevel(level int) bool {
return minCompressionLevel <= level && level <= maxCompressionLevel
-func compressNoContextTakeover(w io.WriteCloser, level int) io.WriteCloser {
+// FlateWriter ...