Removed 'recover-from-panic' band-aids.

All the panics must be handled by the user code.
This commit is contained in:
Aliaksandr Valialkin
2016-07-12 12:30:33 +03:00
parent d1dd56c016
commit 886e541160
4 changed files with 0 additions and 119 deletions
-5
View File
@@ -10,7 +10,6 @@ import (
"mime/multipart"
"net"
"os"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
@@ -1673,10 +1672,6 @@ func hijackConnHandler(r io.Reader, c net.Conn, s *Server, h HijackHandler) {
hjc := s.acquireHijackConn(r, c)
defer func() {
if r := recover(); r != nil {
s.logger().Printf("panic on hijacked conn: %s\nStack trace:\n%s", r, debug.Stack())
}
if br, ok := r.(*bufio.Reader); ok {
releaseReader(s, br)
}
-7
View File
@@ -3,7 +3,6 @@ package fasthttp
import (
"bufio"
"io"
"runtime/debug"
"sync"
"github.com/valyala/fasthttp/fasthttputil"
@@ -42,12 +41,6 @@ func NewStreamReader(sw StreamWriter) io.ReadCloser {
}
go func() {
defer func() {
if r := recover(); r != nil {
defaultLogger.Printf("panic in StreamWriter: %s\nStack trace:\n%s", r, debug.Stack())
}
}()
sw(bw)
bw.Flush()
pw.Close()
-8
View File
@@ -3,7 +3,6 @@ package fasthttp
import (
"net"
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
@@ -203,13 +202,6 @@ func (wp *workerPool) workerFunc(ch *workerChan) {
var c net.Conn
defer func() {
if r := recover(); r != nil {
wp.Logger.Printf("panic: %s\nStack trace:\n%s", r, debug.Stack())
if c != nil {
c.Close()
}
}
wp.lock.Lock()
wp.workersCount--
wp.lock.Unlock()
-99
View File
@@ -1,10 +1,8 @@
package fasthttp
import (
"fmt"
"io/ioutil"
"net"
"sync/atomic"
"testing"
"time"
@@ -168,100 +166,3 @@ func testWorkerPoolMaxWorkersCount(t *testing.T) {
}
wp.Stop()
}
func TestWorkerPoolPanicErrorSerial(t *testing.T) {
testWorkerPoolPanicErrorMulti(t)
}
func TestWorkerPoolPanicErrorConcurrent(t *testing.T) {
concurrency := 10
ch := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
testWorkerPoolPanicErrorMulti(t)
ch <- struct{}{}
}()
}
for i := 0; i < concurrency; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
}
func testWorkerPoolPanicErrorMulti(t *testing.T) {
var globalCount uint64
wp := &workerPool{
WorkerFunc: func(conn net.Conn) error {
count := atomic.AddUint64(&globalCount, 1)
switch count % 3 {
case 0:
panic("foobar")
case 1:
return fmt.Errorf("fake error")
}
return nil
},
MaxWorkersCount: 1000,
MaxIdleWorkerDuration: time.Millisecond,
Logger: &customLogger{},
}
for i := 0; i < 10; i++ {
testWorkerPoolPanicError(t, wp)
}
}
func testWorkerPoolPanicError(t *testing.T, wp *workerPool) {
wp.Start()
ln := fasthttputil.NewInmemoryListener()
clientsCount := 10
clientCh := make(chan struct{}, clientsCount)
for i := 0; i < clientsCount; i++ {
go func() {
conn, err := ln.Dial()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
data, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if len(data) > 0 {
t.Fatalf("unexpected data read: %q. Expecting empty data", data)
}
if err = conn.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
clientCh <- struct{}{}
}()
}
for i := 0; i < clientsCount; i++ {
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !wp.Serve(conn) {
t.Fatalf("worker pool mustn't be full")
}
}
for i := 0; i < clientsCount; i++ {
select {
case <-clientCh:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
if err := ln.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
wp.Stop()
}