Skip to content

Instantly share code, notes, and snippets.

Avatar

David Pennington Xeoncross

View GitHub Profile
@Xeoncross
Xeoncross / ranked_window.go
Created Jun 22, 2022
Keep the keys with the highest values in a map https://go.dev/play/p/E-PQldhgz4M
View ranked_window.go
package main
import "fmt"
func BenchmarkMapIteration(b *testing.B) {
size := 1000
ranked := make(map[int]uint8, size)
for i := 1; i < size; i++ {
ranked[i] = uint8(i % 8)
@Xeoncross
Xeoncross / int_bits.go
Created Jun 22, 2022
Examples of bit lengths of different integer values: https://go.dev/play/p/gLZKA9B233I
View int_bits.go
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 Jun 22, 2022
Encoding a sorted sequence of integers using delta encoding can save 40%-60% on storage costs: https://go.dev/play/p/KdLslro7n1n
View delta_encoding_demo.go
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 Jun 20, 2022
Distributed rate limiting with non-whole time periods (max 3 requests every 5 seconds)
View README.md

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 Jun 4, 2022
Example of creating a string that implements an interface in Go / golang: https://go.dev/play/p/-IC8BGzjPPG
View string_interface.go
package main
import "fmt"
type A string
func (a A) Foo() {
fmt.Printf("Foo: %q\n", a)
}
@Xeoncross
Xeoncross / csgo.mac.md
Last active Apr 27, 2022
CS:GO on Macs
View csgo.mac.md

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 Dec 28, 2021
Quick docker setup of new database for building out project DDL / UML for RDBMS entities
View .env
MYSQL_HOST=mysqldb
MYSQL_USER=design
MYSQL_PASS=design
MYSQL_NAME=design
MYSQL_PORT=3306
@Xeoncross
Xeoncross / the_future_of_go.md
Last active Feb 7, 2022
The future of fast MVP's using Go and Typescript using code generation
View the_future_of_go.md

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 Dec 12, 2021
Common Crawl Corpus parsing in Go
View common_crawl_corpus_in_go.md
View common-crawl-scanner.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"