Skip to content

Commit

Permalink
add mutex for prom transport
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed May 22, 2022
1 parent 6ba9352 commit 6a7b543
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

NOW = $(shell date -u '+%Y%m%d%I%M%S')

RELEASE_VERSION = 5.8.0
RELEASE_VERSION = 5.8.1

APP = n9e
SERVER_BIN = $(APP)
Expand Down
15 changes: 7 additions & 8 deletions etc/webapi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ MetricsYamlFile = "./etc/metrics.yaml"
BuiltinAlertsDir = "./etc/alerts"
BuiltinDashboardsDir = "./etc/dashboards"

# config | api
ClustersFrom = "config"

# using when ClustersFrom = "api"
ClustersFromAPIs = []

[[NotifyChannels]]
Label = "邮箱"
# do not change Key
Expand Down Expand Up @@ -172,14 +178,7 @@ BasicAuthUser = ""
BasicAuthPass = ""
# timeout settings, unit: ms
Timeout = 30000
DialTimeout = 10000
TLSHandshakeTimeout = 30000
ExpectContinueTimeout = 1000
IdleConnTimeout = 90000
# time duration, unit: ms
KeepAlive = 30000
MaxConnsPerHost = 0
MaxIdleConns = 100
DialTimeout = 3000
MaxIdleConnsPerHost = 100

[Ibex]
Expand Down
21 changes: 18 additions & 3 deletions src/webapi/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/didi/nightingale/v5/src/pkg/oidcc"
"github.com/didi/nightingale/v5/src/pkg/ormx"
"github.com/didi/nightingale/v5/src/storage"
"github.com/didi/nightingale/v5/src/webapi/prom"
)

var (
Expand Down Expand Up @@ -82,6 +81,8 @@ type Config struct {
MetricsYamlFile string
BuiltinAlertsDir string
BuiltinDashboardsDir string
ClustersFrom string
ClustersFromAPIs []string
ContactKeys []LabelAndKey
NotifyChannels []LabelAndKey
Log logx.Config
Expand All @@ -91,12 +92,26 @@ type Config struct {
AnonymousAccess AnonymousAccess
LDAP ldapx.LdapSection
Redis storage.RedisConfig
DB ormx.DBConfig
Clusters []prom.Options
DB ormx.DBConfig
Clusters []ClusterOptions
Ibex Ibex
OIDC oidcc.Config
}

type ClusterOptions struct {
Name string
Prom string

BasicAuthUser string
BasicAuthPass string

Timeout int64
DialTimeout int64
KeepAlive int64

MaxIdleConnsPerHost int
}

type LabelAndKey struct {
Label string `json:"label"`
Key string `json:"key"`
Expand Down
63 changes: 25 additions & 38 deletions src/webapi/prom/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,59 @@ package prom
import (
"net"
"net/http"
"sync"
"time"
)

type Options struct {
Name string
Prom string

BasicAuthUser string
BasicAuthPass string

Timeout int64
DialTimeout int64
TLSHandshakeTimeout int64
ExpectContinueTimeout int64
IdleConnTimeout int64
KeepAlive int64

MaxConnsPerHost int
MaxIdleConns int
MaxIdleConnsPerHost int
}
"github.com/didi/nightingale/v5/src/webapi/config"
)

type ClusterType struct {
Opts Options
Opts config.ClusterOptions
Transport *http.Transport
}

type ClustersType struct {
M map[string]ClusterType
}

func NewClusters() ClustersType {
return ClustersType{
M: make(map[string]ClusterType),
}
datas map[string]ClusterType
mutex *sync.RWMutex
}

func (cs *ClustersType) Put(name string, cluster ClusterType) {
cs.M[name] = cluster
cs.mutex.Lock()
cs.datas[name] = cluster
cs.mutex.Unlock()
}

func (cs *ClustersType) Get(name string) (ClusterType, bool) {
c, has := cs.M[name]
cs.mutex.RLock()
defer cs.mutex.RUnlock()

c, has := cs.datas[name]
return c, has
}

var Clusters = NewClusters()
var Clusters = ClustersType{
datas: make(map[string]ClusterType),
mutex: new(sync.RWMutex),
}

func Init() error {
if config.C.ClustersFrom != "" && config.C.ClustersFrom != "config" {
return nil
}

opts := config.C.Clusters

func Init(opts []Options) error {
for i := 0; i < len(opts); i++ {
cluster := ClusterType{
Opts: opts[i],
Transport: &http.Transport{
// TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond,
KeepAlive: time.Duration(opts[i].KeepAlive) * time.Millisecond,
Timeout: time.Duration(opts[i].DialTimeout) * time.Millisecond,
}).DialContext,
ResponseHeaderTimeout: time.Duration(opts[i].Timeout) * time.Millisecond,
TLSHandshakeTimeout: time.Duration(opts[i].TLSHandshakeTimeout) * time.Millisecond,
ExpectContinueTimeout: time.Duration(opts[i].ExpectContinueTimeout) * time.Millisecond,
MaxConnsPerHost: opts[i].MaxConnsPerHost,
MaxIdleConns: opts[i].MaxIdleConns,
MaxIdleConnsPerHost: opts[i].MaxIdleConnsPerHost,
IdleConnTimeout: time.Duration(opts[i].IdleConnTimeout) * time.Millisecond,
},
}
Clusters.Put(opts[i].Name, cluster)
Expand Down
2 changes: 1 addition & 1 deletion src/webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (a Webapi) initialize() (func(), error) {
models.InitRoot()

// init prometheus proxy config
if err = prom.Init(config.C.Clusters); err != nil {
if err = prom.Init(); err != nil {
return nil, err
}

Expand Down

0 comments on commit 6a7b543

Please sign in to comment.