Skip to content

Commit

Permalink
faet:support envoy rls
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Aug 10, 2023
1 parent 9ac7f48 commit c93639c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 240 deletions.
6 changes: 6 additions & 0 deletions bootstrap/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ func (p *Agent) Start(ctx context.Context) error {
}

mux := p.debugSvr.Handler.(*http.ServeMux)
mux.HandleFunc("/sidecar/health/readiness", func(resp http.ResponseWriter, _ *http.Request) {
resp.WriteHeader(http.StatusOK)
})
mux.HandleFunc("/sidecar/health/liveness", func(resp http.ResponseWriter, _ *http.Request) {
resp.WriteHeader(http.StatusOK)
})
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
Expand Down
6 changes: 5 additions & 1 deletion bootstrap/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ func defaultSidecarConfig() *SidecarConfig {
},
RateLimit: &rls.Config{
Enable: false,
Network: "uinx",
Network: "unix",
Address: rls.DefaultRLSAddress,
},
Metrics: &metrics.MetricConfig{
Enable: false,
Port: 15985,
},
Debugger: &DebugConfig{
Enable: true,
Port: 50000,
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ docker_tag=$1

echo "docker repository : ${DOCKER_IMAGE_RPO}/polaris-sidecar, tag : ${docker_tag}"

bash build.sh
bash build.sh ${docker_tag}

if [ $? != 0 ]; then
echo "build polaris-sidecar failed"
Expand Down
11 changes: 9 additions & 2 deletions envoy/rls/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/polarismesh/polaris-go"
"github.com/polarismesh/polaris-go/pkg/model"
"github.com/polarismesh/polaris-sidecar/pkg/client"
"github.com/polarismesh/polaris-sidecar/pkg/log"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
Expand Down Expand Up @@ -88,17 +90,20 @@ func (svr *RateLimitServer) Destroy() {
const MaxUint32 = uint32(1<<32 - 1)

func (svr *RateLimitServer) ShouldRateLimit(ctx context.Context, req *pb.RateLimitRequest) (*pb.RateLimitResponse, error) {
log.Info("[envoy-rls] receive ratelimit request", zap.Any("req", req))
acquireQuota := req.GetHitsAddend()
if acquireQuota == 0 {
acquireQuota = 1
}

quotaReq, err := svr.buildQuotaRequest(req.GetDomain(), acquireQuota, req.GetDescriptors())
if err != nil {
log.Error("[envoy-rls] build ratelimit quota request", zap.Error(err))
return nil, err
}
future, err := svr.limiter.GetQuota(quotaReq)
if err != nil {
log.Error("[envoy-rls] get quota", zap.Error(err))
return nil, err
}
resp := future.Get()
Expand All @@ -115,11 +120,13 @@ func (svr *RateLimitServer) ShouldRateLimit(ctx context.Context, req *pb.RateLim
})
}

return &pb.RateLimitResponse{
rlsRsp := &pb.RateLimitResponse{
OverallCode: overallCode,
Statuses: descriptorStatus,
RawBody: []byte(resp.Info),
}, nil
}
log.Info("[envoy-rls] send envoy rls response", zap.Any("rsp", rlsRsp))
return rlsRsp, nil
}

func (svr *RateLimitServer) buildQuotaRequest(domain string, acquireQuota uint32,
Expand Down
25 changes: 11 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ require (
github.com/intel-go/cpuid v0.0.0-20220614022739-219e067757cb
github.com/miekg/dns v1.1.55
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/polarismesh/polaris-go v1.5.1
github.com/polarismesh/specification v1.3.2-alpha.5
github.com/polarismesh/polaris-go v1.5.2
github.com/polarismesh/specification v1.3.2
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.24.0
google.golang.org/grpc v1.56.1
go.uber.org/zap v1.25.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -27,7 +27,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -39,19 +38,17 @@ require (
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.10.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230626202813-9b080da550b3 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230626202813-9b080da550b3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230626202813-9b080da550b3 // indirect
google.golang.org/genproto v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit c93639c

Please sign in to comment.