network: Optimize IsTLSEnabled() #1054
2 changed files with 17 additions and 7 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue