Pārlūkot izejas kodu

Make max database connection count configurable and default to 20

Tulir Asokan 6 gadi atpakaļ
vecāks
revīzija
38540d8efb
3 mainītis faili ar 21 papildinājumiem un 0 dzēšanām
  1. 9 0
      config/config.go
  2. 9 0
      example-config.yaml
  3. 3 0
      main.go

+ 9 - 0
config/config.go

@@ -38,6 +38,9 @@ type Config struct {
 		Database struct {
 		Database struct {
 			Type string `yaml:"type"`
 			Type string `yaml:"type"`
 			URI  string `yaml:"uri"`
 			URI  string `yaml:"uri"`
+
+			MaxOpenConns int `yaml:"max_open_conns"`
+			MaxIdleConns int `yaml:"max_idle_conns"`
 		} `yaml:"database"`
 		} `yaml:"database"`
 
 
 		StateStore string `yaml:"state_store_path"`
 		StateStore string `yaml:"state_store_path"`
@@ -58,6 +61,11 @@ type Config struct {
 	Logging appservice.LogConfig `yaml:"logging"`
 	Logging appservice.LogConfig `yaml:"logging"`
 }
 }
 
 
+func (config *Config) setDefaults() {
+	config.AppService.Database.MaxOpenConns = 20
+	config.AppService.Database.MaxIdleConns = 2
+}
+
 func Load(path string) (*Config, error) {
 func Load(path string) (*Config, error) {
 	data, err := ioutil.ReadFile(path)
 	data, err := ioutil.ReadFile(path)
 	if err != nil {
 	if err != nil {
@@ -65,6 +73,7 @@ func Load(path string) (*Config, error) {
 	}
 	}
 
 
 	var config = &Config{}
 	var config = &Config{}
+	config.setDefaults()
 	err = yaml.Unmarshal(data, config)
 	err = yaml.Unmarshal(data, config)
 	return config, err
 	return config, err
 }
 }

+ 9 - 0
example-config.yaml

@@ -23,6 +23,10 @@ appservice:
         #   SQLite: File name is enough. https://github.com/mattn/go-sqlite3#connection-string
         #   SQLite: File name is enough. https://github.com/mattn/go-sqlite3#connection-string
         #   Postgres: Connection string. For example, postgres://user:password@host/database
         #   Postgres: Connection string. For example, postgres://user:password@host/database
         uri: mautrix-whatsapp.db
         uri: mautrix-whatsapp.db
+        # Maximum number of connections. Mostly relevant for Postgres.
+        max_open_conns: 20
+        max_idle_conns: 2
+
     # Path to the Matrix room state store.
     # Path to the Matrix room state store.
     state_store_path: ./mx-state.json
     state_store_path: ./mx-state.json
 
 
@@ -56,6 +60,11 @@ bridge:
 
 
     # WhatsApp connection timeout in seconds.
     # WhatsApp connection timeout in seconds.
     connection_timeout: 20
     connection_timeout: 20
+    # Maximum number of times to retry connecting on connection error.
+    max_connection_attempts: 3
+    # Whether or not the bridge should send a notice to the user's management room when it retries connecting.
+    # If false, it will only report when it stops retrying.
+    report_connection_retry: true
 
 
     # The prefix for commands. Only required in non-management rooms.
     # The prefix for commands. Only required in non-management rooms.
     command_prefix: "!wa"
     command_prefix: "!wa"

+ 3 - 0
main.go

@@ -139,6 +139,9 @@ func (bridge *Bridge) Init() {
 		os.Exit(14)
 		os.Exit(14)
 	}
 	}
 
 
+	bridge.DB.SetMaxOpenConns(bridge.Config.AppService.Database.MaxOpenConns)
+	bridge.DB.SetMaxIdleConns(bridge.Config.AppService.Database.MaxIdleConns)
+
 	bridge.Log.Debugln("Initializing Matrix event processor")
 	bridge.Log.Debugln("Initializing Matrix event processor")
 	bridge.EventProcessor = appservice.NewEventProcessor(bridge.AS)
 	bridge.EventProcessor = appservice.NewEventProcessor(bridge.AS)
 	bridge.Log.Debugln("Initializing Matrix event handler")
 	bridge.Log.Debugln("Initializing Matrix event handler")