Skip to content

Instantly share code, notes, and snippets.

@mohashari
mohashari / snippet-1.txt
Created July 24, 2026 01:01
Building a Lock-Free Multi-Producer Single-Consumer (MPSC) Ring Buffer in Rust with Atomic Memory Orderings — code snippets
use std::cell::UnsafeCell;
use std::mem::MaybeUninit;
use std::sync::atomic::AtomicUsize;
#[repr(align(64))]
struct Cell<T> {
// Monotonically increasing sequence to synchronize producers and consumer
sequence: AtomicUsize,
// The actual storage slot for the message, using UnsafeCell to allow interior mutability
value: UnsafeCell<MaybeUninit<T>>,
@mohashari
mohashari / snippet-1.js
Created July 24, 2026 01:00
Tracing Linux Epoll Starvation and Thread Pool Congestion in High-Throughput Node.js Services via eBPF — code snippets
const fastify = require('fastify')({ logger: false });
const crypto = require('crypto');
const fs = require('fs').promises;
const path = require('path');
// Simulate a database verification using scrypt
const verifyPassword = (password, salt) => {
return new Promise((resolve, reject) => {
// This executes on the libuv thread pool
crypto.scrypt(password, salt, 64, (err, derivedKey) => {
@mohashari
mohashari / snippet-1.txt
Created July 23, 2026 01:04
Implementing a Zero-Copy Serializer in Rust Using FlatBuffers and Shared Memory for IPC — code snippets
namespace telemetry;
table SensorReading {
sensor_id: uint64;
timestamp: int64;
temperature: double;
humidity: double;
firmware_version: string;
raw_payload: [ubyte];
}
@mohashari
mohashari / snippet-1.sh
Created July 23, 2026 01:03
Implementing Cryptographic Attestation for Ephemeral Microservice Identities using AWS Nitro Enclaves and KMS — code snippets
#!/usr/bin/env bash
# build_eif.sh
set -euo pipefail
echo "[*] Building statically linked Go application..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s -extldflags '-static'" \
-o bootstrap_app ./cmd/bootstrap
echo "[*] Generating container definition..."
@mohashari
mohashari / snippet-1.go
Created July 23, 2026 01:02
Implementing Low-Latency Structured JSON Outputs from LLMs with Finite State Machine Guided Decoding in Go — code snippets
package main
import (
"fmt"
"reflect"
"strings"
)
// Compiler handles the conversion of Go structs into GBNF (GGML Backus-Naur Form) grammars.
type Compiler struct {
@mohashari
mohashari / snippet-1.txt
Created July 23, 2026 01:01
Tracing CPU Cache Misses and TLB Thrashing in High-Throughput Java Applications using eBPF and perf_events — code snippets
package com.performance.cache;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
/**
* Demonstrates the cache-friendly layout pattern.
* Compares standard pointer-chasing node traversal against a contiguous,
* cache-prefetcher-friendly off-heap array layout.
@mohashari
mohashari / snippet-1.go
Created July 22, 2026 01:02
Mitigating Replay Attacks in AWS IAM Roles Anywhere with Cryptographic Challenge-Response and SPIFFE — code snippets
package spiffeclient
import (
"context"
"crypto/x509"
"fmt"
"time"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
@mohashari
mohashari / snippet-1.go
Created July 22, 2026 01:01
Building a Zero-Allocation HTTP/2 HPACK Header Encoder in Go — code snippets
package hpack
// AppendVarint encodes an integer 'i' with an 'n'-bit prefix and appends it to 'dst'.
// This implementation requires zero heap allocations and operates directly on the destination slice.
func AppendVarint(dst []byte, i uint64, n uint8) []byte {
if n < 1 || n > 8 {
panic("invalid prefix size")
}
maxPrefixVal := uint64((1 << n) - 1)
if i < maxPrefixVal {
@mohashari
mohashari / snippet-1.txt
Created July 22, 2026 01:00
Profiling JVM Safepoint Delays at Scale with JDK Flight Recorder and eBPF — code snippets
package com.mycompany.observability;
import jdk.jfr.Configuration;
import jdk.jfr.Recording;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.ParseException;
import java.time.Duration;
public class JfrSafepointProfiler {
@mohashari
mohashari / snippet-1.txt
Created July 21, 2026 01:02
Automating Real-Time Goroutine Leak Detection in Production Using eBPF and Runtime/pprof — code snippets
// +build ignore
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
char LICENSE[] SEC("license") = "Dual BSD/GPL";
// Map to temporarily store callerpc between newproc1 and runqput on the same thread
struct {