Skip to content

Commit e3ae31a

Browse files
authored
fix: http2 client to http1.1 (#20)
temporarily change http transport to HTTP 1.1 until the bug golang/go#47882 is fixed
1 parent 57be902 commit e3ae31a

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

main.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"flag"
66
"fmt"
77
"log"
8+
"net"
89
"net/http"
10+
"runtime/debug"
911
"time"
1012

1113
"github.com/gofiber/fiber/v2"
1214
"github.com/gofiber/fiber/v2/middleware/recover"
1315
"github.com/gofiber/fiber/v2/middleware/session"
16+
"github.com/gofiber/fiber/v2/utils"
1417
"github.com/gofiber/storage/memory"
1518
)
1619

@@ -26,8 +29,23 @@ func main() {
2629
var port string
2730
flag.StringVar(&port, "p", "8091", "listening port")
2831
flag.Parse()
29-
// TODO configure transport
30-
client := &http.Client{}
32+
33+
// DefaultTransport without ForceAttemptHTTP2 (temporarily disable HTTP2)
34+
// TODO enable http2 as soon as the bug https://github.com/golang/go/issues/47882 is fixed
35+
client := &http.Client{
36+
Transport: &http.Transport{
37+
Proxy: http.ProxyFromEnvironment,
38+
DialContext: (&net.Dialer{
39+
Timeout: 30 * time.Second,
40+
KeepAlive: 30 * time.Second,
41+
}).DialContext,
42+
MaxIdleConns: 100,
43+
IdleConnTimeout: 90 * time.Second,
44+
TLSHandshakeTimeout: 10 * time.Second,
45+
ExpectContinueTimeout: 1 * time.Second,
46+
},
47+
}
48+
3149
// TODO static map www.example.com -> mail.com (from config file)
3250
conv := NewDomainConverter("host.juicyrout:" + port)
3351
conv.AddStaticMapping("www.w3.org", "www.w3.org")
@@ -61,6 +79,10 @@ func main() {
6179

6280
app.Use(recover.New(recover.Config{
6381
EnableStackTrace: true,
82+
StackTraceHandler: func(e interface{}) {
83+
log.Println("panic: ", e)
84+
log.Println("stack: ", utils.UnsafeString(debug.Stack()))
85+
},
6486
}))
6587
auth := NewAuthMiddleware(AuthConfig{
6688
CookieName: "session_id",

proxy.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"log"
45
"net/http"
56

67
"github.com/gofiber/fiber/v2"
@@ -25,7 +26,14 @@ type proxyHandler struct {
2526
}
2627

2728
func (p *proxyHandler) Handle(c *fiber.Ctx) error {
28-
req := p.req.Process(c)
29+
var req *http.Request
30+
defer func() {
31+
if err := recover(); err != nil {
32+
log.Println("panic in proxy for request: ", req)
33+
panic(err)
34+
}
35+
}()
36+
req = p.req.Process(c)
2937
resp, err := p.client.Do(req)
3038
if err != nil {
3139
return err

0 commit comments

Comments
 (0)