adds go-lang limiter

This commit is contained in:
Hyatt 2024-04-05 14:11:18 -05:00
parent 6930e0aaa3
commit 4cd664b933
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA

View File

@ -0,0 +1,57 @@
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
type Limiter struct {
ch chan struct{}
}
func main() {
var l = &Limiter{
ch: make(chan struct{}, 1),
}
for i := 1; i <= 5; i++ {
go l.execute(func() {
var fName = "task"
fmt.Printf("Starting Execution of %s\n", fName)
time.Sleep(time.Second * 5)
fmt.Printf("Finished Execution of %s\n", fName)
})
}
forever()
}
func forever() {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
// wait for signal
sig := <-s
fmt.Printf("\nstarting shutdown procedure, detected signal: %s\n", sig)
}
func (l *Limiter) execute(f func()) error {
l.ch <- struct{}{}
defer func() {
<-l.ch
}()
f()
return nil
}
/*
var fName = "thingOne"
fmt.Printf("Starting Execution of %s\n", fName)
time.Sleep(time.Second * 5)
fmt.Printf("Finished Execution of %s\n", fName)
*/