Skip to content

Instantly share code, notes, and snippets.

@andrewhowdencom
Last active December 15, 2023 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewhowdencom/40ba1dc9daac2e898c0d1a20e529b6a8 to your computer and use it in GitHub Desktop.
Save andrewhowdencom/40ba1dc9daac2e898c0d1a20e529b6a8 to your computer and use it in GitHub Desktop.
lol
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
interval := 0
total := 0
go func() {
t := time.NewTicker(time.Second * 1)
for {
nt := <-t.C
fmt.Println(nt.String(), "total", total, "interval", interval)
interval = 0
}
}()
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
total++
interval++
}))
if err := http.ListenAndServe("localhost:8099", http.DefaultServeMux); err != nil {
fmt.Println(err)
}
}
package main
import (
"fmt"
"io"
"net/http"
"sync"
"time"
)
func main() {
total := 0
interval := 0
limit := 10
step := 1
live := 0
errMap := map[string]int{}
var mu sync.Mutex
var lmu sync.Mutex
var emu sync.Mutex
go func() {
t := time.NewTicker(time.Second * 1)
for {
next := <-t.C
// Print the current total, throughput
fmt.Println(next, "total", total, "live", live, "limit", limit, "interval", interval, "step", step)
for k, v := range errMap {
fmt.Println(k, v)
}
// Adjust the limit
// Limit exceeds the last; we're above the max. Bump it back a bit.
if limit > interval {
// Half the step, so we can binary search the upper bound value.
step = step / 2
limit = limit - step
} else if limit < interval {
step = step * 2
limit = limit + step
}
if step == 0 {
step++
}
interval = 0
}
}()
for {
// Needs to figure out an upper bound.
if live > limit {
continue
}
lmu.Lock()
live++
lmu.Unlock()
go func() {
defer func() {
lmu.Lock()
defer lmu.Unlock()
live--
}()
r, err := http.Get("http://localhost:8099")
if err != nil {
emu.Lock()
errMap[err.Error()]++
emu.Unlock()
return
}
io.Copy(io.Discard, r.Body)
r.Body.Close()
mu.Lock()
defer mu.Unlock()
total++
interval++
}()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment