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 / background_job.rs
Last active September 8, 2023 15:03
A dead simple background job processor written in Rust, using a double-ended queue and primitives sich as Arc and Mutex
use std::sync::{Arc, Mutex};
use std::sync::Condvar;
use std::thread;
use std::collections::HashMap;
use std::time::Duration;
struct Node<T> {
value: T,
next: Option<Arc<Mutex<Node<T>>>>,
previous: Option<Arc<Mutex<Node<T>>>>,
@leandronsp
leandronsp / socket.rs
Created August 5, 2023 16:55
A UNIX socket server written in Rust
// A UNIX socket server written in pure Rust
use std::io::Read;
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::Path;
fn main() {
let socket = Path::new("/tmp/echo.sock");
if socket.exists() {
@leandronsp
leandronsp / socket-server.go
Created August 5, 2023 16:11
A UNIX socket server written in Go
// A UNIX socket server written in Go, using the net package.
package main
import (
"fmt"
"net"
"os"
)
@leandronsp
leandronsp / background_job_test.go
Created August 5, 2023 02:15
A dead simple background processor in Go, using a double-ended queue based on a doubly linked list
// go mod init queues
// go mod tidy
// go get github.com/stretchr/testify/assert
// go mod tidy
// go test background_job_test.go
package queues
import (
"fmt"
@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 / 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 ====================
"
"