diff --git a/types.go b/types.go index 00662a3..647d419 100644 --- a/types.go +++ b/types.go @@ -28,7 +28,7 @@ import ( ) var ( - mu sync.Mutex + mu sync.RWMutex registry = make(map[reflect.Type]string) ) @@ -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) @@ -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 {