Skip to content

Instantly share code, notes, and snippets.

View calam1's full-sized avatar

Christopher Lam calam1

View GitHub Profile
@calam1
calam1 / pipeline_generators.go
Last active May 30, 2024 13:32
An example from the book Concurrency in Go generating random numbers and testing if they are prime, using fan in and fan out, plus updated to use generics vs interface{} also updated to show using context instead of done channel, but left old code as an example
package main
import (
"context"
"fmt"
"math/rand"
"runtime"
"sync"
"time"
)
@calam1
calam1 / traffic_prioritization.rs
Last active March 26, 2024 21:43
Not sure if this works, but a quick POC of a custom layer/middleware for the rust crate tower - traffic prioritization based off of a cookie value
use bytes::Bytes;
use http_body_util::combinators::BoxBody;
use hyper::{Error, Response, Request};
use std::collections::BinaryHeap;
use tower::{Layer, Service};
use std::sync::{Arc, Mutex};
use std::cmp::Ordering;
use std::pin::Pin;
use std::task::{Context, Poll};
@calam1
calam1 / aoc_2023_day01a.rs
Last active December 2, 2023 00:00
AOC Day 01a Rust solution
fn main() {
let re = regex::Regex::new(r"[A-Za-z]").unwrap();
println!(
"{:?}",
include_str!("../input.txt")
.lines()
.map(|s| re.split(s).filter(|s| !s.is_empty()))
.map(|s| {
s.into_iter()
.flat_map(|s| {
@calam1
calam1 / textfile_AI.py
Last active May 2, 2023 22:46
an example of using OpenAI api and Dolly 2.0 to read a file and then ask it a question
# the file contents of state_of_the_union.txt is at the end of this gist, just copy and paste it into a file named state_of_the union.txt and place it in the same directory as the python file
# OpenAI api example
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
@calam1
calam1 / main.go
Created December 14, 2022 21:33
A quick and dirty JWT/JWKS endpoint validator
package main
import (
"log"
"time"
// remember go get these libs
"github.com/dgrijalva/jwt-go"
"github.com/MicahParks/keyfunc"
)
@calam1
calam1 / envoyfilter.yml
Last active December 14, 2022 21:33
envoyfilter writing authorization header in the response and setting cookie values
---
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: lua-filter
namespace: dev
spec:
workloadSelector:
labels:
# we run with api-key 123abc and we are limited to 5 (which is what the auth svc returned to us via the header)
❯❯❯ hey -c 1 -n 100 -H "x-api-key:123abc" http://localhost:30000/index on branch: main
...
Status code distribution:
[200] 5 responses
[429] 95 responses
@calam1
calam1 / envoy_on_request.lua
Created August 16, 2022 15:10
lua script that sets dynamicMetadata
function envoy_on_request(request_handle)
local reqs_per_unit = request_handle:streamInfo():dynamicMetadata():get("envoy.filters.http.header_to_metadata")["rate.requests_per_unit"]
local reqs_unit = request_handle:streamInfo():dynamicMetadata():get("envoy.filters.http.header_to_metadata")["rate.unit"]
request_handle:logWarn("requests_per_unit value: " .. reqs_per_unit)
request_handle:logWarn("unit value: " .. reqs_unit)
local rate_limits = { requests_per_unit = reqs_per_unit, unit = reqs_unit}
request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.ratelimit.override", "limit", rate_limits)
@calam1
calam1 / envoyfilterauth.yml
Created August 16, 2022 15:03
EnvoyFilter for auth and header to metadata for rate limit override
---
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: core-authz-filter
spec:
workloadSelector:
labels:
app: python-api
@calam1
calam1 / envoyfilter.yml
Created August 16, 2022 13:58
Envoyfilter defining the descriptors in the configmap
---
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: python-api-filter-ratelimit-svc
spec:
workloadSelector:
labels:
app: python-api
configPatches: