Skip to content

Commit e5e8395

Browse files
committed
Pre-compile regexes in parseClientListString and parseSentinelMasterString
1 parent b086682 commit e5e8395

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

exporter/clients.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
log "github.com/sirupsen/logrus"
1212
)
1313

14+
var reClientListId = regexp.MustCompile(`^id=\d+ addr=\S+`)
15+
1416
type ClientInfo struct {
1517
Id,
1618
Name,
@@ -41,7 +43,7 @@ id=14 addr=127.0.0.1:64958 fd=9 name= age=5 idle=0 flags=N db=0 sub=0 psub=0 mul
4143
id=40253233 addr=fd40:1481:21:dbe0:7021:300:a03:1a06:44426 fd=19 name= age=782 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 ow=0 owmem=0 events=r cmd=client user=default lib-name=redis-py lib-ver=5.0.1 numops=9
4244
*/
4345
func parseClientListString(clientInfo string) (*ClientInfo, bool) {
44-
if matched, _ := regexp.MatchString(`^id=\d+ addr=\S+`, clientInfo); !matched {
46+
if !reClientListId.MatchString(clientInfo) {
4547
return nil, false
4648
}
4749
connectedClient := ClientInfo{}

exporter/sentinels.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
log "github.com/sirupsen/logrus"
1111
)
1212

13+
var reSentinelMaster = regexp.MustCompile(`^master\d+`)
14+
1315
func (e *Exporter) handleMetricsSentinel(ch chan<- prometheus.Metric, fieldKey string, fieldValue string) {
1416
switch fieldKey {
1517
case
@@ -197,7 +199,7 @@ valid examples:
197199
*/
198200
func parseSentinelMasterString(master string, masterInfo string) (masterName string, masterStatus string, masterAddr string, masterSlaves float64, masterSentinels float64, ok bool) {
199201
ok = false
200-
if matched, _ := regexp.MatchString(`^master\d+`, master); !matched {
202+
if !reSentinelMaster.MatchString(master) {
201203
return
202204
}
203205
matchedMasterInfo := make(map[string]string)

0 commit comments

Comments
 (0)