diff --git a/get_config.go b/get_config.go index f175fdc..165ccd5 100644 --- a/get_config.go +++ b/get_config.go @@ -28,9 +28,6 @@ func (d *Diamond) GetConfig(args *GetConfigRequest) ([]byte, error) { if err := d.withUsual(args.Tenant, args.Group)(header); err != nil { return nil, err } - if err != nil { - return nil, err - } request := d.c.NewRequest(). WithTimeout(apiTimeout). WithPath(acmConfig.String(ip)). diff --git a/long_pull.go b/long_pull.go index 1c21dbb..d1af44d 100644 --- a/long_pull.go +++ b/long_pull.go @@ -18,44 +18,8 @@ func (d *Diamond) LongPull(unit Unit, contentMD5 string) (string, error) { if err != nil { return "", err } - headerSetters := []headerSetter{ - d.withLongPollingTimeout(), - d.withUsual(d.option.tenant, unit.Group), - } - header := make(http.Header) - for _, setter := range headerSetters { - if err := setter(header); err != nil { - return "", err - } - } - var longPollRequest struct { - ProbeModifyRequest string `url:"Probe-Modify-Request"` - } - longPollRequest.ProbeModifyRequest = strings.Join([]string{unit.DataID, unit.Group, contentMD5, d.option.tenant}, wordSeparator) + lineSeparator - request := d.c.NewRequest(). - WithPath(acmLongPull.String(ip)). - WithFormURLEncodedBody(longPollRequest). - WithHeader(header). - Post() - response, err := d.c.Do(request) - if err != nil { - return "", err - } - - switch response.StatusCode() { - case http.StatusServiceUnavailable: - return "", serviceUnavailableErr - case http.StatusInternalServerError: - return "", internalServerErr - } - - if !response.Success() { - return "", errors.New(response.String()) - } - - ret := url.QueryEscape(strings.Join([]string{unit.DataID, unit.Group, d.option.tenant}, wordSeparator) + lineSeparator) - if contentMD5 == "" || - ret == strings.TrimSpace(response.String()) { + switch contentMD5 { + case "": args := new(GetConfigRequest) args.Tenant = d.option.tenant args.Group = unit.Group @@ -64,12 +28,62 @@ func (d *Diamond) LongPull(unit Unit, contentMD5 string) (string, error) { if err != nil { return "", err } - contentMD5 := Md5(string(content)) + contentMD5 = Md5(string(content)) config := Config{ Content: content, } unit.ch <- config return contentMD5, nil + default: + headerSetters := []headerSetter{ + d.withLongPollingTimeout(), + d.withUsual(d.option.tenant, unit.Group), + } + header := make(http.Header) + for _, setter := range headerSetters { + if err := setter(header); err != nil { + return "", err + } + } + var longPollRequest struct { + ProbeModifyRequest string `url:"Probe-Modify-Request"` + } + longPollRequest.ProbeModifyRequest = strings.Join([]string{unit.DataID, unit.Group, contentMD5, d.option.tenant}, wordSeparator) + lineSeparator + request := d.c.NewRequest(). + WithPath(acmLongPull.String(ip)). + WithFormURLEncodedBody(longPollRequest). + WithHeader(header). + Post() + response, err := d.c.Do(request) + if err != nil { + return "", err + } + switch response.StatusCode() { + case http.StatusServiceUnavailable: + return "", serviceUnavailableErr + case http.StatusInternalServerError: + return "", internalServerErr + } + if !response.Success() { + return "", errors.New(response.String()) + } + ret := url.QueryEscape(strings.Join([]string{unit.DataID, unit.Group, d.option.tenant}, wordSeparator) + lineSeparator) + if ret == strings.TrimSpace(response.String()) { + args := new(GetConfigRequest) + args.Tenant = d.option.tenant + args.Group = unit.Group + args.DataID = unit.DataID + content, err := d.GetConfig(args) + if err != nil { + return "", err + } + contentMD5 := Md5(string(content)) + config := Config{ + Content: content, + } + unit.ch <- config + return contentMD5, nil + } + return contentMD5, nil } - return contentMD5, nil }