This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Person = { | |
| birthday: Date; | |
| }; | |
| type PersonJson = { | |
| birthday: string; | |
| }; | |
| function isPerson(value: Person | PersonJson): value is Person { | |
| return value.birthday instanceof Date; | |
| } | |
| function calcAge( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Person = { | |
| birthday: Date; | |
| }; | |
| type PersonJson = { | |
| birthday: string; | |
| }; | |
| function calcAge( | |
| input: Date | null | undefined | string | Person | PersonJson | |
| ): number { | |
| if (!input) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Person = { | |
| birthday: Date; | |
| }; | |
| type Car = { | |
| yearOfConstruction: Date; | |
| }; | |
| function calcAge( | |
| input: Date | null | undefined | string | Person | Car | |
| ): number { | |
| if (!value) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Person = { | |
| birthday: Date; | |
| }; | |
| type Car = { | |
| yearOfConstruction: Date; | |
| }; | |
| function calcAge( | |
| input: Date | null | undefined | string | Person | Car | |
| ): number { | |
| if (!input) { |