瀏覽代碼

Merge pull request #1632 from slingamn/mysql_safety

fix #1622
tags/v2.7.0-rc1
Shivaram Lingamneni 3 年之前
父節點
當前提交
317720bfc8
沒有連結到貢獻者的電子郵件帳戶。
共有 5 個檔案被更改,包括 23 行新增0 行删除
  1. 3
    0
      default.yaml
  2. 7
    0
      irc/config.go
  3. 2
    0
      irc/mysql/config.go
  4. 8
    0
      irc/mysql/history.go
  5. 3
    0
      traditional.yaml

+ 3
- 0
default.yaml 查看文件

@@ -748,6 +748,9 @@ datastore:
748 748
         password: "hunter2"
749 749
         history-database: "oragono_history"
750 750
         timeout: 3s
751
+        max-conns: 4
752
+        # this may be necessary to prevent middleware from closing your connections:
753
+        #conn-max-lifetime: 180s
751 754
 
752 755
 # languages config
753 756
 languages:

+ 7
- 0
irc/config.go 查看文件

@@ -18,6 +18,7 @@ import (
18 18
 	"path/filepath"
19 19
 	"reflect"
20 20
 	"regexp"
21
+	"runtime"
21 22
 	"strconv"
22 23
 	"strings"
23 24
 	"time"
@@ -1481,6 +1482,12 @@ func LoadConfig(filename string) (config *Config, err error) {
1481 1482
 
1482 1483
 	config.Datastore.MySQL.ExpireTime = time.Duration(config.History.Restrictions.ExpireTime)
1483 1484
 	config.Datastore.MySQL.TrackAccountMessages = config.History.Retention.EnableAccountIndexing
1485
+	if config.Datastore.MySQL.MaxConns == 0 {
1486
+		// #1622: not putting an upper limit on the number of MySQL connections is
1487
+		// potentially dangerous. as a naive heuristic, assume they're running on the
1488
+		// same machine:
1489
+		config.Datastore.MySQL.MaxConns = runtime.NumCPU()
1490
+	}
1484 1491
 
1485 1492
 	config.Server.Cloaks.Initialize()
1486 1493
 	if config.Server.Cloaks.Enabled {

+ 2
- 0
irc/mysql/config.go 查看文件

@@ -17,6 +17,8 @@ type Config struct {
17 17
 	Password        string
18 18
 	HistoryDatabase string `yaml:"history-database"`
19 19
 	Timeout         time.Duration
20
+	MaxConns        int           `yaml:"max-conns"`
21
+	ConnMaxLifetime time.Duration `yaml:"conn-max-lifetime"`
20 22
 
21 23
 	// XXX these are copied from elsewhere in the config:
22 24
 	ExpireTime           time.Duration

+ 8
- 0
irc/mysql/history.go 查看文件

@@ -100,6 +100,14 @@ func (m *MySQL) Open() (err error) {
100 100
 		return err
101 101
 	}
102 102
 
103
+	if m.config.MaxConns != 0 {
104
+		m.db.SetMaxOpenConns(m.config.MaxConns)
105
+		m.db.SetMaxIdleConns(m.config.MaxConns)
106
+	}
107
+	if m.config.ConnMaxLifetime != 0 {
108
+		m.db.SetConnMaxLifetime(m.config.ConnMaxLifetime)
109
+	}
110
+
103 111
 	err = m.fixSchemas()
104 112
 	if err != nil {
105 113
 		return err

+ 3
- 0
traditional.yaml 查看文件

@@ -721,6 +721,9 @@ datastore:
721 721
         password: "hunter2"
722 722
         history-database: "oragono_history"
723 723
         timeout: 3s
724
+        max-conns: 4
725
+        # this may be necessary to prevent middleware from closing your connections:
726
+        #conn-max-lifetime: 180s
724 727
 
725 728
 # languages config
726 729
 languages:

Loading…
取消
儲存