Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions internal/sock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"os"
"strings"
"sync"
"sync/atomic"

"github.com/dagu-dev/dagu/internal/utils"
)
Expand All @@ -17,7 +19,8 @@ import (
type Server struct {
*Config
listener net.Listener
quit bool
quit atomic.Bool
mu sync.Mutex
}

type Config struct {
Expand All @@ -32,7 +35,6 @@ type HttpHandlerFunc func(w http.ResponseWriter, r *http.Request)
func NewServer(c *Config) (*Server, error) {
return &Server{
Config: c,
quit: false,
}, nil
}

Expand Down Expand Up @@ -61,7 +63,7 @@ func (svr *Server) Serve(listen chan error) error {
}()
for {
conn, err := svr.listener.Accept()
if svr.quit {
if svr.quit.Load() {
return ErrServerRequestedShutdown
}
if err == nil {
Expand All @@ -79,8 +81,10 @@ func (svr *Server) Serve(listen chan error) error {

// Shutdown stops the frontend.
func (svr *Server) Shutdown() error {
if !svr.quit {
svr.quit = true
svr.mu.Lock()
defer svr.mu.Unlock()
if !svr.quit.Load() {
svr.quit.Store(true)
if svr.listener != nil {
err := svr.listener.Close()
utils.LogErr("close listener", err)
Expand Down