chg: [main] rate limiter

d4forward
Jean-Louis Huynen 2020-04-23 12:14:56 +02:00
parent ac5cd4449a
commit 2f6da40367
No known key found for this signature in database
GPG Key ID: 64799157F4BD6B93
1 changed files with 12 additions and 2 deletions

View File

@ -66,6 +66,7 @@ type (
ct time.Duration ct time.Duration
ce bool ce bool
retry time.Duration retry time.Duration
rate time.Duration
cc bool cc bool
ca x509.CertPool ca x509.CertPool
d4error uint8 d4error uint8
@ -104,13 +105,15 @@ var (
tmpct, _ = time.ParseDuration("5mn") tmpct, _ = time.ParseDuration("5mn")
tmpcka, _ = time.ParseDuration("30s") tmpcka, _ = time.ParseDuration("30s")
tmpretry, _ = time.ParseDuration("30s") tmpretry, _ = time.ParseDuration("30s")
tmprate, _ = time.ParseDuration("200ms")
confdir = flag.String("c", "", "configuration directory") confdir = flag.String("c", "", "configuration directory")
debug = flag.Bool("v", false, "Set to True, true, TRUE, 1, or t to enable verbose output on stdout") debug = flag.Bool("v", false, "Set to True, true, TRUE, 1, or t to enable verbose output on stdout")
ce = flag.Bool("ce", true, "Set to True, true, TRUE, 1, or t to enable TLS on network destination") ce = flag.Bool("ce", true, "Set to True, true, TRUE, 1, or t to enable TLS on network destination")
ct = flag.Duration("ct", tmpct, "Set timeout in human format") ct = flag.Duration("ct", tmpct, "Set timeout in human format")
cka = flag.Duration("cka", tmpcka, "Keep Alive time human format, 0 to disable") cka = flag.Duration("cka", tmpcka, "Keep Alive time human format, 0 to disable")
retry = flag.Duration("rt", tmpretry, "Time in human format before retry after connection failure, set to 0 to exit on failure") retry = flag.Duration("rt", tmpretry, "Rime in human format before retry after connection failure, set to 0 to exit on failure")
rate = flag.Duration("rl", tmprate, "Rate limiter: time in human format before retry after EOF")
cc = flag.Bool("cc", false, "Check TLS certificate against rootCA.crt") cc = flag.Bool("cc", false, "Check TLS certificate against rootCA.crt")
) )
@ -156,11 +159,15 @@ func main() {
d4.cc = *cc d4.cc = *cc
d4.cka = *cka d4.cka = *cka
d4.retry = *retry d4.retry = *retry
d4.rate = *rate
s := make(chan os.Signal, 1) s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, os.Kill) signal.Notify(s, os.Interrupt, os.Kill)
c := make(chan string) c := make(chan string)
// Launching the Rate limiter
ratelimiter := time.Tick(d4.rate)
d4.mhb = bytes.NewBuffer(d4.mh) d4.mhb = bytes.NewBuffer(d4.mh)
for { for {
@ -180,6 +187,9 @@ func main() {
} }
// copy routine // copy routine
go d4Copy(d4p, c) go d4Copy(d4p, c)
log.Printf("---d4Copy")
// Block until the rate limiter allow us to continue
<-ratelimiter
} else if d4.retry > 0 { } else if d4.retry > 0 {
go func() { go func() {
infof(fmt.Sprintf("Sleeping for %.f seconds before retry...\n", d4.retry.Seconds())) infof(fmt.Sprintf("Sleeping for %.f seconds before retry...\n", d4.retry.Seconds()))
@ -195,7 +205,7 @@ func main() {
select { select {
case str := <-c: case str := <-c:
infof(str) infof(str)
// log.Printf("Channel c: %q\n", str) //log.Printf("Channel c: %q\n", str)
continue continue
case <-s: case <-s:
fmt.Println(" Exiting") fmt.Println(" Exiting")