Skip to content

Commit

Permalink
perf: support configrue es and http connections
Browse files Browse the repository at this point in the history
Signed-off-by: yangk <[email protected]>
  • Loading branch information
yangkaa authored and DokiDoki1103 committed Jul 26, 2024
1 parent 47f8782 commit 4bf94da
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
34 changes: 33 additions & 1 deletion db/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package mysql

import (
"os"
"strconv"
"sync"
"time"

"github.com/goodrain/rainbond/db/config"
"github.com/goodrain/rainbond/db/model"
Expand Down Expand Up @@ -52,7 +54,37 @@ func CreateManager(config config.Config) (*Manager, error) {
if err != nil {
return nil, err
}

// 获取底层的 sql.DB 对象
sqlDB := db.DB()
if err != nil {
logrus.Errorf("failed to get sql.DB from gorm.DB: %v", err)
return nil, err
}
maxOpenConns := 2500
maxIdleConns := 500
maxLifeTime := 5
if os.Getenv("DB_MAX_OPEN_CONNS") != "" {
openCon, err := strconv.Atoi(os.Getenv("DB_MAX_OPEN_CONNS"))
if err == nil {
maxOpenConns = openCon
}
}
if os.Getenv("DB_MAX_IDLE_CONNS") != "" {
idleCon, err := strconv.Atoi(os.Getenv("DB_MAX_IDLE_CONNS"))
if err == nil {
maxIdleConns = idleCon
}
}
if os.Getenv("DB_CONN_MAX_LIFE_TIME") != "" {
lifeTime, err := strconv.Atoi(os.Getenv("DB_CONN_MAX_LIFE_TIME"))
if err == nil {
maxLifeTime = lifeTime
}
}
// 配置连接池参数
sqlDB.SetMaxOpenConns(maxOpenConns) // 设置最大打开连接数
sqlDB.SetMaxIdleConns(maxIdleConns) // 设置最大空闲连接数
sqlDB.SetConnMaxLifetime(time.Duration(maxLifeTime) * time.Minute) //
}
if config.DBType == "cockroachdb" {
var err error
Expand Down
43 changes: 33 additions & 10 deletions pkg/component/es/esComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/sirupsen/logrus"
"io"
"net/http"
"os"
"strconv"
"time"
)

var defaultEsComponent *Component
Expand All @@ -20,27 +23,47 @@ type Component struct {
client *http.Client
}

// createHTTPClient - 创建带有连接池配置的 HTTP 客户端
func createHTTPClient() *http.Client {
maxIdleConns := 100
if os.Getenv("HTTP_MAX_IDLE_CONNS") != "" {
idleCon, err := strconv.Atoi(os.Getenv("HTTP_MAX_IDLE_CONNS"))
if err == nil {
maxIdleConns = idleCon
}
}
maxIdleConnsPerHost := 100
if os.Getenv("HTTP_MAX_IDLE_CONNS_PER_HOST") != "" {
idleCon, err := strconv.Atoi(os.Getenv("HTTP_MAX_IDLE_CONNS_PER_HOST"))
if err == nil {
maxIdleConnsPerHost = idleCon
}
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
MaxIdleConns: maxIdleConns, // 设置最大空闲连接数
MaxIdleConnsPerHost: maxIdleConnsPerHost, // 设置每个主机的最大空闲连接数
IdleConnTimeout: 90 * time.Second, // 设置空闲连接的超时时间
TLSHandshakeTimeout: 10 * time.Second, // 设置 TLS 握手超时时间
ExpectContinueTimeout: 1 * time.Second, // 设置 100-continue 状态码超时时间
}
client := &http.Client{Transport: tr}
return client
}

func (c *Component) Start(ctx context.Context, cfg *configs.Config) error {
c.url = cfg.APIConfig.ElasticSearchURL
c.username = cfg.APIConfig.ElasticSearchUsername
c.password = cfg.APIConfig.ElasticSearchPassword
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
c.client = client
c.client = createHTTPClient()
return nil
}

func (c *Component) SingleStart(url, username, password string) {
c.url = url
c.username = username
c.password = password
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
c.client = client
c.client = createHTTPClient()
}

func (c *Component) CloseHandle() {
Expand Down

0 comments on commit 4bf94da

Please sign in to comment.