Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed May 8, 2024
1 parent cd397a8 commit 0907121
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
var (
ErrEmptyParam = errors.New("error empty param")
ErrPickNote = errors.New("error pick node")
ErrFormRequest = errors.New("error form request")
ErrReadBody = errors.New("error read body")
ErrReachedRetryTimes = errors.New("error reached retry times")
)

Expand Down Expand Up @@ -105,14 +103,14 @@ func (e *Engine) Set(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("%v%v/%v/%v%v", addr, e.options.BasePath, key, value, _internalFlag)
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
http.Error(w, ErrFormRequest.Error(), http.StatusInternalServerError)
return
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
_, err = http.DefaultClient.Do(req)
slog.Info("EZCache: node set distantly", "from", e.options.Addr, "to", addr)
if err != nil {
http.Error(w, ErrFormRequest.Error(), http.StatusInternalServerError)
return
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
}
}
Expand Down Expand Up @@ -153,34 +151,32 @@ func (e *Engine) Get(w http.ResponseWriter, r *http.Request) {
http.Error(w, ErrPickNote.Error(), http.StatusInternalServerError)
return
}
// skip node itself
if strings.Contains(pickedNodeAddr, e.options.Addr) {
continue
return
}

url := fmt.Sprintf("%v%v/%v%v", pickedNodeAddr, e.options.BasePath, key, _internalFlag)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
http.Error(w, ErrFormRequest.Error(), http.StatusInternalServerError)
return
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
resp, err := http.DefaultClient.Do(req)
slog.Info("EZCache: node get distantly", "from", e.options.Addr, "to", pickedNodeAddr)
if err != nil {
http.Error(w, ErrFormRequest.Error(), http.StatusInternalServerError)
return
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}

// retry
if resp.StatusCode != http.StatusOK {
_ = resp.Body.Close()
continue
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
http.Error(w, ErrReadBody.Error(), http.StatusInternalServerError)
return
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
// populate cache
e.cache.Set(key, ByteView{
Expand Down Expand Up @@ -218,14 +214,14 @@ func (e *Engine) Delete(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("%v%v/%v%v", addr, e.options.BasePath, key, _internalFlag)
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
http.Error(w, ErrFormRequest.Error(), http.StatusInternalServerError)
return
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
_, err = http.DefaultClient.Do(req)
slog.Info("EZCache: node delete distantly", "from", e.options.Addr, "to", addr)
if err != nil {
http.Error(w, ErrFormRequest.Error(), http.StatusInternalServerError)
return
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
}

Expand Down

0 comments on commit 0907121

Please sign in to comment.