mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-01 04:21:36 +01:00
Improve awsecr parseRegion method.
Signed-off-by: 疯魔慕薇 <kfanjian@gmail.com>
This commit is contained in:
parent
d38a08b15a
commit
55cead910f
@ -31,6 +31,14 @@ import (
|
||||
"github.com/goharbor/harbor/src/replication/model"
|
||||
)
|
||||
|
||||
const (
|
||||
regionPattern = "https://(?:api|\\d+\\.dkr)\\.ecr\\.([\\w\\-]+)\\.amazonaws\\.com"
|
||||
)
|
||||
|
||||
var (
|
||||
regionRegexp = regexp.MustCompile(regionPattern)
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := adp.RegisterFactory(model.RegistryTypeAwsEcr, func(registry *model.Registry) (adp.Adapter, error) {
|
||||
return newAdapter(registry)
|
||||
@ -59,8 +67,7 @@ func newAdapter(registry *model.Registry) (*adapter, error) {
|
||||
}
|
||||
|
||||
func parseRegion(url string) (string, error) {
|
||||
pattern := "https://(?:api|\\d+\\.dkr)\\.ecr\\.([\\w\\-]+)\\.amazonaws\\.com"
|
||||
rs := regexp.MustCompile(pattern).FindStringSubmatch(url)
|
||||
rs := regionRegexp.FindStringSubmatch(url)
|
||||
if rs == nil {
|
||||
return "", errors.New("Bad aws url")
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package awsecr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -251,3 +253,33 @@ func TestAwsAuthCredential_Modify(t *testing.T) {
|
||||
err = a.Modify(req)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
var urlForBenchmark = []string{
|
||||
"https://1234.dkr.ecr.test-region.amazonaws.com/v2/",
|
||||
"https://api.ecr.test-region.amazonaws.com",
|
||||
"https://test-region.amazonaws.com",
|
||||
}
|
||||
|
||||
func compileRegexpEveryTime(url string) (string, error) {
|
||||
rs := regexp.MustCompile(regionPattern).FindStringSubmatch(url)
|
||||
if rs == nil {
|
||||
return "", errors.New("Bad aws url")
|
||||
}
|
||||
return rs[1], nil
|
||||
}
|
||||
|
||||
func BenchmarkGetRegion(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, url := range urlForBenchmark {
|
||||
parseRegion(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCompileRegexpEveryTime(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, url := range urlForBenchmark {
|
||||
compileRegexpEveryTime(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user