Skip to content

Commit

Permalink
apply suggestion
Browse files Browse the repository at this point in the history
apply suggestion
  • Loading branch information
jiuker committed Jul 11, 2024
1 parent d9e7458 commit 73af42a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
39 changes: 15 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,17 @@ const (
)

var (
globalQuietEnabled bool
globalDebugEnabled bool
globalLoggingEnabled bool
globalTrace string
globalJSONEnabled bool
globalConsoleDisplay bool
globalErrorsOnly bool
globalStatusCodes []int
globalConnStatsRWMutex sync.RWMutex
globalConnStats []*ConnStats
log2 *logrus.Logger
globalHostBalance string
globalQuietEnabled bool
globalDebugEnabled bool
globalLoggingEnabled bool
globalTrace string
globalJSONEnabled bool
globalConsoleDisplay bool
globalErrorsOnly bool
globalStatusCodes []int
globalConnStats atomic.Pointer[[]*ConnStats]
log2 *logrus.Logger
globalHostBalance string
)

const (
Expand Down Expand Up @@ -401,12 +400,7 @@ func (b *Backend) updateCallStats(t shortTraceMsg) {
b.Stats.MinLatency = time.Duration(int64(math.Min(float64(b.Stats.MinLatency), float64(t.CallStats.Latency))))
b.Stats.Rx += int64(t.CallStats.Rx)
b.Stats.Tx += int64(t.CallStats.Tx)
// automatically update the global stats
// Read/Write Lock is not required here
globalConnStatsRWMutex.RLock()
connStats := globalConnStats
globalConnStatsRWMutex.RUnlock()
for _, c := range connStats {
for _, c := range *globalConnStats.Load() {
if c == nil {
continue
}
Expand Down Expand Up @@ -863,10 +857,7 @@ func configureSite(ctxt context.Context, ctx *cli.Context, siteNum int, siteStrs
var backends []*Backend
var prevScheme string
var transport http.RoundTripper
globalConnStatsRWMutex.Lock()
defer globalConnStatsRWMutex.Unlock()
// reset connstats
globalConnStats = []*ConnStats{}
var connStats []*ConnStats
if len(endpoints) == 1 && ctx.GlobalBool("rr-dns-mode") {
// guess it is LB config address
target, err := url.Parse(endpoints[0])
Expand Down Expand Up @@ -940,9 +931,9 @@ func configureSite(ctxt context.Context, ctx *cli.Context, siteNum int, siteStrs
go backend.healthCheck(ctxt)
proxy.ErrorHandler = backend.ErrorHandler
backends = append(backends, backend)
globalConnStats = append(globalConnStats, newConnStats(endpoint))
connStats = append(connStats, newConnStats(endpoint))
}

globalConnStats.Store(&connStats)
return &site{
backends: backends,
}
Expand Down
7 changes: 1 addition & 6 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ func (c *sidekickCollector) Describe(ch chan<- *prometheus.Desc) {

// Collect is called by the Prometheus registry when collecting metrics.
func (c *sidekickCollector) Collect(ch chan<- prometheus.Metric) {
// automatically read the global stats
// Read/Write Lock is not required here
globalConnStatsRWMutex.RLock()
connStats := globalConnStats
globalConnStatsRWMutex.RUnlock()
for _, c := range connStats {
for _, c := range *globalConnStats.Load() {
if c == nil {
continue
}
Expand Down

0 comments on commit 73af42a

Please sign in to comment.