adds go-lang limiter
This commit is contained in:
parent
6930e0aaa3
commit
4cd664b933
57
cmd/block-multiple/main.go
Normal file
57
cmd/block-multiple/main.go
Normal 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)
|
||||||
|
*/
|
Loading…
x
Reference in New Issue
Block a user