zhulijian1 commented on a change in pull request #108: #109 client支持指定revision
URL: https://github.com/apache/servicecomb-kie/pull/108#discussion_r386248676
 
 

 ##########
 File path: client/client.go
 ##########
 @@ -158,31 +161,39 @@ func (c *Client) Get(ctx context.Context, opts 
...GetOption) (*model.KVResponse,
        h := http.Header{}
        resp, err := c.c.Do(ctx, http.MethodGet, url, h, nil)
        if err != nil {
-               return nil, err
+               return nil, -1, err
+       }
+       responseRevision, errRevision := 
strconv.Atoi(resp.Header.Get(common.HeaderRevision))
+       if errRevision != nil {
+               responseRevision = -1
        }
        b := httputil.ReadBody(resp)
        if resp.StatusCode != http.StatusOK {
                if resp.StatusCode == http.StatusNotFound {
-                       return nil, ErrKeyNotExist
+                       return nil, responseRevision, ErrKeyNotExist
                }
                if resp.StatusCode == http.StatusNotModified {
-                       return nil, ErrNoChanges
+                       return nil, responseRevision, ErrNoChanges
                }
                openlogging.Error(MsgGetFailed, 
openlogging.WithTags(openlogging.Tags{
                        "k":      options.Key,
                        "status": resp.Status,
                        "body":   b,
                }))
-               return nil, fmt.Errorf(FmtGetFailed, options.Key, resp.Status, 
b)
+               return nil, responseRevision, fmt.Errorf(FmtGetFailed, 
options.Key, resp.Status, b)
+       } else if errRevision != nil {
+               msg := fmt.Sprintf("get revision from response header failed 
when the request status is OK: %v", err)
+               openlogging.Error(msg)
+               return nil, responseRevision, fmt.Errorf(msg)
        }
        var kvs *model.KVResponse
        err = json.Unmarshal(b, &kvs)
        if err != nil {
                openlogging.Error("unmarshal kv failed:" + err.Error())
-               return nil, err
+               return nil, responseRevision, err
        }
-       c.CurrentRevision = resp.Header.Get(common.HeaderRevision)
-       return kvs, nil
+       c.currentRevision = responseRevision
+       return kvs, responseRevision, nil
 
 Review comment:
   
revision和response必须同时返回,调用者之后再调CurrentRevisio()的时候可能已经变了,不是上次get时候的revision了。协程1get
 -> put更新 -> 协程2get ->协程1查CurrentRevisio ->协程1用CurrentRevisio get

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to