Skip to content

Instantly share code, notes, and snippets.

@Tiscs
Tiscs / snowflake.ts
Created October 6, 2025 02:39
A Snowflake ID generator that produces unique 64-bit identifiers.
const DEFAULT_ID_EPOCH = 1704067200000; // 2024-01-01T00:00:00Z
const TIMESTAMP_BITS = 41; // dedicated bits for timestamp
const MAX_PAYLOAD_BITS = 64 - 1 - TIMESTAMP_BITS; // 22 bits left for clusterId, workerId, sequence
/**
* A Snowflake ID generator that produces unique 64-bit identifiers.
* The ID is composed of:
* - 1 bit: unused (sign bit)
* - 41 bits: timestamp in milliseconds since a custom epoch
* - N bits: cluster ID (configurable, default 5 bits)
@Tiscs
Tiscs / react-websocket.ts
Created January 11, 2024 05:19
react-websocket.ts
import { useRef, useState, useEffect } from "react";
export type SubProtocol = "protobuf" | "msgpack" | "json";
export function useSocketClient<P extends SubProtocol, B extends BinaryType>(
url: string | URL,
protocol: P = "protobuf" as P,
binaryType: B = "arraybuffer" as B,
) {
type M = P extends "json" ? string : B extends "arraybuffer" ? ArrayBufferLike : B extends "blob" ? Blob : unknown;
@Tiscs
Tiscs / IdWorker.java
Created September 12, 2023 06:08
Snowflake id-worker written in java.
package io.github.tiscs.sbp.snowflake;
public class IdWorker {
// 2020-01-01T00:00:00.000Z
public static final long DEFAULT_ID_EPOCH = 1577836800000L;
public static final String LOWER_HEX_FORMAT = "%016x";
public static final String UPPER_HEX_FORMAT = "%016X";
public IdWorker(long clusterId, long workerId) throws IllegalAccessException {
this(clusterId, workerId, DEFAULT_ID_EPOCH, 5L, 5L, 12L);
@Tiscs
Tiscs / snowflake.go
Last active October 4, 2025 14:56
Snowflake id-worker written in golang.
package snowflake
import (
"errors"
"fmt"
"sync"
"time"
)
var (
@Tiscs
Tiscs / IdWorker.kt
Last active October 22, 2022 03:54
Snowflake id-worker written in kotlin.
package io.github.tiscs.sbp.snowflake
// 2020-01-01T00:00:00.000Z
const val DEFAULT_ID_EPOCH = 1577836800000L
const val LOWER_HEX_FORMAT = "%016x"
const val UPPER_HEX_FORMAT = "%016X"
class IdWorker(
private val clusterId: Long,
private val workerId: Long,
@Tiscs
Tiscs / CurdRepository.kt
Created October 26, 2019 11:12
Exposed Curd Repository
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement
import org.springframework.transaction.annotation.Transactional
@Transactional
class CurdRepository<M : Model<K>, K>(
private val primary: Column<K>,
private val mapper: (ResultRow) -> M
) {
@Tiscs
Tiscs / IdWorker.cs
Last active October 17, 2022 07:54
Snowflake/IdWorker.cs
using System;
using System.Linq;
namespace Snowflake
{
public class IdWorker
{
// 2020-01-01T00:00:00.000Z
public const long DEFAULT_ID_EPOCH = 1577836800000L;