Skip to content

Commit

Permalink
client: improve log msg and connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
vyloy committed Sep 15, 2023
1 parent 3d2ff2f commit a3a9aa1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
21 changes: 17 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 7 additions & 21 deletions client/idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
running status = iota
idle
wait
connecting
)

type status int
Expand All @@ -34,6 +35,7 @@ type idleManager struct {
statusCond *sync.Cond
min uint
close atomic.Bool
initMtx sync.Mutex
}

func (m *idleManager) String() string {
Expand All @@ -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 {
Expand All @@ -68,7 +70,7 @@ func (m *idleManager) InitIdle(id uint) (exit bool) {
n++
}
}
if n <= m.min {
if n < m.min {
return false
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -196,7 +182,7 @@ func (m *idleManager) WaitIdle(id uint) {
m.statusCond.Wait()
continue
}
m.status[id] = idle
m.status[id] = connecting
return
}
}
Expand Down
4 changes: 3 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -311,6 +312,7 @@ func (s *Server) Start() (err error) {
return
}
}
s.Logger.Info().Msg(spew.Sdump(s.config))
return
}

Expand Down

0 comments on commit a3a9aa1

Please sign in to comment.