Skip to content

Commit

Permalink
Merge pull request #143 from nabbar/fix_221103
Browse files Browse the repository at this point in the history
Package Logger :
- Fix too many open file for logger file
- Fix invalid FD for log file
- Fix bug with field into entry
- Fix race detection on testing

Package Status :
- Add capability to send short into query to received a short output without any result of component
- Add Connection Header as Close to router status return of Get
- Fix race detection into health with concurrent read/write data
- Fix race detection with update of main router

Package HTTPServer :
- Add option to disable keepalive for server
- Fix config tag error
- Fix logger usage / close

Package Config :
- Fix component using logger to implement a close
- Fix logger initialization :
  - use local var to setup new logger based on logger clone if loger still existing
  - if error while configuring new logger, do not change logger
  - closing old logger before replace it with new

Package Router :
- Fix missing ignore error return
- Fix golib logger not closed

Other :
- Bump dependencies
  • Loading branch information
nabbar authored Nov 7, 2022
2 parents 6ff8611 + 3994856 commit 284521c
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 118 deletions.
6 changes: 3 additions & 3 deletions config/components/aws/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type ComponentAws interface {
SetAws(a libaws.AWS)
}

func New(drv ConfigDriver, logKey string) ComponentAws {
func New(drv ConfigDriver) ComponentAws {
return &componentAws{
ctx: nil,
get: nil,
Expand All @@ -64,8 +64,8 @@ func Register(cfg libcfg.Config, key string, cpt ComponentAws) {
cfg.ComponentSet(key, cpt)
}

func RegisterNew(cfg libcfg.Config, drv ConfigDriver, key, logKey string) {
cfg.ComponentSet(key, New(drv, logKey))
func RegisterNew(cfg libcfg.Config, drv ConfigDriver, key string) {
cfg.ComponentSet(key, New(drv))
}

func Load(getCpt libcfg.FuncComponentGet, key string) ComponentAws {
Expand Down
2 changes: 2 additions & 0 deletions config/components/http/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package http

import (
"net/http"
"sync"

libcfg "github.com/nabbar/golib/config"
libhts "github.com/nabbar/golib/httpserver"
Expand Down Expand Up @@ -60,6 +61,7 @@ func New(tlsKey, logKey string, handler map[string]http.Handler) ComponentHttp {
}

return &componentHttp{
m: sync.Mutex{},
tls: tlsKey,
log: logKey,
run: false,
Expand Down
45 changes: 31 additions & 14 deletions config/components/log/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,52 @@ func (c *componentLog) _runCli(getCfg libcfg.FuncComponentConfigGet) liberr.Erro
c.m.Lock()
defer c.m.Unlock()

if c.l == nil {
if c.ctx == nil {
return ErrorComponentNotInitialized.Error(nil)
}
if c.ctx == nil {
return ErrorComponentNotInitialized.Error(nil)
}

if c.l == nil {
c.l = liblog.New(c.ctx())
c.l.SetLevel(c.v)
}

return nil
}

func (c *componentLog) _run(getCfg libcfg.FuncComponentConfigGet) liberr.Error {
var (
e error

log liblog.Logger
cnf *liblog.Options
err liberr.Error
)

if log, e = c.l.Clone(); e != nil {
log = liblog.New(c.ctx())
log.SetLevel(c.v)
}

if cnf, err = c._GetOptions(getCfg); err != nil {
return err
} else if cnf == nil {
return ErrorConfigInvalid.Error(nil)
} else if e = log.SetOptions(cnf); e != nil {
return ErrorReloadLog.Error(err)
}

if c.l != nil {
_ = c.l.Close()
}

c.l = log

return nil
}

func (c *componentLog) _run(getCfg libcfg.FuncComponentConfigGet) liberr.Error {
fb, fa := c._getFct()

if err := c._runFct(fb); err != nil {
return err
} else if err = c._runCli(getCfg); err != nil {
return err
} else if cnf, err = c._GetOptions(getCfg); err != nil {
return err
} else if cnf == nil {
return ErrorConfigInvalid.Error(nil)
} else if e := c.l.SetOptions(cnf); e != nil {
return ErrorReloadLog.Error(err)
} else if err = c._runFct(fa); err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ require (
github.com/mattn/go-colorable v0.1.13
github.com/mitchellh/go-homedir v1.1.0
github.com/nats-io/jwt/v2 v2.3.0
github.com/nats-io/nats-server/v2 v2.9.5
github.com/nats-io/nats.go v1.19.0
github.com/nats-io/nats-server/v2 v2.9.6
github.com/nats-io/nats.go v1.19.1
github.com/onsi/ginkgo/v2 v2.4.0
github.com/onsi/gomega v1.23.0
github.com/onsi/gomega v1.24.0
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.13.1
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/jwalterweatherman v1.1.0
github.com/spf13/viper v1.13.0
github.com/spf13/viper v1.14.0
github.com/vbauerster/mpb/v5 v5.4.0
github.com/xanzy/go-gitlab v0.74.0
github.com/xhit/go-simple-mail v2.2.2+incompatible
github.com/xujiajun/nutsdb v0.11.0
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326
golang.org/x/exp v0.0.0-20221106115401-f9659909a136
golang.org/x/net v0.1.0
golang.org/x/oauth2 v0.1.0
golang.org/x/sync v0.1.0
Expand Down Expand Up @@ -97,7 +97,6 @@ require (
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.6.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down
19 changes: 13 additions & 6 deletions httpserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,35 +215,41 @@ type ServerConfig struct {
// which may be active globally, which is MaxHandlers.
// If zero, MaxConcurrentStreams defaults to at least 100, per
// the HTTP/2 spec's recommendations.
MaxConcurrentStreams uint32 `json:"max_concurrent_streams" json:"max_concurrent_streams" yaml:"max_concurrent_streams" toml:"max_concurrent_streams"`
MaxConcurrentStreams uint32 `mapstructure:"max_concurrent_streams" json:"max_concurrent_streams" yaml:"max_concurrent_streams" toml:"max_concurrent_streams"`

// MaxReadFrameSize optionally specifies the largest frame
// this server is willing to read. A valid value is between
// 16k and 16M, inclusive. If zero or otherwise invalid, a
// default value is used.
MaxReadFrameSize uint32 `json:"max_read_frame_size" json:"max_read_frame_size" yaml:"max_read_frame_size" toml:"max_read_frame_size"`
MaxReadFrameSize uint32 `mapstructure:"max_read_frame_size" json:"max_read_frame_size" yaml:"max_read_frame_size" toml:"max_read_frame_size"`

// PermitProhibitedCipherSuites, if true, permits the use of
// cipher suites prohibited by the HTTP/2 spec.
PermitProhibitedCipherSuites bool `json:"permit_prohibited_cipher_suites" json:"permit_prohibited_cipher_suites" yaml:"permit_prohibited_cipher_suites" toml:"permit_prohibited_cipher_suites"`
PermitProhibitedCipherSuites bool `mapstructure:"permit_prohibited_cipher_suites" json:"permit_prohibited_cipher_suites" yaml:"permit_prohibited_cipher_suites" toml:"permit_prohibited_cipher_suites"`

// IdleTimeout specifies how long until idle clients should be
// closed with a GOAWAY frame. PING frames are not considered
// activity for the purposes of IdleTimeout.
IdleTimeout time.Duration `json:"idle_timeout" json:"idle_timeout" yaml:"idle_timeout" toml:"idle_timeout"`
IdleTimeout time.Duration `mapstructure:"idle_timeout" json:"idle_timeout" yaml:"idle_timeout" toml:"idle_timeout"`

// MaxUploadBufferPerConnection is the size of the initial flow
// control window for each connections. The HTTP/2 spec does not
// allow this to be smaller than 65535 or larger than 2^32-1.
// If the value is outside this range, a default value will be
// used instead.
MaxUploadBufferPerConnection int32 `json:"max_upload_buffer_per_connection" json:"max_upload_buffer_per_connection" yaml:"max_upload_buffer_per_connection" toml:"max_upload_buffer_per_connection"`
MaxUploadBufferPerConnection int32 `mapstructure:"max_upload_buffer_per_connection" json:"max_upload_buffer_per_connection" yaml:"max_upload_buffer_per_connection" toml:"max_upload_buffer_per_connection"`

// MaxUploadBufferPerStream is the size of the initial flow control
// window for each stream. The HTTP/2 spec does not allow this to
// be larger than 2^32-1. If the value is zero or larger than the
// maximum, a default value will be used instead.
MaxUploadBufferPerStream int32 `json:"max_upload_buffer_per_stream" json:"max_upload_buffer_per_stream" yaml:"max_upload_buffer_per_stream" toml:"max_upload_buffer_per_stream"`
MaxUploadBufferPerStream int32 `mapstructure:"max_upload_buffer_per_stream" json:"max_upload_buffer_per_stream" yaml:"max_upload_buffer_per_stream" toml:"max_upload_buffer_per_stream"`

// DisableKeepAlive controls whether HTTP keep-alives are disabled.
// By default, keep-alives are always enabled. Only very
// resource-constrained environments or servers in the process of
// shutting down should disable them.
DisableKeepAlive bool `mapstructure:"disable_keep_alive" json:"disable_keep_alive" yaml:"disable_keep_alive" toml:"disable_keep_alive"`
}

func (c *ServerConfig) Clone() ServerConfig {
Expand All @@ -262,6 +268,7 @@ func (c *ServerConfig) Clone() ServerConfig {
IdleTimeout: c.IdleTimeout,
MaxUploadBufferPerConnection: c.MaxUploadBufferPerConnection,
MaxUploadBufferPerStream: c.MaxUploadBufferPerStream,
DisableKeepAlive: c.DisableKeepAlive,
Name: c.Name,
Listen: c.Listen,
Expose: c.Expose,
Expand Down
28 changes: 24 additions & 4 deletions httpserver/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,13 @@ func (s *srvRun) Listen(cfg *ServerConfig, handler http.Handler) liberr.Error {
s.bnd = bind
s.tls = sTls || ssl.LenCertificatePair() > 0

var (
l = s.getLogger()
)

srv := &http.Server{
Addr: cfg.GetListen().Host,
ErrorLog: s.getLogger().GetStdLogger(liblog.ErrorLevel, log.LstdFlags|log.Lmicroseconds),
ErrorLog: l.GetStdLogger(liblog.ErrorLevel, log.LstdFlags|log.Lmicroseconds),
}

if cfg.ReadTimeout > 0 {
Expand All @@ -214,6 +218,12 @@ func (s *srvRun) Listen(cfg *ServerConfig, handler http.Handler) liberr.Error {
srv.IdleTimeout = cfg.IdleTimeout
}

if cfg.DisableKeepAlive {
srv.SetKeepAlivesEnabled(false)
} else {
srv.SetKeepAlivesEnabled(true)
}

if ssl.LenCertificatePair() > 0 {
srv.TLSConfig = ssl.TlsConfig("")
}
Expand Down Expand Up @@ -279,15 +289,20 @@ func (s *srvRun) Listen(cfg *ServerConfig, handler http.Handler) liberr.Error {
s.srv = srv
s.m.Unlock()

go func(ctx context.Context, cnl context.CancelFunc, name, host string, tlsMandatory bool) {
var _log = s.getLogger()
go func(ctx context.Context, cnl context.CancelFunc, _log liblog.Logger, name, host string, tlsMandatory bool) {
ent := _log.Entry(liblog.InfoLevel, "server stopped")

defer func() {
ent.Log()

if _log != nil {
_ = _log.Close()
}

if ctx != nil && cnl != nil && ctx.Err() == nil {
cnl()
}

s.setRunning(false)
}()

Expand Down Expand Up @@ -323,7 +338,7 @@ func (s *srvRun) Listen(cfg *ServerConfig, handler http.Handler) liberr.Error {
ent.Level = liblog.ErrorLevel
ent.ErrorAdd(true, er)
}
}(s.ctx, s.cnl, name, bind, sTls)
}(s.ctx, s.cnl, l, name, bind, sTls)

return nil
}
Expand All @@ -347,6 +362,7 @@ func (s *srvRun) srvShutdown() {

defer func() {
cancel()

if s.srv != nil {
err := s.srv.Close()

Expand All @@ -356,6 +372,10 @@ func (s *srvRun) srvShutdown() {

_log.Entry(liblog.ErrorLevel, "closing server").ErrorAdd(true, err).Check(liblog.InfoLevel)
}

if _log != nil {
_ = _log.Close()
}
}()

if s.srv != nil {
Expand Down
6 changes: 3 additions & 3 deletions logger/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func (e *Entry) SetGinContext(ctx *gin.Context) *Entry {

// FieldAdd allow to add one couple key/val as type string/interface into the custom field of the entry.
func (e *Entry) FieldAdd(key string, val interface{}) *Entry {
e.Fields.Add(key, val)
e.Fields = e.Fields.Add(key, val)
return e
}

// FieldMerge allow to merge a Field pointer into the custom field of the entry.
func (e *Entry) FieldMerge(fields Fields) *Entry {
e.Fields.Merge(fields)
e.Fields = e.Fields.Merge(fields)
return e
}

Expand All @@ -110,7 +110,7 @@ func (e *Entry) FieldSet(fields Fields) *Entry {
}

func (e *Entry) FieldClean(keys ...string) *Entry {
e.Fields.Clean(keys...)
e.Fields = e.Fields.Clean(keys...)
return e
}

Expand Down
Loading

0 comments on commit 284521c

Please sign in to comment.