chore: badly document code
This commit is contained in:
parent
d5a55c663b
commit
4137eea1e0
10
main.go
10
main.go
@ -13,19 +13,23 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Global counter
|
||||||
var C counter
|
var C counter
|
||||||
|
|
||||||
|
// Counter mutex for counter
|
||||||
type counter struct {
|
type counter struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
n int
|
n int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add value to counter
|
||||||
func (c *counter) Add() {
|
func (c *counter) Add() {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.n++
|
c.n++
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get value from counter
|
||||||
func (c *counter) Get() int {
|
func (c *counter) Get() int {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
n := c.n
|
n := c.n
|
||||||
@ -33,12 +37,14 @@ func (c *counter) Get() int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset counter
|
||||||
func (c *counter) Reset() {
|
func (c *counter) Reset() {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.n = 0
|
c.n = 0
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getenv reads an environment variable named k and returns it as type D
|
||||||
func getenv[D ~string | int](k string, d D) D {
|
func getenv[D ~string | int](k string, d D) D {
|
||||||
v := os.Getenv(k)
|
v := os.Getenv(k)
|
||||||
if len(v) == 0 {
|
if len(v) == 0 {
|
||||||
@ -59,10 +65,12 @@ func getenv[D ~string | int](k string, d D) D {
|
|||||||
return r.(D)
|
return r.(D)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// serve evaluates if the limit of requests is reached
|
||||||
func serve(l int) bool {
|
func serve(l int) bool {
|
||||||
return C.Get() < l
|
return C.Get() < l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handler generates the echo server response
|
||||||
func handler(l int) http.HandlerFunc {
|
func handler(l int) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
@ -78,6 +86,7 @@ func handler(l int) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// httpHealth is a bad implementation of a health check
|
||||||
func httpHealth(l int) http.HandlerFunc {
|
func httpHealth(l int) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !serve(l) {
|
if !serve(l) {
|
||||||
@ -89,6 +98,7 @@ func httpHealth(l int) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reset resets the request counter
|
||||||
func reset(rt string) http.HandlerFunc {
|
func reset(rt string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user