perf: slightly optimize config/config.go

- Exclude the struct tag is space
 - Exclude configuration file comment, which is starting with space

fix: in the case of panic unexpected happened in the method parse0 of parser/parser.go, closing of the channel is needed to prevent the program from getting stuck~
  - fine-tune the coding of parser/parser.go

Limited ability, hope to make it better~
This commit is contained in:
tianyi
2022-11-01 21:40:15 +08:00
committed by finley
parent 83f2c5fad4
commit 55fd1d399d
2 changed files with 6 additions and 12 deletions

View File

@@ -50,7 +50,7 @@ func parse(src io.Reader) *ServerProperties {
scanner := bufio.NewScanner(src) scanner := bufio.NewScanner(src)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
if len(line) > 0 && line[0] == '#' { if len(line) > 0 && strings.TrimLeft(line, " ")[0] == '#' {
continue continue
} }
pivot := strings.IndexAny(line, " ") pivot := strings.IndexAny(line, " ")
@@ -72,7 +72,7 @@ func parse(src io.Reader) *ServerProperties {
field := t.Elem().Field(i) field := t.Elem().Field(i)
fieldVal := v.Elem().Field(i) fieldVal := v.Elem().Field(i)
key, ok := field.Tag.Lookup("cfg") key, ok := field.Tag.Lookup("cfg")
if !ok { if !ok || strings.TrimLeft(key, " ") == "" {
key = field.Name key = field.Name
} }
value, ok := rawMap[strings.ToLower(key)] value, ok := rawMap[strings.ToLower(key)]

View File

@@ -76,6 +76,7 @@ func parse0(reader io.Reader, ch chan<- *Payload) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
logger.Error(err, string(debug.Stack())) logger.Error(err, string(debug.Stack()))
close(ch)
} }
}() }()
@@ -88,17 +89,12 @@ func parse0(reader io.Reader, ch chan<- *Payload) {
var ioErr bool var ioErr bool
msg, ioErr, err = readLine(bufReader, &state) msg, ioErr, err = readLine(bufReader, &state)
if err != nil { if err != nil {
ch <- &Payload{Err: err}
if ioErr { // encounter io err, stop read if ioErr { // encounter io err, stop read
ch <- &Payload{
Err: err,
}
close(ch) close(ch)
return return
} }
// protocol err, reset read state // protocol err, reset read state
ch <- &Payload{
Err: err,
}
state = readState{} state = readState{}
continue continue
} }
@@ -227,9 +223,8 @@ func parseMultiBulkHeader(msg []byte, state *readState) error {
state.expectedArgsCount = int(expectedLine) state.expectedArgsCount = int(expectedLine)
state.args = make([][]byte, 0, expectedLine) state.args = make([][]byte, 0, expectedLine)
return nil return nil
} else {
return errors.New("protocol error: " + string(msg))
} }
return errors.New("protocol error: " + string(msg))
} }
func parseBulkHeader(msg []byte, state *readState) error { func parseBulkHeader(msg []byte, state *readState) error {
@@ -246,9 +241,8 @@ func parseBulkHeader(msg []byte, state *readState) error {
state.expectedArgsCount = 1 state.expectedArgsCount = 1
state.args = make([][]byte, 0, 1) state.args = make([][]byte, 0, 1)
return nil return nil
} else {
return errors.New("protocol error: " + string(msg))
} }
return errors.New("protocol error: " + string(msg))
} }
func parseSingleLineReply(msg []byte) (redis.Reply, error) { func parseSingleLineReply(msg []byte) (redis.Reply, error) {