Skip to content

Commit

Permalink
Merge pull request #33 from vyloy/dev
Browse files Browse the repository at this point in the history
improve log msg and connection pool
update go dependencies
  • Loading branch information
vyloy authored Sep 15, 2023
2 parents 3d2ff2f + cb992f4 commit 5131c6f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 50 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
27 changes: 17 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,41 @@ module github.com/isrc-cas/gt
go 1.19

require (
github.com/archdx/zerolog-sentry v1.0.1
github.com/archdx/zerolog-sentry v1.5.0
github.com/buger/jsonparser v1.1.1
github.com/davecgh/go-spew v1.1.1
github.com/emirpasic/gods v1.18.1
github.com/gorilla/websocket v1.4.2
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/golang-lru/v2 v2.0.3
github.com/jonboulle/clockwork v0.2.2
github.com/lestrrat-go/strftime v1.0.5
github.com/mattn/go-pointer v0.0.1
github.com/pion/logging v0.2.2
github.com/pion/turn/v2 v2.0.8
github.com/pion/turn/v3 v3.0.1
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.26.1
github.com/rs/zerolog v1.30.0
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/getsentry/sentry-go v0.12.0 // indirect
github.com/getsentry/sentry-go v0.24.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pion/dtls/v2 v2.2.7 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/stun v0.3.5 // indirect
github.com/pion/transport v0.13.1 // indirect
github.com/pion/stun/v2 v2.0.0 // indirect
github.com/pion/transport/v2 v2.2.4 // indirect
github.com/pion/transport/v3 v3.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
Loading

0 comments on commit 5131c6f

Please sign in to comment.