Skip to content

Commit

Permalink
*: Refactor session#domainMap use RWMutex to replace Mutex (#33739)
Browse files Browse the repository at this point in the history
close #33737
  • Loading branch information
likzn committed Apr 6, 2022
1 parent 164261c commit 967d203
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ import (

type domainMap struct {
domains map[string]*domain.Domain
mu sync.Mutex
mu sync.RWMutex
}

func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
dm.mu.Lock()
defer dm.mu.Unlock()
dm.mu.RLock()

// If this is the only domain instance, and the caller doesn't provide store.
if len(dm.domains) == 1 && store == nil {
Expand All @@ -60,6 +59,7 @@ func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {

key := store.UUID()
d = dm.domains[key]
dm.mu.RUnlock()
if d != nil {
return
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
if err != nil {
return nil, err
}
dm.domains[key] = d
dm.Set(store, d)

return
}
Expand All @@ -103,6 +103,12 @@ func (dm *domainMap) Delete(store kv.Storage) {
dm.mu.Unlock()
}

func (dm *domainMap) Set(store kv.Storage, domain *domain.Domain) {
dm.mu.Lock()
dm.domains[store.UUID()] = domain
dm.mu.Unlock()
}

var (
domap = &domainMap{
domains: map[string]*domain.Domain{},
Expand Down

0 comments on commit 967d203

Please sign in to comment.