Skip to content

Instantly share code, notes, and snippets.

@mrk21
mrk21 / fork_safe_proxy.rb
Last active May 2, 2024 07:17
Simple fork-safe proxy for Ruby
class ForkSafeProxy < BasicObject
class << self
def unwrap(instance)
instance.__send__(:__value__)
end
end
def initialize(&initializer)
@mutex = ::Thread::Mutex.new
@initializer = initializer
@mrk21
mrk21 / retryer.rb
Last active April 20, 2024 04:16
Exponential Backoff And Jitter for Ruby
class Retryer
class RetryError < StandardError; end
class Backoff
def call(attempt)
raise NotImplementedError
end
end
class ExponentialBackoffAndEqualJitter < Backoff
@mrk21
mrk21 / 01_thread_pool.rb
Last active April 20, 2024 04:16
Simple thread pool for Ruby
class ThreadPool
def initialize(n)
@n = n
end
def start
@queue = Thread::SizedQueue.new(@n)
@threads = @n.times.map do
Thread.new { self.exec }
end
@mrk21
mrk21 / env.rb
Last active March 9, 2024 10:49
Define a method liked `ENV.fetch()` which returns specified default value even when a value of specified key was empty string. see: https://mrk21.hatenablog.com/entry/2024/03/09/190844
class << ENV
# This method behavior is almost same as `ENV.fetch()`, but it returns default value even when the value was empty string.
def fetch2(*args, &block)
key, default = args
value = fetch(*args, &block)
return value if value != ''
raise KeyError, format('key not found: "%s"', key) if args.size == 1 && !block
warn('block supersedes default value argument', uplevel: 1) if args.size == 2 && block
block ? yield(key) : default
@mrk21
mrk21 / mecab_sort.rb
Last active February 1, 2023 06:09
Sort array by Kanji using MeCab on Ruby.
# $ brew install mecab
# $ brew install mecab-ipadic
# $ gem install natto
# $ ruby mecab_sort.rb
# "シュジンコウ"
# "ジョウシャ"
# "ジョウシャ"
# "コウムイン"
# "シュジンコウ"
# "コウムイン"
@mrk21
mrk21 / simple_mutex.ts
Created November 1, 2022 02:15
Simple mutex for JavaScript
class SimpleMutex {
_lock: boolean;
_resolves: Array<() => void>;
constructor() {
this._lock = false;
this._resolves = [];
}
async synchronize(callback: () => Promise<any>) {
@mrk21
mrk21 / deepcopy.go
Created October 21, 2022 06:46
Simple deep copy for Go(reflection/generics)
package main
import (
"encoding/json"
"fmt"
"reflect"
)
// Example:
//
@mrk21
mrk21 / wsl-forwarding.ps1
Created February 4, 2022 18:44
Auto port forwarding to WSL2
$HostIP = [IPAddress]"0.0.0.0"
function Invoke-WSL-Command([String]$Cmd) {
while (1) {
$Result = wsl bash -c $Cmd
if (!$?) {
Write-Debug "Retry...: $Cmd"
Start-Sleep 1
continue
}
@mrk21
mrk21 / network_ip.ps1
Created February 4, 2022 14:51
PowerScript: Network IP
function Get-Network-From-CIDR([String]$CIDR) {
$Parsed = $CIDR -split "/"
$IP = [IPAddress]$Parsed[0]
$Length = [Int]$Parsed[1]
$SubnetMask = [IPAddress](([Math]::Pow(2, 32) - 1) -bxor ([Math]::Pow(2, (32 - $Length)) - 1))
$IP = [IPAddress]($IP.Address -band $SubnetMask.Address)
return New-Object PSObject -Property @{
IP = $IP;
SubnetMask = $SubnetMask
}
@mrk21
mrk21 / gin-method-overriding-middleware.go
Last active November 28, 2021 12:06
Gin middleware. It override HTTP method by `_method` POST body parameter. It's for HTML forms.
package main
import (
"strings"
"github.com/gin-gonic/gin"
)
// Override HTTP method by `_method` POST body parameter. It's for HTML forms.
// @see [bu/gin-method-override: MethodOverride middleware for Gin web framework](https://github.com/bu/gin-method-override)