1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-22 04:17:03 +00:00

add Context.ResponseWriter.IsHijacked to report whether the underline conn is already hijacked and a lot of cleanup and minor ws stress test example improvements

Former-commit-id: 444d4f0718d5c6d7544834c5e44dafb872980238
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-02-23 07:23:10 +02:00
parent bda36145e5
commit ddec78af0a
31 changed files with 124 additions and 100 deletions

View File

@@ -11,21 +11,24 @@ import (
"github.com/kataras/iris/websocket"
)
const totalClients = 16000 // max depends on the OS.
const verbose = true
const (
endpoint = "localhost:8080"
totalClients = 16000 // max depends on the OS.
verbose = false
maxC = 0
)
func main() {
ws := websocket.New(websocket.Config{})
ws.OnConnection(handleConnection)
// websocket.Config{PingPeriod: ((60 * time.Second) * 9) / 10}
go func() {
dur := 8 * time.Second
dur := 4 * time.Second
if totalClients >= 64000 {
// if more than 64000 then let's no check every 8 seconds, let's do it every 24 seconds,
// just for simplicity, either way works.
// if more than 64000 then let's perform those checks every 24 seconds instead,
// either way works.
dur = 24 * time.Second
}
t := time.NewTicker(dur)
@@ -40,12 +43,16 @@ func main() {
n := ws.GetTotalConnections()
if n > 0 {
started = true
if maxC > 0 && n > maxC {
log.Printf("current connections[%d] > MaxConcurrentConnections[%d]", n, maxC)
return
}
}
if started {
totalConnected := atomic.LoadUint64(&count)
if totalConnected == totalClients {
disconnectedN := atomic.LoadUint64(&totalDisconnected)
connectedN := atomic.LoadUint64(&totalConnected)
if disconnectedN == totalClients && connectedN == totalClients {
if n != 0 {
log.Println("ALL CLIENTS DISCONNECTED BUT LEFTOVERS ON CONNECTIONS LIST.")
} else {
@@ -53,7 +60,7 @@ func main() {
}
return
} else if n == 0 {
log.Printf("%d/%d CLIENTS WERE NOT CONNECTED AT ALL. CHECK YOUR OS NET SETTINGS. ALL OTHER CONNECTED CLIENTS DISCONNECTED SUCCESSFULLY.\n",
log.Printf("%d/%d CLIENTS WERE NOT CONNECTED AT ALL. CHECK YOUR OS NET SETTINGS. THE REST CLIENTS WERE DISCONNECTED SUCCESSFULLY.\n",
totalClients-totalConnected, totalClients)
return
@@ -64,11 +71,18 @@ func main() {
app := iris.New()
app.Get("/", ws.Handler())
app.Run(iris.Addr(":8080"))
app.Run(iris.Addr(endpoint), iris.WithoutServerError(iris.ErrServerClosed))
}
var totalConnected uint64
func handleConnection(c websocket.Connection) {
if c.Err() != nil {
log.Fatalf("[%d] upgrade failed: %v", atomic.LoadUint64(&totalConnected)+1, c.Err())
return
}
atomic.AddUint64(&totalConnected, 1)
c.OnError(func(err error) { handleErr(c, err) })
c.OnDisconnect(func() { handleDisconnect(c) })
c.On("chat", func(message string) {
@@ -76,12 +90,12 @@ func handleConnection(c websocket.Connection) {
})
}
var count uint64
var totalDisconnected uint64
func handleDisconnect(c websocket.Connection) {
atomic.AddUint64(&count, 1)
newC := atomic.AddUint64(&totalDisconnected, 1)
if verbose {
log.Printf("client [%s] disconnected!\n", c.ID())
log.Printf("[%d] client [%s] disconnected!\n", newC, c.ID())
}
}