From 6a7b543ad69f9ec843e2858ed689e4e540cf9a79 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Sun, 22 May 2022 12:45:25 +0800 Subject: [PATCH] add mutex for prom transport --- Makefile | 2 +- etc/webapi.conf | 15 +++++---- src/webapi/config/config.go | 21 +++++++++++-- src/webapi/prom/prom.go | 63 +++++++++++++++---------------------- src/webapi/webapi.go | 2 +- 5 files changed, 52 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index 22a6dca4..760f95f3 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/etc/webapi.conf b/etc/webapi.conf index 7cbd2642..0ba8ddd7 100644 --- a/etc/webapi.conf +++ b/etc/webapi.conf @@ -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 @@ -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] diff --git a/src/webapi/config/config.go b/src/webapi/config/config.go index d158a154..bb775008 100644 --- a/src/webapi/config/config.go +++ b/src/webapi/config/config.go @@ -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 ( @@ -82,6 +81,8 @@ type Config struct { MetricsYamlFile string BuiltinAlertsDir string BuiltinDashboardsDir string + ClustersFrom string + ClustersFromAPIs []string ContactKeys []LabelAndKey NotifyChannels []LabelAndKey Log logx.Config @@ -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"` diff --git a/src/webapi/prom/prom.go b/src/webapi/prom/prom.go index 23b8c39d..849a6b9c 100644 --- a/src/webapi/prom/prom.go +++ b/src/webapi/prom/prom.go @@ -3,55 +3,48 @@ package prom import ( "net" "net/http" + "sync" "time" + + "github.com/didi/nightingale/v5/src/webapi/config" ) -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 -} - 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], @@ -59,16 +52,10 @@ func Init(opts []Options) error { // 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) diff --git a/src/webapi/webapi.go b/src/webapi/webapi.go index 4c84f549..28312578 100644 --- a/src/webapi/webapi.go +++ b/src/webapi/webapi.go @@ -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 }