Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace map[string]string with sync.Map #363

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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