From 4e70e48d302d31d4e9c2a3b4a083a995300bd5d5 Mon Sep 17 00:00:00 2001 From: liudui Date: Wed, 28 Jun 2023 11:23:37 +0800 Subject: [PATCH] replace map[string]string with sync.Map Signed-off-by: liudui --- .../metaprotocol/application_protocol.go | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/pkg/model/metaprotocol/application_protocol.go b/pkg/model/metaprotocol/application_protocol.go index 808abb401..0e7e5445d 100644 --- a/pkg/model/metaprotocol/application_protocol.go +++ b/pkg/model/metaprotocol/application_protocol.go @@ -20,28 +20,26 @@ import ( "sync" ) -var lock sync.Mutex -var applicationProtocols = map[string]string{ - "dubbo": "aeraki.meta_protocol.codec.dubbo", - "thrift": "aeraki.meta_protocol.codec.thrift", +var applicationProtocols sync.Map + +func init() { + applicationProtocols.Store("dubbo", "aeraki.meta_protocol.codec.dubbo") + applicationProtocols.Store("thrift", "aeraki.meta_protocol.codec.thrift") } // SetApplicationProtocolCodec sets the codec for a specific protocol func SetApplicationProtocolCodec(protocol, codec string) { - lock.Lock() - defer lock.Unlock() - applicationProtocols[protocol] = codec + applicationProtocols.Store(protocol, codec) } // GetApplicationProtocolCodec get the codec for a specific protocol func GetApplicationProtocolCodec(protocol string) (string, error) { - lock.Lock() - defer lock.Unlock() - codec := applicationProtocols[protocol] - if codec != "" { - return codec, nil + codec, ok := applicationProtocols.Load(protocol) + if !ok { + return "", fmt.Errorf("can't find codec for protocol: %s", protocol) + } - return "", fmt.Errorf("can't find codec for protocol: %s", protocol) + return fmt.Sprintf("%s", codec), nil } // GetApplicationProtocolFromPortName extracts the application protocol name from metaprotocol port name