when parsing a source, try to process all of the servers to ensure maximum connectivity

- a corrupt source (no names/descriptions) will still abort parsing immediately
- a duplicate, missing or invalid server stamp will not abort parsing
  - these errors are logged as warnings, and returned as a single error at the end
This commit is contained in:
Alison Winters 2019-10-06 18:14:21 +08:00 committed by Frank Denis
parent 6513818cb3
commit 99e56a400f
1 changed files with 16 additions and 4 deletions

View File

@ -217,12 +217,19 @@ func (source *Source) Parse(prefix string) ([]RegisteredServer, error) {
func (source *Source) parseV2(prefix string) ([]RegisteredServer, error) {
var registeredServers []RegisteredServer
var stampErrs []string
appendStampErr := func(format string, a ...interface{}) {
stampErr := fmt.Sprintf(format, a...)
stampErrs = append(stampErrs, stampErr)
dlog.Warn(stampErr)
}
in := string(source.in)
parts := strings.Split(in, "## ")
if len(parts) < 2 {
return registeredServers, fmt.Errorf("Invalid format for source at [%v]", source.urls)
}
parts = parts[1:]
PartsLoop:
for _, part := range parts {
part = strings.TrimFunc(part, unicode.IsSpace)
subparts := strings.Split(part, "\n")
@ -240,7 +247,8 @@ func (source *Source) parseV2(prefix string) ([]RegisteredServer, error) {
subpart = strings.TrimFunc(subpart, unicode.IsSpace)
if strings.HasPrefix(subpart, "sdns:") {
if len(stampStr) > 0 {
return registeredServers, fmt.Errorf("Multiple stamps for server [%s] in source from [%v]", name, source.urls)
appendStampErr("Multiple stamps for server [%s]", name)
continue PartsLoop
}
stampStr = subpart
continue
@ -253,12 +261,13 @@ func (source *Source) parseV2(prefix string) ([]RegisteredServer, error) {
description += subpart
}
if len(stampStr) < 6 {
return registeredServers, fmt.Errorf("Missing stamp for server [%s] in source from [%v]", name, source.urls)
appendStampErr("Missing stamp for server [%s]", name)
continue
}
stamp, err := stamps.NewServerStampFromString(stampStr)
if err != nil {
dlog.Errorf("Invalid or unsupported stamp: [%v]", stampStr)
return registeredServers, err
appendStampErr("Invalid or unsupported stamp [%v]: %s", stampStr, err.Error())
continue
}
registeredServer := RegisteredServer{
name: name, stamp: stamp, description: description,
@ -266,6 +275,9 @@ func (source *Source) parseV2(prefix string) ([]RegisteredServer, error) {
dlog.Debugf("Registered [%s] with stamp [%s]", name, stamp.String())
registeredServers = append(registeredServers, registeredServer)
}
if len(stampErrs) > 0 {
return registeredServers, fmt.Errorf("%s", strings.Join(stampErrs, ", "))
}
return registeredServers, nil
}