Skip to content

Commit

Permalink
Merge pull request #25 from wzshiming/fix/lock_for_getTypeByUrl
Browse files Browse the repository at this point in the history
Fix lock for getTypeByUrl
  • Loading branch information
AkihiroSuda authored Mar 18, 2021
2 parents 63e572d + ca4a32a commit 5e43fb8
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
mu sync.Mutex
mu sync.RWMutex
registry = make(map[reflect.Type]string)
)

Expand Down Expand Up @@ -65,9 +65,9 @@ func Register(v interface{}, args ...string) {

// TypeURL returns the type url for a registered type.
func TypeURL(v interface{}) (string, error) {
mu.Lock()
mu.RLock()
u, ok := registry[tryDereference(v)]
mu.Unlock()
mu.RUnlock()
if !ok {
// fallback to the proto registry if it is a proto message
pb, ok := v.(proto.Message)
Expand Down Expand Up @@ -182,13 +182,16 @@ type urlType struct {
}

func getTypeByUrl(url string) (urlType, error) {
mu.RLock()
for t, u := range registry {
if u == url {
mu.RUnlock()
return urlType{
t: t,
}, nil
}
}
mu.RUnlock()
// fallback to proto registry
t := proto.MessageType(url)
if t != nil {
Expand Down

0 comments on commit 5e43fb8

Please sign in to comment.