network: Optimize IsTLSEnabled() #1054

Merged
fyrchik merged 1 commit from fyrchik/frostfs-node:fix-network into master 2024-03-22 13:55:08 +00:00
2 changed files with 17 additions and 7 deletions
Showing only changes of commit c7a12ca3d8 - Show all commits

View file

@ -13,11 +13,6 @@ var tls, _ = multiaddr.NewMultiaddr("/" + tlsProtocolName)
// IsTLSEnabled searches for wrapped TLS protocol in multiaddr.
func (a Address) IsTLSEnabled() bool {
for _, protoc := range a.ma.Protocols() {
if protoc.Code == multiaddr.P_TLS {
return true
}
}
return false
_, err := a.ma.ValueForProtocol(multiaddr.P_TLS)
return err == nil
}

View file

@ -27,3 +27,18 @@ func TestAddress_TLSEnabled(t *testing.T) {
require.Equal(t, test.wantTLS, addr.IsTLSEnabled(), test.input)
}
}
func BenchmarkAddressTLSEnabled(b *testing.B) {
var addr Address
err := addr.FromString("/dns4/localhost/tcp/8080/tls")
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
var enabled bool
for i := 0; i < b.N; i++ {
enabled = addr.IsTLSEnabled()
}
require.True(b, enabled)
}