Add timeout support for FCGI TCP connections.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
11deb796e8
commit
a61c28bc5b
@ -9,7 +9,6 @@ import (
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/fcgi"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
@ -141,76 +140,3 @@ func main() {
|
||||
log.Println("[Main] Goodbye")
|
||||
//os.Exit(0)
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getListener(config conf.ConfigYaml, cwd string) net.Listener {
|
||||
split := strings.Split(strings.ToLower(config.Listen.WebNetwork), ":")
|
||||
if len(split) == 0 {
|
||||
log.Fatalln("Invalid Web Network")
|
||||
return nil
|
||||
} else {
|
||||
var theListener net.Listener
|
||||
var theError error
|
||||
log.Println("[Main] Socket Network Type:" + split[0])
|
||||
log.Printf("[Main] Starting up %s server on %s...\n", config.Listen.WebMethod, config.Listen.Web)
|
||||
switch split[0] {
|
||||
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6":
|
||||
theListener, theError = net.Listen(strings.ToLower(config.Listen.WebNetwork), config.Listen.Web)
|
||||
case "unix", "unixgram", "unixpacket":
|
||||
socketPath := config.Listen.Web
|
||||
if !filepath.IsAbs(socketPath) {
|
||||
if !filepath.IsAbs(cwd) {
|
||||
log.Fatalln("Web Path Not Absolute And No Working Directory.")
|
||||
return nil
|
||||
}
|
||||
socketPath = path.Join(cwd, socketPath)
|
||||
}
|
||||
log.Println("[Main] Removing old socket.")
|
||||
if err := os.RemoveAll(socketPath); err != nil {
|
||||
log.Fatalln("Could Not Remove Old Socket.")
|
||||
return nil
|
||||
}
|
||||
theListener, theError = net.Listen(strings.ToLower(config.Listen.WebNetwork), config.Listen.Web)
|
||||
default:
|
||||
log.Fatalln("Unknown Web Network.")
|
||||
return nil
|
||||
}
|
||||
if theError != nil {
|
||||
log.Fatalln("Failed to listen due to:", theError)
|
||||
return nil
|
||||
}
|
||||
return theListener
|
||||
}
|
||||
}
|
||||
|
||||
func runBackgroundHttp(s *http.Server, l net.Listener, tlsEnabled bool) {
|
||||
var err error
|
||||
if tlsEnabled {
|
||||
err = s.ServeTLS(l, "", "")
|
||||
} else {
|
||||
err = s.Serve(l)
|
||||
}
|
||||
if err != nil {
|
||||
if err == http.ErrServerClosed {
|
||||
log.Println("The http server shutdown successfully")
|
||||
} else {
|
||||
log.Fatalf("[Http] Error trying to host the http server: %s\n", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runBackgroundFCgi(h http.Handler, l net.Listener) {
|
||||
err := fcgi.Serve(l, h)
|
||||
if err != nil {
|
||||
if err == net.ErrClosed {
|
||||
log.Println("The fcgi server shutdown successfully")
|
||||
} else {
|
||||
log.Fatalf("[Http] Error trying to host the fcgi server: %s\n", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
49
cmd/wappcityuni/timeout-listener.go
Normal file
49
cmd/wappcityuni/timeout-listener.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
type tListener struct {
|
||||
net.Listener
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
}
|
||||
|
||||
func (l *tListener) Accept() (net.Conn, error) {
|
||||
c, err := l.Listener.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tc := &tConn{
|
||||
Conn: c,
|
||||
ReadTimeout: l.ReadTimeout,
|
||||
WriteTimeout: l.WriteTimeout,
|
||||
}
|
||||
return tc, nil
|
||||
}
|
||||
|
||||
type tConn struct {
|
||||
net.Conn
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
}
|
||||
|
||||
func (c *tConn) Read(b []byte) (n int, err error) {
|
||||
err = c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, err = c.Conn.Read(b)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *tConn) Write(b []byte) (n int, err error) {
|
||||
err = c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n, err = c.Conn.Write(b)
|
||||
return
|
||||
}
|
93
cmd/wappcityuni/utils.go
Normal file
93
cmd/wappcityuni/utils.go
Normal file
@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"golang.captainalm.com/cityuni-webserver/conf"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/fcgi"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getListener(config conf.ConfigYaml, cwd string) net.Listener {
|
||||
split := strings.Split(strings.ToLower(config.Listen.WebNetwork), ":")
|
||||
if len(split) == 0 {
|
||||
log.Fatalln("Invalid Web Network")
|
||||
return nil
|
||||
} else {
|
||||
var theListener net.Listener
|
||||
var theError error
|
||||
log.Println("[Main] Socket Network Type:" + split[0])
|
||||
log.Printf("[Main] Starting up %s server on %s...\n", config.Listen.WebMethod, config.Listen.Web)
|
||||
switch split[0] {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
theListener, theError = net.Listen(strings.ToLower(config.Listen.WebNetwork), config.Listen.Web)
|
||||
if theError == nil && strings.ToLower(config.Listen.WebMethod) == "fcgi" {
|
||||
theListener = &tListener{
|
||||
Listener: theListener,
|
||||
ReadTimeout: config.Listen.ReadTimeout,
|
||||
WriteTimeout: config.Listen.WriteTimeout,
|
||||
}
|
||||
}
|
||||
case "unix", "unixgram", "unixpacket":
|
||||
socketPath := config.Listen.Web
|
||||
if !filepath.IsAbs(socketPath) {
|
||||
if !filepath.IsAbs(cwd) {
|
||||
log.Fatalln("Web Path Not Absolute And No Working Directory.")
|
||||
return nil
|
||||
}
|
||||
socketPath = path.Join(cwd, socketPath)
|
||||
}
|
||||
log.Println("[Main] Removing old socket.")
|
||||
if err := os.RemoveAll(socketPath); err != nil {
|
||||
log.Fatalln("Could Not Remove Old Socket.")
|
||||
return nil
|
||||
}
|
||||
theListener, theError = net.Listen(strings.ToLower(config.Listen.WebNetwork), config.Listen.Web)
|
||||
default:
|
||||
log.Fatalln("Unknown Web Network.")
|
||||
return nil
|
||||
}
|
||||
if theError != nil {
|
||||
log.Fatalln("Failed to listen due to:", theError)
|
||||
return nil
|
||||
}
|
||||
return theListener
|
||||
}
|
||||
}
|
||||
|
||||
func runBackgroundHttp(s *http.Server, l net.Listener, tlsEnabled bool) {
|
||||
var err error
|
||||
if tlsEnabled {
|
||||
err = s.ServeTLS(l, "", "")
|
||||
} else {
|
||||
err = s.Serve(l)
|
||||
}
|
||||
if err != nil {
|
||||
if err == http.ErrServerClosed {
|
||||
log.Println("The http server shutdown successfully")
|
||||
} else {
|
||||
log.Fatalf("[Http] Error trying to host the http server: %s\n", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runBackgroundFCgi(h http.Handler, l net.Listener) {
|
||||
err := fcgi.Serve(l, h)
|
||||
if err != nil {
|
||||
if err == net.ErrClosed {
|
||||
log.Println("The fcgi server shutdown successfully")
|
||||
} else {
|
||||
log.Fatalf("[Http] Error trying to host the fcgi server: %s\n", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user