diff --git a/go.mod b/go.mod index 8a40225f..0bbba05a 100644 --- a/go.mod +++ b/go.mod @@ -101,6 +101,7 @@ require ( github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect github.com/rs/xid v1.4.0 // indirect github.com/smartwalle/crypto4go v1.0.2 // indirect + github.com/sourcegraph/conc v0.2.0 github.com/spf13/afero v1.9.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 8a90f8f9..d5987397 100644 --- a/go.sum +++ b/go.sum @@ -1265,8 +1265,8 @@ github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= -github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY= @@ -1315,6 +1315,8 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9 github.com/snowflakedb/gosnowflake v1.6.3/go.mod h1:6hLajn6yxuJ4xUHZegMekpq9rnQbGJ7TMwXjgTmA6lg= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0= +github.com/sourcegraph/conc v0.2.0 h1:96VpOCAtXDCQ8Oycz0ftHqdPyMi8w12ltN4L2noYg7s= +github.com/sourcegraph/conc v0.2.0/go.mod h1:8lmPpTLA0hsWqw4lw7wS1e694U2tMjRrc1Asvupb4QM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= diff --git a/internal/service/server.go b/internal/service/server.go index 5ebbca04..7ea3554d 100644 --- a/internal/service/server.go +++ b/internal/service/server.go @@ -6,12 +6,12 @@ package service import ( "fmt" - "sync" "github.com/fatih/color" "github.com/gin-gonic/gin" "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/pkg/util" + "github.com/sourcegraph/conc" ) var ( @@ -50,21 +50,19 @@ func (p *serverPool[T]) from(addr string, newServer func() T) T { return s } -func (p *serverPool[T]) startServer(wg *sync.WaitGroup, maxSidSize int) { +func (p *serverPool[T]) startServer(wg *conc.WaitGroup, maxSidSize int) { for _, srv := range p.servers { - wg.Add(1) - go func(t T) { - ss := t.services() - if len(ss) < 1 { - return - } + ss := srv.services() + if len(ss) == 0 { + continue + } + startSrv := srv.start + wg.Go(func() { for _, s := range ss { colorPrint(actOnStart, s.OnStart(), maxSidSize, s) } - colorPrint(actStart, t.start(), maxSidSize, ss...) - // remember to done sync.WaitGroup - wg.Done() - }(srv) + colorPrint(actStart, startSrv(), maxSidSize, ss...) + }) } } @@ -164,7 +162,7 @@ func colorPrint(act byte, err error, l int, ss ...Service) { } // Start start all servers -func Start(wg *sync.WaitGroup) { +func Start(wg *conc.WaitGroup) { srvSize, maxSidSize := checkServices() if srvSize < 1 { return diff --git a/main.go b/main.go index 9245cc24..3b16fa38 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "os" "os/signal" "strings" - "sync" "syscall" "github.com/fatih/color" @@ -19,6 +18,7 @@ import ( "github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/pkg/debug" "github.com/rocboss/paopao-ce/pkg/util" + "github.com/sourcegraph/conc" ) var ( @@ -64,13 +64,12 @@ func main() { debug.StartPyroscope() // start services - wg := &sync.WaitGroup{} + wg := &conc.WaitGroup{} fmt.Fprintf(color.Output, "\nstarting run service...\n\n") service.Start(wg) // graceful stop services - wg.Add(1) - go func() { + wg.Go(func() { quit := make(chan os.Signal, 1) // kill (no param) default send syscall.SIGTERM // kill -2 is syscall.SIGINT @@ -79,7 +78,6 @@ func main() { <-quit fmt.Fprintf(color.Output, "\nshutting down server...\n\n") service.Stop() - wg.Done() - }() + }) wg.Wait() }