Skip to content

Instantly share code, notes, and snippets.

View clarkmcc's full-sized avatar

Clark McCauley clarkmcc

View GitHub Profile
@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 / 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 / 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 / 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 / byte_counter.go
Last active March 19, 2020 04:52
Used for tracking metadata about a request and dumping it into a data warehouse such as Google BigQuery
package counter
import (
"cloud.google.com/go/civil"
"context"
"fmt"
"gitlab.com/ptmesh/api/pkg/repositories"
"go.mongodb.org/mongo-driver/bson/primitive"
"net/http"
"sync"
@clarkmcc
clarkmcc / throttler.go
Last active March 19, 2020 04:36
Throttler implements a leaky bucket request throttling algorithm where the duration between two requests is never less than the MinInterval
type ThrottleEngine struct {
// The key to this map represents a unique user being throttled, such as
// an api key, or some user identification obtained from a header
MostRecentRequest map[string]time.Time
MinInterval time.Duration
M *sync.Mutex
}
type HttpHandlerFunc func(http.ResponseWriter, *http.Request)
@clarkmcc
clarkmcc / get_arch_darwin.go
Created March 5, 2020 17:58
Golang function to determine whether a MacOS machine is 32 bit or 64 bit
func GetArch() (string, *errors.ErrorCode) {
cmd := exec.Command(
"getconf",
"LONG_BIT",
)
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("error getting arch: %v", err)
return "", errors.New(errors.UnsupportedArchitecture)
@clarkmcc
clarkmcc / get_arch_windows.go
Created March 5, 2020 17:56
Golang function to determine whether a windows machine is 32 bit or 64 bit.
func GetArch() (string, *errors.ErrorCode) {
cmd := exec.Command(
"wmic",
"os",
"get",
"osarchitecture",
)
output, err := cmd.CombinedOutput()
if err != nil {
@clarkmcc
clarkmcc / startup.go
Last active March 5, 2020 17:53
This package is a dependency installer for Golang. Dependencies can be registered with the startup.Engine, checked, and installed.
package startup
import (
"fmt"
"log"
"sync"
)
const BufferedChan int = 1
const FailedToInstallDependency = "failed to install dependency"
@clarkmcc
clarkmcc / aggregate.go
Created January 13, 2020 22:46
Handy functions for creating MongoDB aggregate pipelines in Go
package aggregate
import "go.mongodb.org/mongo-driver/bson"
type Operation bson.M
// The Pipe functions similar to the RxJS pipe function
// (https://rxjs-dev.firebaseapp.com/api/index/function/pipe) in that
// it accepts a set of functions as parameters. Each function receives
// as its input, the output of the previous function. The allows you