Skip to content

Commit e64c2dd

Browse files
authored
Merge pull request #54 from aliyun/feature/register_holo
perf: clean old hologres instance when registering
2 parents b329179 + d3d1ed3 commit e64c2dd

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

datasource/hologres/hologres.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"database/sql/driver"
66
"fmt"
7+
"sync"
78
"time"
89

910
"github.com/lib/pq"
@@ -31,19 +32,26 @@ func (d HologresDriver) Open(name string) (driver.Conn, error) {
3132
}
3233

3334
type Hologres struct {
34-
DSN string
35-
DB *sql.DB
36-
Name string
35+
DSN string
36+
DB *sql.DB
37+
Name string
38+
RegisterTime time.Time
3739
}
3840

39-
var hologresInstances = make(map[string]*Hologres)
41+
var hologresInstances sync.Map
4042

4143
func GetHologres(name string) (*Hologres, error) {
42-
if _, ok := hologresInstances[name]; !ok {
44+
value, ok := hologresInstances.Load(name)
45+
if !ok {
46+
return nil, fmt.Errorf("Hologres not found, name:%s", name)
47+
}
48+
49+
hologresInstance, ok := value.(*Hologres)
50+
if !ok {
4351
return nil, fmt.Errorf("Hologres not found, name:%s", name)
4452
}
4553

46-
return hologresInstances[name], nil
54+
return hologresInstance, nil
4755
}
4856
func (m *Hologres) Init() error {
4957
db, err := sql.Open("hologres", m.DSN)
@@ -61,25 +69,37 @@ func (m *Hologres) Init() error {
6169
return err
6270
}
6371

64-
func RegisterHologres(name, dsn string) {
65-
if _, ok := hologresInstances[name]; ok {
66-
return
72+
func RegisterHologres(name, dsn string, useCustomAuth bool) {
73+
value, ok := hologresInstances.Load(name)
74+
if ok {
75+
if useCustomAuth {
76+
return
77+
}
78+
hologresInstance, ok2 := value.(*Hologres)
79+
if ok2 && time.Since(hologresInstance.RegisterTime) < 12*time.Hour {
80+
return
81+
}
6782
}
6883
m := &Hologres{
69-
DSN: dsn,
70-
Name: name,
84+
DSN: dsn,
85+
Name: name,
86+
RegisterTime: time.Now(),
7187
}
7288
err := m.Init()
7389
if err != nil {
7490
fmt.Printf("event=RegisterHologres\tdsn=%s\tname=%s", dsn, name)
7591
panic(err)
7692
}
77-
hologresInstances[name] = m
93+
hologresInstances.Store(name, m)
7894

7995
}
8096

8197
func RemoveHologres(name string) {
82-
hologres, ok := hologresInstances[name]
98+
value, ok := hologresInstances.Load(name)
99+
if !ok {
100+
return
101+
}
102+
hologres, ok := value.(*Hologres)
83103
if !ok {
84104
return
85105
}
@@ -88,5 +108,5 @@ func RemoveHologres(name string) {
88108
hologres.DB.Close()
89109
}
90110

91-
delete(hologresInstances, name)
111+
hologresInstances.Delete(name)
92112
}

domain/project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func NewProject(p *api.Project, isInitClient bool) *Project {
3737
}
3838
if isInitClient {
3939
dsn := onlineStore.Datasource.GenerateDSN(constants.Datasource_Type_Hologres)
40-
hologres.RegisterHologres(onlineStore.Name, dsn)
40+
useCustomAuth := onlineStore.Datasource.HologresAuth != ""
41+
hologres.RegisterHologres(onlineStore.Name, dsn, useCustomAuth)
4142
}
4243
project.OnlineStore = onlineStore
4344
case constants.Datasource_Type_IGraph:

0 commit comments

Comments
 (0)