mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
Bump dependancies
- AWS SDK to release v1.0.0 - other dependancies Fix Packages : - AWS : fix validator function, rules, config model - Certificates : fix func NewFrom - HTTPServer: fix IsRunning Fix other : - Fix CI/CD job to prevent alert on files modified - Fix missing licence comment header
This commit is contained in:
committed by
Nicolas JUHEL
parent
d46a10bb82
commit
1249f319bc
@@ -1,3 +1,29 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
|
||||
@@ -35,6 +35,7 @@ const (
|
||||
ErrorPoolValidate
|
||||
ErrorPoolListen
|
||||
ErrorServerValidate
|
||||
ErrorPortUse
|
||||
)
|
||||
|
||||
var isCodeError = false
|
||||
@@ -64,6 +65,8 @@ func getMessage(code errors.CodeError) (message string) {
|
||||
return "at least one server has listen error"
|
||||
case ErrorServerValidate:
|
||||
return "config server seems to be not valid"
|
||||
case ErrorPortUse:
|
||||
return "server port is still used"
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2019 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
func ListenWaitNotify(allSrv ...HTTPServer) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(allSrv))
|
||||
|
||||
for _, s := range allSrv {
|
||||
go func(serv HTTPServer) {
|
||||
defer wg.Done()
|
||||
serv.Listen()
|
||||
serv.WaitNotify()
|
||||
}(s)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func Listen(allSrv ...HTTPServer) {
|
||||
for _, s := range allSrv {
|
||||
go func(serv HTTPServer) {
|
||||
serv.Listen()
|
||||
}(s)
|
||||
}
|
||||
}
|
||||
|
||||
func Restart(allSrv ...HTTPServer) {
|
||||
for _, s := range allSrv {
|
||||
s.Restart()
|
||||
}
|
||||
}
|
||||
|
||||
func Shutdown(allSrv ...HTTPServer) {
|
||||
for _, s := range allSrv {
|
||||
s.Shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
func IsRunning(allSrv ...HTTPServer) bool {
|
||||
for _, s := range allSrv {
|
||||
if s.IsRunning() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -35,6 +35,8 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/nabbar/golib/logger"
|
||||
|
||||
"github.com/nabbar/golib/semaphore"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
@@ -109,24 +111,21 @@ func (p pool) Add(srv ...Server) (PoolServer, liberr.Error) {
|
||||
for _, s := range srv {
|
||||
if !r.Has(s.GetBindable()) {
|
||||
r = append(r, s)
|
||||
continue
|
||||
}
|
||||
|
||||
if x := r.Get(s.GetBindable()); x != nil {
|
||||
s.Merge(x)
|
||||
|
||||
if x.IsRunning() {
|
||||
x.Shutdown()
|
||||
if e := s.Listen(nil); e != nil {
|
||||
return r, e
|
||||
}
|
||||
for _, x := range r {
|
||||
if x.GetBindable() != s.GetBindable() {
|
||||
continue
|
||||
} else if !x.Merge(s) {
|
||||
r = r.Del(s.GetBindable()).(pool)
|
||||
r = append(r, s)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
r = r.Del(s.GetBindable()).(pool)
|
||||
r = append(r, s)
|
||||
}
|
||||
|
||||
return append(p, srv...), nil
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (p pool) Get(bindAddress string) Server {
|
||||
@@ -150,10 +149,6 @@ func (p pool) Del(bindAddress string) PoolServer {
|
||||
|
||||
var r = make(pool, 0)
|
||||
|
||||
if p != nil {
|
||||
r = p
|
||||
}
|
||||
|
||||
for _, s := range p {
|
||||
if s.GetBindable() != bindAddress {
|
||||
r = append(r, s)
|
||||
@@ -336,31 +331,14 @@ func (p pool) Listen(handler http.Handler) liberr.Error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
e liberr.Error
|
||||
s semaphore.Sem
|
||||
x context.Context
|
||||
c context.CancelFunc
|
||||
)
|
||||
|
||||
defer func() {
|
||||
c()
|
||||
s.DeferMain()
|
||||
}()
|
||||
var e liberr.Error
|
||||
|
||||
e = ErrorPoolListen.Error(nil)
|
||||
x, c = context.WithTimeout(context.Background(), timeoutRestart)
|
||||
s = semaphore.NewSemaphoreWithContext(x, 0)
|
||||
|
||||
logger.InfoLevel.Log("Calling listen for All Servers")
|
||||
p.MapRun(func(srv Server) {
|
||||
_ = s.NewWorker()
|
||||
go func() {
|
||||
defer s.DeferWorker()
|
||||
e.AddParentError(srv.Listen(handler))
|
||||
}()
|
||||
e.AddParentError(srv.Listen(handler))
|
||||
})
|
||||
|
||||
_ = s.WaitAll()
|
||||
logger.InfoLevel.Log("End of Calling listen for All Servers")
|
||||
|
||||
if !e.HasParent() {
|
||||
e = nil
|
||||
|
||||
@@ -1,3 +1,29 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020 Nicolas JUHEL
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
@@ -27,6 +53,7 @@ type server struct {
|
||||
run atomic.Value
|
||||
cfg *ServerConfig
|
||||
srv *http.Server
|
||||
cnl context.CancelFunc
|
||||
}
|
||||
|
||||
type Server interface {
|
||||
@@ -51,6 +78,7 @@ func NewServer(cfg *ServerConfig) Server {
|
||||
return &server{
|
||||
cfg: cfg,
|
||||
srv: nil,
|
||||
cnl: nil,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +107,13 @@ func (s *server) GetExpose() string {
|
||||
}
|
||||
|
||||
func (s *server) IsRunning() bool {
|
||||
return s.run.Load().(bool)
|
||||
if i := s.run.Load(); i == nil {
|
||||
return false
|
||||
} else if b, ok := i.(bool); !ok {
|
||||
return false
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) IsTLS() bool {
|
||||
@@ -169,10 +203,19 @@ func (s *server) Listen(handler http.Handler) liberr.Error {
|
||||
s.Shutdown()
|
||||
}
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if e := s.PortInUse(); e != nil {
|
||||
s.Shutdown()
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
s.srv = srv
|
||||
|
||||
go func() {
|
||||
ctx, cnl := context.WithCancel(s.cfg.getContext())
|
||||
s.cnl = cnl
|
||||
|
||||
defer func() {
|
||||
cnl()
|
||||
@@ -197,13 +240,16 @@ func (s *server) Listen(handler http.Handler) liberr.Error {
|
||||
err = s.srv.ListenAndServe()
|
||||
}
|
||||
|
||||
if !errors.Is(err, ctx.Err()) {
|
||||
if err != nil && ctx.Err() != nil && ctx.Err().Error() == err.Error() {
|
||||
return
|
||||
} else if err != nil && errors.Is(err, http.ErrServerClosed) {
|
||||
return
|
||||
} else if err != nil {
|
||||
liblog.ErrorLevel.LogErrorCtxf(liblog.NilLevel, "Listen Server '%s'", err, s.GetName())
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (s *server) WaitNotify() {
|
||||
@@ -230,21 +276,61 @@ func (s *server) Shutdown() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeoutShutdown)
|
||||
defer func() {
|
||||
cancel()
|
||||
|
||||
if s.srv != nil {
|
||||
_ = s.srv.Close()
|
||||
}
|
||||
|
||||
s.setNotRunning()
|
||||
}()
|
||||
|
||||
liblog.InfoLevel.Logf("Shutdown Server '%s'...", s.GetName())
|
||||
|
||||
if err := s.srv.Shutdown(ctx); err != nil {
|
||||
liblog.ErrorLevel.Logf("Shutdown Server '%s' Error: %v", s.GetName(), err)
|
||||
if s.cnl != nil {
|
||||
s.cnl()
|
||||
}
|
||||
|
||||
if s.srv != nil {
|
||||
err := s.srv.Shutdown(ctx)
|
||||
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
liblog.ErrorLevel.Logf("Shutdown Server '%s' Error: %v", s.GetName(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) Merge(srv Server) bool {
|
||||
if x, ok := srv.(*server); ok {
|
||||
s.srv = x.srv
|
||||
s.cfg = x.cfg
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *server) PortInUse() liberr.Error {
|
||||
var (
|
||||
dia = net.Dialer{}
|
||||
con net.Conn
|
||||
err error
|
||||
ctx context.Context
|
||||
cnl context.CancelFunc
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if cnl != nil {
|
||||
cnl()
|
||||
}
|
||||
if con != nil {
|
||||
_ = con.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
ctx, cnl = context.WithTimeout(context.TODO(), 2*time.Second)
|
||||
con, err = dia.DialContext(ctx, "tcp", s.cfg.Listen)
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return ErrorPortUse.Error(nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user