Cache of the valid channel name regex
* Added a Benchmark to verify the performance * Now it is 10 times faster
This commit is contained in:
@@ -7,7 +7,7 @@ run-debug: debug
|
|||||||
${GOPATH}/bin/ipe --config ${GOPATH}/src/github.com/dimiro1/ipe/config.json -logtostderr=true -v=2
|
${GOPATH}/bin/ipe --config ${GOPATH}/src/github.com/dimiro1/ipe/config.json -logtostderr=true -v=2
|
||||||
|
|
||||||
test:
|
test:
|
||||||
GO15VENDOREXPERIMENT=1 go test `go list ./... | grep -v vendor`
|
GO15VENDOREXPERIMENT=1 go test -bench . `go list ./... | grep -v vendor | grep -v functional | grep -v github.com/dimiro1/ipe$$`
|
||||||
|
|
||||||
dev-deps:
|
dev-deps:
|
||||||
go get github.com/pusher/pusher-http-go
|
go get github.com/pusher/pusher-http-go
|
||||||
|
|||||||
+8
-10
@@ -14,6 +14,12 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var validChannelName *regexp.Regexp
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
validChannelName = regexp.MustCompile("^[A-Za-z0-9_\\-=@,.;]+$")
|
||||||
|
}
|
||||||
|
|
||||||
// HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result
|
// HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result
|
||||||
func HashMAC(message, key []byte) string {
|
func HashMAC(message, key []byte) string {
|
||||||
mac := hmac.New(sha256.New, key)
|
mac := hmac.New(sha256.New, key)
|
||||||
@@ -25,18 +31,10 @@ func HashMAC(message, key []byte) string {
|
|||||||
|
|
||||||
// GenerateSessionID Generate a new random Hash
|
// GenerateSessionID Generate a new random Hash
|
||||||
func GenerateSessionID() string {
|
func GenerateSessionID() string {
|
||||||
MAX := math.MaxInt64
|
return fmt.Sprintf("%d.%d", rand.Intn(math.MaxInt64), rand.Intn(math.MaxInt64))
|
||||||
|
|
||||||
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsChannelNameValid Verify if the channel name is valid
|
// IsChannelNameValid Verify if the channel name is valid
|
||||||
func IsChannelNameValid(channelName string) bool {
|
func IsChannelNameValid(channelName string) bool {
|
||||||
matched, err := regexp.MatchString("^[A-Za-z0-9_\\-=@,.;]+$", channelName)
|
return validChannelName.Match([]byte(channelName))
|
||||||
|
|
||||||
if err == nil && matched {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,18 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func BenchmarkGenerateSession(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
GenerateSessionID()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkIsChannelNameValid(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
IsChannelNameValid("hello-world")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGenerateSession(t *testing.T) {
|
func TestGenerateSession(t *testing.T) {
|
||||||
sessionID := GenerateSessionID()
|
sessionID := GenerateSessionID()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user