Skip to content

Commit

Permalink
fix: long pull和get config的顺序
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Jul 25, 2019
1 parent f31f05b commit d03124e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 43 deletions.
3 changes: 0 additions & 3 deletions get_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
94 changes: 54 additions & 40 deletions long_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit d03124e

Please sign in to comment.