Skip to content

Commit

Permalink
replace map[string]string with sync.Map
Browse files Browse the repository at this point in the history
Signed-off-by: liudui <liuduidui@beyondcent.com>
  • Loading branch information
xichengliudui committed Jun 28, 2023
1 parent 1475178 commit 4e70e48
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions pkg/model/metaprotocol/application_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e70e48

Please sign in to comment.