Skip to content

Instantly share code, notes, and snippets.

View clarkmcc's full-sized avatar

Clark McCauley clarkmcc

View GitHub Profile
@clarkmcc
clarkmcc / stopinformer.go
Last active April 17, 2020 00:06
I developed this package to handle cases where I wanted to stop running goroutines and verify that they were in fact stopped. This can be paired up with a map to track named goroutines and inform respective goroutines to shut down or start again.
package concurrency
type SingleStructChan chan struct{}
type SingleAckerChan chan *stopAcker
type DoubleStructChan chan chan struct{}
type StopInformer interface {
// Stop blocks until the stop informer has received confirmation that the resource has stopped
Stop()
@clarkmcc
clarkmcc / stopinformermap.go
Created April 17, 2020 00:06
StopInformerMap provides an easy interface for interacting with named stop informers
package concurrency
import (
"sync"
)
// StopInformerMap provides an easy interface for interacting with named stop informers
type StopInformerMap interface {
// Starts a new named stop informer
Start(name string, informer StopInformer)
@clarkmcc
clarkmcc / resilio.yaml
Last active October 31, 2021 11:36
A Kubernetes implementation of Resilio Sync
apiVersion: v1
kind: Namespace
metadata:
name: resilio
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: resilio-pv-volume
namespace: resilio
@clarkmcc
clarkmcc / expiringcounter.go
Last active July 25, 2020 15:01
A thread-safe counter that can optionally be expired. The expiration works that way a rate bucket works, where the counter is reset repeatedly every time the counter has expired. This counter is useful for questions like, "how often did x happen over the course of y?"."
package threadsafe
import (
"sync"
"time"
)
// Handy for comparisons
var ZeroTime = time.Time{}
@clarkmcc
clarkmcc / expiringerrorcounter.go
Created July 25, 2020 15:10
ExpiringErrorCounter maintains a registry of counter errors that expires after a specified amount of time. This package also supports registering even handlers that will be called if the counter of a particular error crosses a threshold in the specified amount of time.
package threadsafe
import (
"sync"
"time"
)
// Based on https://gist.github.com/clarkmcc/2c40db4bb7b3aea412d78c8a490f030e
// ExpiringErrorCounter maintains a registry of counter errors that expires
// after a specified amount of time. For example if the expiration were set
@clarkmcc
clarkmcc / batcher.go
Last active September 16, 2020 23:44
A zero dependency, callback based batcher
package reflector
import (
"sync"
)
// Batcher knows how to batch a slice of objects, calling do for each batch
// until Next returns false.
//
// batcher.Next(func(objs []interface{}) {
@clarkmcc
clarkmcc / main.go
Created March 16, 2021 18:43
Get all filenames inside an Golang embedded filesystem.
func getAllFilenames(fs *embed.FS, path string) (out []string, err error) {
if len(path) == 0 {
path = "."
}
entries, err := fs.ReadDir(path)
if err != nil {
return nil, err
}
for _, entry := range entries {
fp := filepath.Join(path, entry.Name())
@clarkmcc
clarkmcc / delete-shutdown-pods.sh
Last active May 11, 2022 12:15
Deletes Kubernetes pods with a status of Shutdown that are usually left over after a GKE pre-emptible node goes offline.
#!/bin/sh
for namespace in $(kubectl get ns | awk '{print $1}' | grep -v NAME); do
for pod in $(kubectl get pod -n $namespace --field-selector=status.phase!=Running | grep Shutdown | awk '{print $1}' | grep -v NAME); do
kubectl delete pod -n "$namespace" "$pod"
done
done
@clarkmcc
clarkmcc / cloudbuild.yaml
Created January 2, 2022 21:32
Builds, pushes, and deploys a container image to Google Cloud Run, just set the $PROJECT_NAME variable
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/$PROJECT_NAME:$SHORT_SHA', '--cache-from', 'gcr.io/$PROJECT_ID/$PROJECT_NAME', '.']
# Push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/$PROJECT_NAME:$SHORT_SHA']
# Deploy to Google Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
@clarkmcc
clarkmcc / diffie-hellman.js
Created January 28, 2022 15:55
A naive and basic implementation of the Diffie-Hellman key exchange to more easily understand how the math works
// Diffie-Hellman asymetric key exchange allows two
// parties to cooperatively create a shared secret
// key without ever exchanging the shared secret.
// This means that it will be nearly impossible for
// a malicious party observing the creation of the
// shared secret to determine the secret key.
// Randomly create a generator number and a number p
// These two numbers are shared between both parties
// in the public space which means they're potentially