Skip to content

Instantly share code, notes, and snippets.

View zerojulian's full-sized avatar

Julian Peterson zerojulian

View GitHub Profile
@zerojulian
zerojulian / backoff_timer.go
Created October 27, 2023 06:13
A method that returns a backoff function that sleeps at increasingly longer intervals.
func CreateBackoffTimer(initial, max, resetAfter time.Duration) func() {
lastWaitAt := time.Now()
delay := initial
return func() {
if time.Since(lastWaitAt) > resetAfter {
delay = initial
}
time.Sleep(delay)
@zerojulian
zerojulian / parallel.go
Created September 25, 2023 23:19
A method to run a function X times in parallel, with Y worker threads
// Parallelize runs the specified number of tasks using the callback fn with the given number of worker threads
// the function argument takes the task number (counting from zero)
func Parallelize(nWorkers int, nTasks int, fn func(int)) {
wg := sync.WaitGroup{}
task := make(chan int, nTasks)
for t := 0; t < nTasks; t++ {
task <- t
}
close(task) // so it wont block when it's ranged over
@zerojulian
zerojulian / vergo
Last active September 12, 2023 11:22
A script to manage go versions
#!/usr/bin/env bash
#
# MIT License
#
# Copyright (c) 2023 ZeroFlucs
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@zerojulian
zerojulian / now.go
Last active September 12, 2023 11:21
basic (ie no options) program to convert between unix timestamps and formatted date/time strings
// now provides simple conversion between unix timestamps and formatted date/time strings
//
// # MIT License
//
// # Copyright (c) 2023 ZeroFlucs
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,