Skip to content

Commit a9be4ef

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Fix incorrect pgsql conn builder behavior (go-gitea#28085) Fix system config cache expiration timing (go-gitea#28072)
2 parents 468b192 + 17d246c commit a9be4ef

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

models/system/setting.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,26 @@ func (d *dbConfigCachedGetter) GetValue(ctx context.Context, key string) (v stri
115115

116116
func (d *dbConfigCachedGetter) GetRevision(ctx context.Context) int {
117117
d.mu.RLock()
118-
defer d.mu.RUnlock()
119-
if time.Since(d.cacheTime) < time.Second {
120-
return d.revision
118+
cachedDuration := time.Since(d.cacheTime)
119+
cachedRevision := d.revision
120+
d.mu.RUnlock()
121+
122+
if cachedDuration < time.Second {
123+
return cachedRevision
121124
}
125+
126+
d.mu.Lock()
127+
defer d.mu.Unlock()
122128
if GetRevision(ctx) != d.revision {
123-
d.mu.RUnlock()
124-
d.mu.Lock()
125129
rev, set, err := GetAllSettings(ctx)
126130
if err != nil {
127131
log.Error("Unable to get all settings: %v", err)
128132
} else {
129-
d.cacheTime = time.Now()
130133
d.revision = rev
131134
d.settings = set
132135
}
133-
d.mu.Unlock()
134-
d.mu.RLock()
135136
}
137+
d.cacheTime = time.Now()
136138
return d.revision
137139
}
138140

modules/setting/database.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func DBConnStr() (string, error) {
109109
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
110110
Database.User, Database.Passwd, connType, Database.Host, Database.Name, paramSep, Database.MysqlCharset, tls)
111111
case "postgres":
112-
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, paramSep, Database.SSLMode)
112+
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, Database.SSLMode)
113113
case "mssql":
114114
host, port := ParseMSSQLHostPort(Database.Host)
115115
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, Database.Name, Database.User, Database.Passwd)
@@ -157,7 +157,8 @@ func parsePostgreSQLHostPort(info string) (host, port string) {
157157
return host, port
158158
}
159159

160-
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbParam, dbsslMode string) (connStr string) {
160+
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbsslMode string) (connStr string) {
161+
dbName, dbParam, _ := strings.Cut(dbName, "?")
161162
host, port := parsePostgreSQLHostPort(dbHost)
162163
connURL := url.URL{
163164
Scheme: "postgres",

modules/setting/database_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,39 @@ func Test_parsePostgreSQLHostPort(t *testing.T) {
5959
func Test_getPostgreSQLConnectionString(t *testing.T) {
6060
tests := []struct {
6161
Host string
62-
Port string
6362
User string
6463
Passwd string
6564
Name string
66-
Param string
6765
SSLMode string
6866
Output string
6967
}{
7068
{
7169
Host: "/tmp/pg.sock",
72-
Port: "4321",
7370
User: "testuser",
7471
Passwd: "space space !#$%^^%^```-=?=",
7572
Name: "gitea",
76-
Param: "",
7773
SSLMode: "false",
7874
Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/gitea?host=%2Ftmp%2Fpg.sock&sslmode=false",
7975
},
8076
{
8177
Host: "localhost",
82-
Port: "1234",
8378
User: "pgsqlusername",
8479
Passwd: "I love Gitea!",
8580
Name: "gitea",
86-
Param: "",
8781
SSLMode: "true",
8882
Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/gitea?sslmode=true",
8983
},
84+
{
85+
Host: "localhost:1234",
86+
User: "user",
87+
Passwd: "pass",
88+
Name: "gitea?param=1",
89+
Output: "postgres://user:pass@localhost:1234/gitea?param=1&sslmode=",
90+
},
9091
}
9192

9293
for _, test := range tests {
93-
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
94+
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.SSLMode)
9495
assert.Equal(t, test.Output, connStr)
9596
}
9697
}

0 commit comments

Comments
 (0)