Commit 821c0fab authored by Matthew Holt's avatar Matthew Holt

core: Refactoring POSIX-only code for build tags

parent 5b196230
...@@ -3,52 +3,17 @@ package caddy ...@@ -3,52 +3,17 @@ package caddy
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/mholt/caddy/caddy/letsencrypt" "github.com/mholt/caddy/caddy/letsencrypt"
"github.com/mholt/caddy/server"
) )
func init() { func init() {
letsencrypt.OnRenew = func() error { return Restart(nil) } letsencrypt.OnRenew = func() error { return Restart(nil) }
// Trap signals
go func() {
shutdown, reload := make(chan os.Signal, 1), make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, os.Kill) // quit the process
signal.Notify(reload, syscall.SIGUSR1) // reload configuration
for {
select {
case <-shutdown:
var exitCode int
serversMu.Lock()
errs := server.ShutdownCallbacks(servers)
serversMu.Unlock()
if len(errs) > 0 {
for _, err := range errs {
log.Println(err)
}
exitCode = 1
}
os.Exit(exitCode)
case <-reload:
err := Restart(nil)
if err != nil {
log.Println(err)
}
}
}
}()
} }
// isLocalhost returns true if the string looks explicitly like a localhost address. // isLocalhost returns true if the string looks explicitly like a localhost address.
...@@ -72,3 +37,31 @@ func checkFdlimit() { ...@@ -72,3 +37,31 @@ func checkFdlimit() {
} }
} }
} }
// caddyfileGob maps bind address to index of the file descriptor
// in the Files array passed to the child process. It also contains
// the caddyfile contents. Used only during graceful restarts.
type caddyfileGob struct {
ListenerFds map[string]uintptr
Caddyfile []byte
}
// isRestart returns whether this process is, according
// to env variables, a fork as part of a graceful restart.
func isRestart() bool {
return os.Getenv("CADDY_RESTART") == "true"
}
// CaddyfileInput represents a Caddyfile as input
// and is simply a convenient way to implement
// the Input interface.
type CaddyfileInput struct {
Filepath string
Contents []byte
}
// Body returns c.Contents.
func (c CaddyfileInput) Body() []byte { return c.Contents }
// Path returns c.Filepath.
func (c CaddyfileInput) Path() string { return c.Filepath }
// +build !windows
package caddy package caddy
import ( import (
...@@ -5,18 +7,9 @@ import ( ...@@ -5,18 +7,9 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"runtime"
"syscall" "syscall"
) )
// caddyfileGob maps bind address to index of the file descriptor
// in the Files array passed to the child process. It also contains
// the caddyfile contents. Used only during graceful restarts.
type caddyfileGob struct {
ListenerFds map[string]uintptr
Caddyfile []byte
}
// Restart restarts the entire application; gracefully with zero // Restart restarts the entire application; gracefully with zero
// downtime if on a POSIX-compatible system, or forcefully if on // downtime if on a POSIX-compatible system, or forcefully if on
// Windows but with imperceptibly-short downtime. // Windows but with imperceptibly-short downtime.
...@@ -31,18 +24,6 @@ func Restart(newCaddyfile Input) error { ...@@ -31,18 +24,6 @@ func Restart(newCaddyfile Input) error {
caddyfileMu.Unlock() caddyfileMu.Unlock()
} }
if runtime.GOOS == "windows" {
err := Stop()
if err != nil {
return err
}
err = Start(newCaddyfile)
if err != nil {
return err
}
return nil
}
if len(os.Args) == 0 { // this should never happen, but just in case... if len(os.Args) == 0 { // this should never happen, but just in case...
os.Args = []string{""} os.Args = []string{""}
} }
...@@ -110,23 +91,3 @@ func Restart(newCaddyfile Input) error { ...@@ -110,23 +91,3 @@ func Restart(newCaddyfile Input) error {
// Child process is listening now; we can stop all our servers here. // Child process is listening now; we can stop all our servers here.
return Stop() return Stop()
} }
// isRestart returns whether this process is, according
// to env variables, a fork as part of a graceful restart.
func isRestart() bool {
return os.Getenv("CADDY_RESTART") == "true"
}
// CaddyfileInput represents a Caddyfile as input
// and is simply a convenient way to implement
// the Input interface.
type CaddyfileInput struct {
Filepath string
Contents []byte
}
// Body returns c.Contents.
func (c CaddyfileInput) Body() []byte { return c.Contents }
// Path returns c.Filepath.
func (c CaddyfileInput) Path() string { return c.Filepath }
package caddy
func Restart(newCaddyfile Input) error {
if newCaddyfile == nil {
caddyfileMu.Lock()
newCaddyfile = caddyfile
caddyfileMu.Unlock()
}
wg.Add(1) // barrier so Wait() doesn't unblock
err := Stop()
if err != nil {
return err
}
err = Start(newCaddyfile)
if err != nil {
return err
}
wg.Done() // take down our barrier
return nil
}
package caddy
import (
"log"
"os"
"os/signal"
"github.com/mholt/caddy/server"
)
func init() {
// Trap quit signals (cross-platform)
go func() {
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, os.Kill)
<-shutdown
var exitCode int
serversMu.Lock()
errs := server.ShutdownCallbacks(servers)
serversMu.Unlock()
if len(errs) > 0 {
for _, err := range errs {
log.Println(err)
}
exitCode = 1
}
os.Exit(exitCode)
}()
}
// +build !windows
package caddy
import (
"log"
"os"
"os/signal"
"syscall"
)
func init() {
// Trap POSIX-only signals
go func() {
reload := make(chan os.Signal, 1)
signal.Notify(reload, syscall.SIGUSR1) // reload configuration
for {
<-reload
err := Restart(nil)
if err != nil {
log.Println(err)
}
}
}()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment