Skip to content

Commit

Permalink
feat: support http level retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jun 10, 2024
1 parent 68d2dc1 commit dff55cd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ require (
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-envparse v0.1.0 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hodgesds/perf-utils v0.7.0 // indirect
github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973 // indirect
github.com/josharian/native v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-envparse v0.1.0 h1:bE++6bhIsNCPLvgDZkYqo3nA+/PFI51pkrHdmPSDFPY=
github.com/hashicorp/go-envparse v0.1.0/go.mod h1:OHheN1GoygLlAkTlXLXvAdnXdZxy8JUweQ1rAXx1xnc=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/hodgesds/perf-utils v0.7.0 h1:7KlHGMuig4FRH5fNw68PV6xLmgTe7jKs9hgAcEAbioU=
github.com/hodgesds/perf-utils v0.7.0/go.mod h1:LAklqfDadNKpkxoAJNHpD5tkY0rkZEVdnCEWN5k4QJY=
github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973 h1:hk4LPqXIY/c9XzRbe7dA6qQxaT6Axcbny0L/G5a4owQ=
Expand Down
4 changes: 2 additions & 2 deletions internal/cmgr/metric_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmgr

import (
"context"
"net/http"

"github.com/Ehco1996/ehco/internal/conn"
"github.com/Ehco1996/ehco/internal/constant"
Expand Down Expand Up @@ -72,9 +71,10 @@ func (cm *cmgrImpl) syncOnce(ctx context.Context) error {
}
cm.closedConnectionsMap = make(map[string][]conn.RelayConn)
cm.lock.Unlock()

if cm.cfg.NeedSync() {
cm.l.Debug("syncing data to server", zap.Any("data", req))
return myhttp.PostJson(http.DefaultClient, cm.cfg.SyncURL, &req)
return myhttp.PostJSONWithRetry(cm.cfg.SyncURL, &req)
} else {
cm.l.Debugf("remove %d closed connections", len(req.Stats))
}
Expand Down
11 changes: 3 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package config
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"

myhttp "github.com/Ehco1996/ehco/pkg/http"

"github.com/Ehco1996/ehco/internal/relay/conf"
"github.com/Ehco1996/ehco/pkg/sub"
xConf "github.com/xtls/xray-core/infra/conf"
Expand Down Expand Up @@ -79,14 +80,8 @@ func (c *Config) readFromFile() error {
}

func (c *Config) readFromHttp() error {
httpc := &http.Client{Timeout: 10 * time.Second}
r, err := httpc.Get(c.PATH)
if err != nil {
return err
}
defer r.Body.Close()
c.l.Infof("Load Config From HTTP: %s", c.PATH)
return json.NewDecoder(r.Body).Decode(&c)
return myhttp.GetJSONWithRetry(c.PATH, &c)
}

func (c *Config) Adjust() error {
Expand Down
22 changes: 19 additions & 3 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@ import (
"bytes"
"encoding/json"
"io"
"net/http"

"github.com/hashicorp/go-retryablehttp"
)

func PostJson(c *http.Client, url string, dataStruct interface{}) error {
func PostJSONWithRetry(url string, dataStruct interface{}) error {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3

buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(dataStruct); err != nil {
return err
}
r, err := http.Post(url, "application/json", buf)
r, err := retryClient.Post(url, "application/json", buf)
if err != nil {
return err
}
defer r.Body.Close()
_, err = io.ReadAll(r.Body)
return err
}

func GetJSONWithRetry(url string, dataStruct interface{}) error {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3

resp, err := retryablehttp.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(&dataStruct)
}

0 comments on commit dff55cd

Please sign in to comment.