Skip to content

Instantly share code, notes, and snippets.

View leandronsp's full-sized avatar

Leandro Proença leandronsp

  • Brazil
View GitHub Profile
@leandronsp
leandronsp / README.md
Last active August 4, 2023 03:28
A dead simple background job in Ruby, using a double ended queue linked list based

Sidequico

A dead simple background job written in pure Ruby, using a double ended queue (deque) based on a doubly linked list (dll). The deque has a blocking version based on primitives such as mutex and condition variable, useful for a background job.

Because we perform operations in the deque at constant time $O(1)$, we are able to implement rpoplpush easily in a performant way, enabling reliability through the use of a processing queue and a dead-letter queue.

Run the unit test:

ruby sidequico_test.rb
@leandronsp
leandronsp / linked_list_queue.rs
Created July 25, 2023 02:03
a queue using singly linked list in Rust (with tail node)
struct Node<T> {
value: T,
next: Option<Box<Node<T>>>
}
impl<T> Node<T> {
fn new(value: T) -> Self {
Self { value, next: None }
}
}
@leandronsp
leandronsp / woodpecker_algorithm.rb
Last active July 10, 2023 16:03
A fair load balancing distribution using the Woodpecker algorithm
require 'test/unit'
def woodpecker_distribution(items)
count = 0
distribution = { woodpecker: [], fink_fox: [], picked: {} }
for idx in 0...items.length
count += 1 unless distribution[:picked][idx]
next if distribution[:picked][idx]
@leandronsp
leandronsp / merge_sort_challenge.go
Last active July 8, 2023 00:05
MergeSort using a bottom-up approach on a singly linked list. This time in Go.
package sorting
// go mod init sorting
// go mod tidy
import (
"testing"
"github.com/stretchr/testify/assert"
)
@leandronsp
leandronsp / merge_sort_challenge.rb
Last active July 7, 2023 23:49
MergeSort using a bottom-up approach on a singly linked list
############################# sort #############################
# Sorts a given node applying a merge sort algorithm,
# using a bottom-up approach (iterative) on a singly linked list
def sort(node)
acc = node
steps = 1
while node
acc = sort_pass(acc, steps)
steps *= 2
@leandronsp
leandronsp / fib.rb
Last active June 22, 2023 23:42
Recursion in Ruby 3.2 (tail call, trampoline and TCO)
def fib(position)
return position if position < 2
fib(position - 1) + fib(position - 2)
end
def fib_tc(position, _current = 0, _next = 1)
return _current if position < 1
fib_tc(position - 1, _next, _current + _next)
@leandronsp
leandronsp / init.vim
Created May 1, 2023 16:56
My dotfiles as of 1st May 2023' (Neovim + Tmux)
" Mostly stolen from Yan Pritzer's most excellent Yadr (github.com/skwp/dotfiles)
" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible
set encoding=utf-8
" ================ General Config ====================
"
"
@leandronsp
leandronsp / chespirito.yaml
Last active February 21, 2023 20:46
Running Chespirito in Kubernetes
apiVersion: apps/v1
metadata:
labels:
app: chespirito-pod
name: chespirito-pod
spec:
replicas: 1
selector:
matchLabels:
app: chespirito-pod
@leandronsp
leandronsp / triggers-rbac.yaml
Created February 19, 2023 16:53
Tekton Event Listener RBAC
apiVersion: v1
kind: ServiceAccount
metadata:
name: tekton-service-account
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: tekton-triggers-minimal
rules:
@leandronsp
leandronsp / server.rb
Created February 15, 2023 11:39
HTTP server using Ractors (Ruby 3)
require 'socket'
@queue = Ractor.new do
loop do
Ractor.yield(Ractor.receive, move: true)
end
end
listener = Ractor.new(@queue) do |queue|
socket = TCPServer.new(PORT)