Skip to content

Commit 38ffeed

Browse files
authored
chore: hash connectionArgs when used as cache key (#199)
1 parent bbeee0d commit 38ffeed

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

datasource.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sqlds
22

33
import (
44
"context"
5+
"crypto/sha256"
56
"database/sql"
67
"encoding/json"
78
"errors"
@@ -41,7 +42,8 @@ func defaultKey(datasourceUID string) string {
4142
}
4243

4344
func keyWithConnectionArgs(datasourceUID string, connArgs json.RawMessage) string {
44-
return fmt.Sprintf("%s-%s", datasourceUID, string(connArgs))
45+
connectionArgsHash := sha256.Sum256(connArgs)
46+
return fmt.Sprintf("%s-%x", datasourceUID, connectionArgsHash)
4547
}
4648

4749
type dbConnection struct {

datasource_connect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ func Test_getDBConnectionFromQuery(t *testing.T) {
6464
desc: "it should return the cached connection for the given args",
6565
dsUID: "uid1",
6666
args: "foo",
67-
expectedKey: "uid1-foo",
67+
expectedKey: "uid1-2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
6868
existingDB: db2,
6969
expectedDB: db2,
7070
},
7171
{
7272
desc: "it should create a new connection with the given args",
7373
dsUID: "uid1",
7474
args: "foo",
75-
expectedKey: "uid1-foo",
75+
expectedKey: "uid1-2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
7676
expectedDB: db3,
7777
},
7878
}

0 commit comments

Comments
 (0)