Skip to content

Instantly share code, notes, and snippets.

View xeoncross's full-sized avatar

David Pennington xeoncross

View GitHub Profile
@xeoncross
xeoncross / int_bits.go
Created June 22, 2022 17:36
Examples of bit lengths of different integer values: https://go.dev/play/p/gLZKA9B233I
package main
import (
"encoding/binary"
"fmt"
)
func main() {
// subtract 1 from each answer since we're counting zero also
fmt.Printf("%d bits = %8b = %d\n", 2, byte(1<<2-1), 1<<2)
@xeoncross
xeoncross / delta_encoding_demo.go
Created June 22, 2022 02:13
Encoding a sorted sequence of integers using delta encoding can save 40%-60% on storage costs: https://go.dev/play/p/KdLslro7n1n
package iiexperiments
import (
"fmt"
"testing"
)
// How much space will using a delta encoding save?
// If we assume we have 1 byte (0-255) to tell us how many bytes the delta is contained in, we can then only store the diff between the last and next int values. This difference is the "delta".
@xeoncross
xeoncross / README.md
Created June 20, 2022 20:00
Distributed rate limiting with non-whole time periods (max 3 requests every 5 seconds)

Rate Limiting Redis with multiples of a time.Duration

In a distributed system, you want the API layer to be stateless. So the app can offload rate limiting to a redis instance. This way, you can use the INCR to both increment and return the number of requests a given IP/Client/API Key has made across any number of App / serverless instances.

However, what do you do if the limit isn't roundable to the current second/minute/hour/day? How does each instance agree which key to use? The answer is to modulo the previous and future keys to find the right one.

For example, 3 requests every 5 seconds is the desired limit. That means we can use -max-1:+max-1 as the range of values to scan to find the key we should use.

max := 5
@xeoncross
xeoncross / string_interface.go
Created June 4, 2022 22:52
Example of creating a string that implements an interface in Go / golang: https://go.dev/play/p/-IC8BGzjPPG
package main
import "fmt"
type A string
func (a A) Foo() {
fmt.Printf("Foo: %q\n", a)
}
@xeoncross
xeoncross / csgo.mac.md
Last active April 27, 2022 02:42
CS:GO on Macs

Intel iMac

i7 32GB RAM AMD Radeon R9 M295X 4 GB

Native Mac Steam Lib

Decent FPS (50-100FPS), but stalls and lags at random times.

@xeoncross
xeoncross / .env
Last active December 28, 2021 01:13
Quick docker setup of new database for building out project DDL / UML for RDBMS entities
MYSQL_HOST=mysqldb
MYSQL_USER=design
MYSQL_PASS=design
MYSQL_NAME=design
MYSQL_PORT=3306
@xeoncross
xeoncross / the_future_of_go.md
Last active August 2, 2023 16:47
The future of fast MVP's using Go and Typescript using code generation

The future of fast MVP's using Go and React

As Go matures, no-code (if you squint) solutions are making their way into the ecosystem. This means faster MVP's and less errors writing out the same CRUD project-after-project.

  1. Starting with just your sql statements, you use sqlc to generate the models and entities
  2. Write your actual service/business logic using the entities and interfaces sqlc generated (wish list: skip this step)
  3. Use goa to generate your OpenAPI HTTP or gRPC webserver
  4. Have openapi-generator produce the Typescript SDK you're UI will use to call it
  5. Build out your interface
  6. Publish (web, app store, etc..)
@xeoncross
xeoncross / common_crawl_corpus_in_go.md
Created December 12, 2021 04:43
Common Crawl Corpus parsing in Go
// cc-scanner impfmt.Errorf("HTTP error for %s: %v", url, err)orts domains from the Common Crawl URL index
//
// LICENCE: No licence is provided for this project
package main
import (
"bufio"
"context"
"encoding/json"
@xeoncross
xeoncross / bitpacking_test.go
Created November 20, 2021 17:38
store a list of 6bit values in a regular 8bit byte array
/*
I'm playing with compression using dictionaries (for learning) and needed to figure out the logic for when saving 6 bits across two different byte boundaries. Basically, when storing multiple 6 bit values in regular 8 bit bytes, how do you calculate the bit shifting needed to split the bits up between different bytes in the []byte slice?
https://goplay.space/#dfgOt3sGJzN
*/
func TestManualBytes(t *testing.T) {
//
// Demo 1: manual
//