Skip to content

Instantly share code, notes, and snippets.

View BetterProgramming's full-sized avatar

BetterProgramming

View GitHub Profile
func (c *baseGoCollector) Collect(ch chan<- Metric) {
ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
n := getRuntimeNumThreads()
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, n)
var stats debug.GCStats
stats.PauseQuantiles = make([]time.Duration, 5)
debug.ReadGCStats(&stats)
quantiles := make(map[float64]float64)
for idx, pq := range stats.PauseQuantiles[1:] {
quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
func TestMemStats(t *testing.T) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
v := reflect.ValueOf(&m).Elem()
for i := 0; i < v.NumField(); i++ {
t.Logf("%s %s %v \n",
v.Field(i).Type(),
v.Type().Field(i).Name,
v.Field(i).Interface())
}
func main() {
runtime.GOMAXPROCS(2)
var wg sync.WaitGroup
wg.Add(2)
fmt.Printf("Starting")
go func() {
defer wg.Done()
for n := 0; n < 10; n++ {
fmt.Printf("%d\n", n)
}
// Register the route to return all the animals
router.get('/animals', async (req: IttyRequest, env: Env) => {
let animals = await getAllAnimals(env)
return new Response(JSON.stringify(animals))
})
// Register the route to return an animal by ID
router.get('/animals/:id', async (req: IttyRequest, env: Env) => {
let animals = await getAllAnimals(env)
const ANIMALS_KV_KEY_ID = 'all-animals'
export async function getAllAnimals(env: Env): Promise<any[]> {
let animals = await env.ANIMALS.get(ANIMALS_KV_KEY_ID)
if (animals === null) {
return []
}
return JSON.parse(animals);
}
function assertNonNullable<T>(value: T): asserts value is NonNullable<T> {
if (value === undefined || value === null) {
throw new Error("undefined or null are not allowed");
}
}
function calcAge(
input: Date | null | undefined | string | Person | PersonJson
): number {
assertNonNullable(input);
if (typeof input === "string") {
type Person = {
birthday: Date;
};
type PersonJson = {
birthday: string;
};
function isPerson(value: Person | PersonJson): value is Person {
return value.birthday instanceof Date;
}
function calcAge(
type Person = {
birthday: Date;
};
type PersonJson = {
birthday: string;
};
function calcAge(
input: Date | null | undefined | string | Person | PersonJson
): number {
if (!input) {
type Person = {
birthday: Date;
};
type Car = {
yearOfConstruction: Date;
};
function calcAge(
input: Date | null | undefined | string | Person | Car
): number {
if (!value) {
type Person = {
birthday: Date;
};
type Car = {
yearOfConstruction: Date;
};
function calcAge(
input: Date | null | undefined | string | Person | Car
): number {
if (!input) {