s3tables: optimize regex usage and improve version token uniqueness
- Pre-compile regex patterns as package-level variables to avoid re-compilation overhead on every call - Add a random component to version token generation to reduce collision probability under high concurrency
This commit is contained in:
@@ -2,6 +2,7 @@ package s3tables
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -9,13 +10,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bucketARNPattern = regexp.MustCompile(`^arn:aws:s3tables:[^:]*:[^:]*:bucket/([a-z0-9_-]+)$`)
|
||||||
|
tableARNPattern = regexp.MustCompile(`^arn:aws:s3tables:[^:]*:[^:]*:bucket/([a-z0-9_-]+)/table/([^/]+)/([a-z0-9_]+)$`)
|
||||||
|
bucketNamePattern = regexp.MustCompile(`^[a-z0-9_-]+$`)
|
||||||
|
)
|
||||||
|
|
||||||
// ARN parsing functions
|
// ARN parsing functions
|
||||||
|
|
||||||
// parseBucketNameFromARN extracts bucket name from table bucket ARN
|
// parseBucketNameFromARN extracts bucket name from table bucket ARN
|
||||||
// ARN format: arn:aws:s3tables:{region}:{account}:bucket/{bucket-name}
|
// ARN format: arn:aws:s3tables:{region}:{account}:bucket/{bucket-name}
|
||||||
func parseBucketNameFromARN(arn string) (string, error) {
|
func parseBucketNameFromARN(arn string) (string, error) {
|
||||||
pattern := regexp.MustCompile(`^arn:aws:s3tables:[^:]*:[^:]*:bucket/([a-z0-9_-]+)$`)
|
matches := bucketARNPattern.FindStringSubmatch(arn)
|
||||||
matches := pattern.FindStringSubmatch(arn)
|
|
||||||
if len(matches) != 2 {
|
if len(matches) != 2 {
|
||||||
return "", fmt.Errorf("invalid bucket ARN: %s", arn)
|
return "", fmt.Errorf("invalid bucket ARN: %s", arn)
|
||||||
}
|
}
|
||||||
@@ -26,8 +32,7 @@ func parseBucketNameFromARN(arn string) (string, error) {
|
|||||||
// ARN format: arn:aws:s3tables:{region}:{account}:bucket/{bucket-name}/table/{namespace}/{table-name}
|
// ARN format: arn:aws:s3tables:{region}:{account}:bucket/{bucket-name}/table/{namespace}/{table-name}
|
||||||
func parseTableFromARN(arn string) (bucketName, namespace, tableName string, err error) {
|
func parseTableFromARN(arn string) (bucketName, namespace, tableName string, err error) {
|
||||||
// Updated regex to align with namespace validation (single-segment)
|
// Updated regex to align with namespace validation (single-segment)
|
||||||
pattern := regexp.MustCompile(`^arn:aws:s3tables:[^:]*:[^:]*:bucket/([a-z0-9_-]+)/table/([^/]+)/([a-z0-9_]+)$`)
|
matches := tableARNPattern.FindStringSubmatch(arn)
|
||||||
matches := pattern.FindStringSubmatch(arn)
|
|
||||||
if len(matches) != 4 {
|
if len(matches) != 4 {
|
||||||
return "", "", "", fmt.Errorf("invalid table ARN: %s", arn)
|
return "", "", "", fmt.Errorf("invalid table ARN: %s", arn)
|
||||||
}
|
}
|
||||||
@@ -92,13 +97,12 @@ type tableMetadataInternal struct {
|
|||||||
// isValidBucketName validates bucket name characters
|
// isValidBucketName validates bucket name characters
|
||||||
// Bucket names must contain only lowercase letters, numbers, hyphens, and underscores
|
// Bucket names must contain only lowercase letters, numbers, hyphens, and underscores
|
||||||
func isValidBucketName(name string) bool {
|
func isValidBucketName(name string) bool {
|
||||||
pattern := regexp.MustCompile(`^[a-z0-9_-]+$`)
|
return bucketNamePattern.MatchString(name)
|
||||||
return pattern.MatchString(name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateVersionToken generates a unique version token
|
// generateVersionToken generates a unique version token
|
||||||
func generateVersionToken() string {
|
func generateVersionToken() string {
|
||||||
return fmt.Sprintf("%d", time.Now().UnixNano())
|
return fmt.Sprintf("%d_%d", time.Now().UnixNano(), rand.Int63())
|
||||||
}
|
}
|
||||||
|
|
||||||
// splitPath splits a path into directory and name components using stdlib
|
// splitPath splits a path into directory and name components using stdlib
|
||||||
|
|||||||
Reference in New Issue
Block a user