Browse Source

fasthttp: periodically close keep-alive connections for even connection distribution among available worker processes in prefork mode

Aliaksandr Valialkin 9 years ago
parent
commit
9bdfdf7f01

+ 9 - 0
frameworks/Go/fasthttp-mysql/src/hello/hello.go

@@ -92,7 +92,16 @@ func main() {
 	}
 	}
 }
 }
 
 
+const maxConnDuration = time.Millisecond * 300
+
 func mainHandler(ctx *fasthttp.RequestCtx) {
 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()
+	}
+
 	path := ctx.Path()
 	path := ctx.Path()
 	switch string(path) {
 	switch string(path) {
 	case "/plaintext":
 	case "/plaintext":

+ 15 - 1
frameworks/Go/fasthttp-postgresql/src/hello/hello.go

@@ -12,6 +12,7 @@ import (
 	"os/exec"
 	"os/exec"
 	"runtime"
 	"runtime"
 	"sort"
 	"sort"
+	"time"
 
 
 	"github.com/jackc/pgx"
 	"github.com/jackc/pgx"
 	"github.com/valyala/fasthttp"
 	"github.com/valyala/fasthttp"
@@ -65,7 +66,11 @@ func main() {
 	var err error
 	var err error
 
 
 	// initialize the connection pool
 	// initialize the connection pool
-	if db, err = initDatabase("localhost", "benchmarkdbuser", "benchmarkdbpass", "hello_world", 5432, maxConnectionCount); err != nil {
+	dbConns := maxConnectionCount
+	if *prefork {
+		dbConns = (maxConnectionCount + runtime.NumCPU() - 1) / runtime.NumCPU()
+	}
+	if db, err = initDatabase("localhost", "benchmarkdbuser", "benchmarkdbpass", "hello_world", 5432, 2*dbConns); err != nil {
 		log.Fatalf("Error opening database: %s", err)
 		log.Fatalf("Error opening database: %s", err)
 	}
 	}
 
 
@@ -79,7 +84,16 @@ func main() {
 	}
 	}
 }
 }
 
 
+const maxConnDuration = time.Millisecond * 300
+
 func mainHandler(ctx *fasthttp.RequestCtx) {
 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()
+	}
+
 	path := ctx.Path()
 	path := ctx.Path()
 	switch string(path) {
 	switch string(path) {
 	case "/plaintext":
 	case "/plaintext":