mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-11 04:05:10 +02:00
feat: use XORM EngineGroup instead of single Engine connection (#7212)
Resolves #7207 Add new configuration to make XORM work with a main and replicas database instances. The follow configuration parameters were added: - `HOST_PRIMARY` - `HOST_REPLICAS` - `LOAD_BALANCE_POLICY`. Options: - `"WeightRandom"` -> `xorm.WeightRandomPolicy` - `"WeightRoundRobin` -> `WeightRoundRobinPolicy` - `"LeastCon"` -> `LeastConnPolicy` - `"RoundRobin"` -> `xorm.RoundRobinPolicy()` - default: `xorm.RandomPolicy()` - `LOAD_BALANCE_WEIGHTS` Co-authored-by: pat-s <patrick.schratz@gmail.com@> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7212 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: pat-s <patrick.schratz@gmail.com> Co-committed-by: pat-s <patrick.schratz@gmail.com>
This commit is contained in:
parent
a23d0453a3
commit
63a80bf2b9
19 changed files with 463 additions and 129 deletions
|
@ -4,6 +4,7 @@
|
|||
package setting
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -107,3 +108,104 @@ func Test_getPostgreSQLConnectionString(t *testing.T) {
|
|||
assert.Equal(t, test.Output, connStr)
|
||||
}
|
||||
}
|
||||
|
||||
func getPostgreSQLEngineGroupConnectionStrings(primaryHost, replicaHosts, user, passwd, name, sslmode string) (string, []string) {
|
||||
// Determine the primary connection string.
|
||||
primary := primaryHost
|
||||
if strings.TrimSpace(primary) == "" {
|
||||
primary = "127.0.0.1:5432"
|
||||
}
|
||||
primaryConn := getPostgreSQLConnectionString(primary, user, passwd, name, sslmode)
|
||||
|
||||
// Build the replica connection strings.
|
||||
replicaConns := []string{}
|
||||
if strings.TrimSpace(replicaHosts) != "" {
|
||||
// Split comma-separated replica host values.
|
||||
hosts := strings.Split(replicaHosts, ",")
|
||||
for _, h := range hosts {
|
||||
trimmed := strings.TrimSpace(h)
|
||||
if trimmed != "" {
|
||||
replicaConns = append(replicaConns,
|
||||
getPostgreSQLConnectionString(trimmed, user, passwd, name, sslmode))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return primaryConn, replicaConns
|
||||
}
|
||||
|
||||
func Test_getPostgreSQLEngineGroupConnectionStrings(t *testing.T) {
|
||||
tests := []struct {
|
||||
primaryHost string // primary host setting (e.g. "localhost" or "[::1]:1234")
|
||||
replicaHosts string // comma-separated replica hosts (e.g. "replica1,replica2:2345")
|
||||
user string
|
||||
passwd string
|
||||
name string
|
||||
sslmode string
|
||||
outputPrimary string
|
||||
outputReplicas []string
|
||||
}{
|
||||
{
|
||||
// No primary override (empty => default) and no replicas.
|
||||
primaryHost: "",
|
||||
replicaHosts: "",
|
||||
user: "",
|
||||
passwd: "",
|
||||
name: "",
|
||||
sslmode: "",
|
||||
outputPrimary: "postgres://:@127.0.0.1:5432?sslmode=",
|
||||
outputReplicas: []string{},
|
||||
},
|
||||
{
|
||||
// Primary set and one replica.
|
||||
primaryHost: "localhost",
|
||||
replicaHosts: "replicahost",
|
||||
user: "user",
|
||||
passwd: "pass",
|
||||
name: "gitea",
|
||||
sslmode: "disable",
|
||||
outputPrimary: "postgres://user:pass@localhost:5432/gitea?sslmode=disable",
|
||||
outputReplicas: []string{"postgres://user:pass@replicahost:5432/gitea?sslmode=disable"},
|
||||
},
|
||||
{
|
||||
// Primary with explicit port; multiple replicas (one without and one with an explicit port).
|
||||
primaryHost: "localhost:5433",
|
||||
replicaHosts: "replica1,replica2:5434",
|
||||
user: "test",
|
||||
passwd: "secret",
|
||||
name: "db",
|
||||
sslmode: "require",
|
||||
outputPrimary: "postgres://test:secret@localhost:5433/db?sslmode=require",
|
||||
outputReplicas: []string{
|
||||
"postgres://test:secret@replica1:5432/db?sslmode=require",
|
||||
"postgres://test:secret@replica2:5434/db?sslmode=require",
|
||||
},
|
||||
},
|
||||
{
|
||||
// IPv6 addresses for primary and replica.
|
||||
primaryHost: "[::1]:1234",
|
||||
replicaHosts: "[::2]:2345",
|
||||
user: "ipv6",
|
||||
passwd: "ipv6pass",
|
||||
name: "ipv6db",
|
||||
sslmode: "disable",
|
||||
outputPrimary: "postgres://ipv6:ipv6pass@[::1]:1234/ipv6db?sslmode=disable",
|
||||
outputReplicas: []string{
|
||||
"postgres://ipv6:ipv6pass@[::2]:2345/ipv6db?sslmode=disable",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
primary, replicas := getPostgreSQLEngineGroupConnectionStrings(
|
||||
test.primaryHost,
|
||||
test.replicaHosts,
|
||||
test.user,
|
||||
test.passwd,
|
||||
test.name,
|
||||
test.sslmode,
|
||||
)
|
||||
assert.Equal(t, test.outputPrimary, primary)
|
||||
assert.Equal(t, test.outputReplicas, replicas)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue