Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Refactor session#domainMap use RWMutex to replace Mutex #33739

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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