Skip to content

Commit

Permalink
replace map[string]string with sync.Map (#363)
Browse files Browse the repository at this point in the history
Signed-off-by: liudui <[email protected]>
  • Loading branch information
xichengliudui authored Jul 1, 2023
1 parent 1475178 commit db3b9dc
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

// nolint: gochecknoinits
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 db3b9dc

Please sign in to comment.