Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexander Emelin FZambia

  • Limassol, Cyprus
View GitHub Profile
@FZambia
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
FZambia / main.go
Last active January 25, 2023 02:31
Experimenting with QUIC and WebTransport in Go, see original post: https://centrifugal.github.io/centrifugo/blog/quic_web_transport/
View main.go
package main
import (
"context"
"crypto/tls"
"encoding/binary"
"errors"
"io"
"log"
"net/url"
@FZambia
FZambia / index.html
Created June 20, 2020 18:05
Centrifuge introduction example source code
View index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="https://rawgit.com/centrifugal/centrifuge-js/master/dist/centrifuge.min.js"></script>
</head>
<body>
<script type="text/javascript">
const centrifuge = new Centrifuge('ws://localhost:8000/websocket');
function drawText(text) {
const div = document.createElement('div');
@FZambia
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": [
"application/json"
],
"produces": [
@FZambia
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
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
github.com/centrifugal/centrifuge
---------------------------------
Incompatible changes:
- (*Client).Send: changed from func(github.com/centrifugal/protocol.Raw) error to func([]byte) error
- (*MemoryEngine).AddHistory: changed from func(string, *github.com/centrifugal/protocol.Publication, *ChannelOptions) (*github.com/centrifugal/protocol.Publication, error) to func(string, *github.com/centrifugal/protocol.Publication, *ChannelOptions) (StreamPosition, bool, error)
- (*Node).Disconnect: changed from func(string, bool) error to func(string, ...DisconnectOption) error
- (*Node).History: changed from func(string) ([]*github.com/centrifugal/protocol.Publication, 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
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 (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
View 0-rate-limiters.md

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
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 ...