diff --git a/client/client.go b/client/client.go index 3b90a610..adc89baf 100644 --- a/client/client.go +++ b/client/client.go @@ -37,6 +37,7 @@ import ( "time" "github.com/buger/jsonparser" + "github.com/davecgh/go-spew/spew" "github.com/isrc-cas/gt/client/api" "github.com/isrc-cas/gt/client/webrtc" "github.com/isrc-cas/gt/config" @@ -267,7 +268,7 @@ func (d *dialer) initWithRemoteAPI(c *Client) (err error) { } stunAddr, err := jsonparser.GetString(r, "stunAddress") if err != nil { - if err != jsonparser.KeyPathNotFoundError { + if !errors.Is(err, jsonparser.KeyPathNotFoundError) { return } } @@ -285,7 +286,7 @@ func (d *dialer) tlsDial() (conn net.Conn, err error) { // Start runs the client agent. func (c *Client) Start() (err error) { - c.Logger.Info().Interface("config", c.Config()).Msg(predef.Version) + c.Logger.Info().Msg(predef.Version) var level webrtc.LoggingSeverity switch c.Config().WebRTCLogLevel { @@ -375,6 +376,9 @@ func (c *Client) Start() (err error) { } c.idleManager = newIdleManager(c.Config().RemoteIdleConnections) + conf4Log := *c.Config() + conf4Log.Secret = "******" + c.Logger.Info().Msg(spew.Sdump(conf4Log)) for i := uint(1); i <= c.Config().RemoteConnections; i++ { go c.connectLoop(dialer, i) c.waitTunnelsShutdown.Add(1) @@ -481,16 +485,21 @@ func (c *Client) connect(d dialer, connID uint) (closing bool) { } }() - exit := c.idleManager.InitIdle(connID) + c.idleManager.initMtx.Lock() + exit := c.idleManager.Init(connID) if !exit { c.Logger.Info().Uint("connID", connID).Msg("trying to connect to remote") conn, err := c.initConn(d, connID) if err == nil { + c.idleManager.SetIdle(connID) + c.idleManager.initMtx.Unlock() conn.readLoop(connID) } else { + c.idleManager.initMtx.Unlock() c.Logger.Error().Err(err).Uint("connID", connID).Msg("failed to connect to remote") } } else { + c.idleManager.initMtx.Unlock() c.Logger.Info().Uint("connID", connID).Msg("wait to connect to remote") } @@ -509,7 +518,7 @@ func (c *Client) connect(d dialer, connID uint) (closing bool) { if err == nil { break } - c.Logger.Error().Err(err).Msg("failed to query server address") + c.Logger.Error().Uint("connID", connID).Err(err).Msg("failed to query server address") time.Sleep(c.Config().ReconnectDelay) } return @@ -771,6 +780,10 @@ func (c *Client) ReloadServices() (err error) { i := copy(buf, connection.ServicesBytes) n := gen(conf, services, buf[i:]) + conf4Log := conf + conf4Log.Secret = "******" + c.Logger.Info().Str("config", "reloading").Msg(spew.Sdump(conf4Log)) + c.initConnMtx.Lock() defer c.initConnMtx.Unlock() c.config.Store(&conf) diff --git a/client/idle.go b/client/idle.go index 0ad58474..e0790bc8 100644 --- a/client/idle.go +++ b/client/idle.go @@ -24,6 +24,7 @@ const ( running status = iota idle wait + connecting ) type status int @@ -34,6 +35,7 @@ type idleManager struct { statusCond *sync.Cond min uint close atomic.Bool + initMtx sync.Mutex } func (m *idleManager) String() string { @@ -51,14 +53,14 @@ func newIdleManager(min uint) *idleManager { return m } -func (m *idleManager) InitIdle(id uint) (exit bool) { +func (m *idleManager) Init(id uint) (exit bool) { m.statusMtx.Lock() defer m.statusMtx.Unlock() - if v, ok := m.status[id]; ok && v == idle { + if v, ok := m.status[id]; ok && v == connecting { return false } - m.status[id] = idle + m.status[id] = connecting var n uint for _, s := range m.status { switch s { @@ -68,7 +70,7 @@ func (m *idleManager) InitIdle(id uint) (exit bool) { n++ } } - if n <= m.min { + if n < m.min { return false } @@ -152,22 +154,6 @@ func (m *idleManager) SetRunningWithTaskCount(id uint, taskCount uint32) { m.statusMtx.Unlock() } -func (m *idleManager) SetRunning(id uint) { - m.statusMtx.RLock() - s := m.status[id] - m.statusMtx.RUnlock() - if s == running { - return - } - - m.statusMtx.Lock() - if m.status[id] != running { - m.status[id] = running - defer m.statusCond.Signal() - } - m.statusMtx.Unlock() -} - func (m *idleManager) SetIdle(id uint) { m.statusMtx.Lock() m.status[id] = idle @@ -196,7 +182,7 @@ func (m *idleManager) WaitIdle(id uint) { m.statusCond.Wait() continue } - m.status[id] = idle + m.status[id] = connecting return } } diff --git a/server/server.go b/server/server.go index d18740c7..c7ab89d6 100644 --- a/server/server.go +++ b/server/server.go @@ -35,6 +35,7 @@ import ( "time" "github.com/buger/jsonparser" + "github.com/davecgh/go-spew/spew" "github.com/isrc-cas/gt/config" "github.com/isrc-cas/gt/logger" "github.com/isrc-cas/gt/predef" @@ -206,7 +207,7 @@ func (s *Server) acceptLoop(l net.Listener, handle func(*conn)) { // Start runs the server. func (s *Server) Start() (err error) { - s.Logger.Info().Interface("config", &s.config).Msg(predef.Version) + s.Logger.Info().Msg(predef.Version) err = s.users.mergeUsers(s.config.Users, nil, nil) if err != nil { @@ -311,6 +312,7 @@ func (s *Server) Start() (err error) { return } } + s.Logger.Info().Msg(spew.Sdump(s.config)) return }