Browse Source

fasthttp: added missing 'time' import to fasthttp-mysql and added jitter to maximum connection duration in prefork mode

Aliaksandr Valialkin 9 years ago
parent
commit
43cc675e7a

+ 11 - 3
frameworks/Go/fasthttp-mysql/src/hello/hello.go

@@ -12,6 +12,8 @@ import (
 	"os/exec"
 	"runtime"
 	"sort"
+	"sync/atomic"
+	"time"
 
 	_ "github.com/go-sql-driver/mysql"
 	"github.com/valyala/fasthttp"
@@ -92,14 +94,20 @@ func main() {
 	}
 }
 
-const maxConnDuration = time.Millisecond * 300
+const maxConnDuration = time.Millisecond * 200
+
+var connDurationJitter uint64
 
 func mainHandler(ctx *fasthttp.RequestCtx) {
 	// Performance hack for prefork mode - periodically close keepalive
 	// connections for evenly distributing connections among available
 	// processes.
-	if *prefork && time.Since(ctx.ConnTime()) > maxConnDuration {
-		ctx.SetConnectionClose()
+	if *prefork {
+		maxDuration := maxConnDuration + time.Millisecond*time.Duration(atomic.LoadUint64(&connDurationJitter))
+		if time.Since(ctx.ConnTime()) > maxDuration {
+			atomic.StoreUint64(&connDurationJitter, uint64(rand.Intn(100)))
+			ctx.SetConnectionClose()
+		}
 	}
 
 	path := ctx.Path()

+ 10 - 3
frameworks/Go/fasthttp-postgresql/src/hello/hello.go

@@ -12,6 +12,7 @@ import (
 	"os/exec"
 	"runtime"
 	"sort"
+	"sync/atomic"
 	"time"
 
 	"github.com/jackc/pgx"
@@ -84,14 +85,20 @@ func main() {
 	}
 }
 
-const maxConnDuration = time.Millisecond * 300
+const maxConnDuration = time.Millisecond * 200
+
+var connDurationJitter uint64
 
 func mainHandler(ctx *fasthttp.RequestCtx) {
 	// Performance hack for prefork mode - periodically close keepalive
 	// connections for evenly distributing connections among available
 	// processes.
-	if *prefork && time.Since(ctx.ConnTime()) > maxConnDuration {
-		ctx.SetConnectionClose()
+	if *prefork {
+		maxDuration := maxConnDuration + time.Millisecond*time.Duration(atomic.LoadUint64(&connDurationJitter))
+		if time.Since(ctx.ConnTime()) > maxDuration {
+			atomic.StoreUint64(&connDurationJitter, uint64(rand.Intn(100)))
+			ctx.SetConnectionClose()
+		}
 	}
 
 	path := ctx.Path()