daemon: use lazyregexp to compile regexes on first use

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-07-15 16:24:29 +02:00
parent d61a6924d7
commit bc1dbd9ea6
4 changed files with 20 additions and 17 deletions

View File

@@ -2,26 +2,25 @@ package testutils // import "github.com/docker/docker/daemon/events/testutils"
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/docker/docker/api/types/events"
timetypes "github.com/docker/docker/api/types/time"
"github.com/docker/docker/internal/lazyregexp"
)
var (
const (
reTimestamp = `(?P<timestamp>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{9}(:?(:?(:?-|\+)\d{2}:\d{2})|Z))`
reEventType = `(?P<eventType>\w+)`
reAction = `(?P<action>\w+)`
reID = `(?P<id>[^\s]+)`
reAttributes = `(\s\((?P<attributes>[^\)]+)\))?`
reString = fmt.Sprintf(`\A%s\s%s\s%s\s%s%s\z`, reTimestamp, reEventType, reAction, reID, reAttributes)
// eventCliRegexp is a regular expression that matches all possible event outputs in the cli
eventCliRegexp = regexp.MustCompile(reString)
)
// eventCliRegexp is a regular expression that matches all possible event outputs in the cli
var eventCliRegexp = lazyregexp.New(fmt.Sprintf(`\A%s\s%s\s%s\s%s%s\z`, reTimestamp, reEventType, reAction, reID, reAttributes))
// ScanMap turns an event string like the default ones formatted in the cli output
// and turns it into map.
func ScanMap(text string) map[string]string {

View File

@@ -15,13 +15,13 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"testing"
"time"
"code.cloudfoundry.org/clock"
"github.com/coreos/go-systemd/v22/journal"
"github.com/docker/docker/internal/lazyregexp"
"github.com/google/uuid"
"gotest.tools/v3/assert"
@@ -109,7 +109,7 @@ func NewT(t *testing.T, outpath string) *Sender {
return s
}
var validVarName = regexp.MustCompile("^[A-Z0-9][A-Z0-9_]*$")
var validVarName = lazyregexp.New("^[A-Z0-9][A-Z0-9_]*$")
// Send is a drop-in replacement for
// github.com/coreos/go-systemd/v22/journal.Send.

View File

@@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/daemon"
import (
"context"
"regexp"
"strconv"
"time"
@@ -15,6 +14,7 @@ import (
timetypes "github.com/docker/docker/api/types/time"
networkSettings "github.com/docker/docker/daemon/network"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/internal/lazyregexp"
"github.com/docker/docker/libnetwork"
"github.com/pkg/errors"
)
@@ -136,6 +136,8 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte
return rep
}
var networkIsInUse = lazyregexp.New(`network ([[:alnum:]]+) is in use`)
// clusterNetworksPrune removes unused cluster networks
func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters filters.Args) (*network.PruneReport, error) {
rep := &network.PruneReport{}
@@ -152,7 +154,7 @@ func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters fil
if err != nil {
return rep, err
}
networkIsInUse := regexp.MustCompile(`network ([[:alnum:]]+) is in use`)
for _, nw := range networks {
select {
case <-ctx.Done():

View File

@@ -7,24 +7,26 @@ import (
"context"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/internal/lazyregexp"
libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
"github.com/pkg/errors"
)
// NOTE: \\s does not detect unicode whitespaces.
// So we use fieldsASCII instead of strings.Fields in parsePSOutput.
// See https://github.com/docker/docker/pull/24358
//
//nolint:gosimple
var psArgsRegexp = lazyregexp.New("\\s+([^\\s]*)=\\s*(PID[^\\s]*)")
func validatePSArgs(psArgs string) error {
// NOTE: \\s does not detect unicode whitespaces.
// So we use fieldsASCII instead of strings.Fields in parsePSOutput.
// See https://github.com/docker/docker/pull/24358
//nolint: gosimple
re := regexp.MustCompile("\\s+([^\\s]*)=\\s*(PID[^\\s]*)")
for _, group := range re.FindAllStringSubmatch(psArgs, -1) {
for _, group := range psArgsRegexp.FindAllStringSubmatch(psArgs, -1) {
if len(group) >= 3 {
k := group[1]
v := group[2]