Skip to content

Commit

Permalink
feat: organize part of the repetitive content (#767)
Browse files Browse the repository at this point in the history
* feat: modify config

* feat: organize part of the repetitive content
  • Loading branch information
houseme authored Oct 24, 2022
1 parent ac7db94 commit b052ca8
Show file tree
Hide file tree
Showing 37 changed files with 468 additions and 394 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ on:
branches:
- main
- release*
- feature/**
pull_request:
branches:
- main
- release*
- feature/**

jobs:
golangci:
Expand Down
12 changes: 10 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ linters-settings:
# Minimum occurrences of constant string count to trigger issue.
# Default: 3
# For subsequent optimization, the value is reduced.
min-occurrences: 72
min-occurrences: 50
# Ignore test files.
# Default: false
ignore-tests: true
Expand Down Expand Up @@ -150,4 +150,12 @@ linters-settings:
gocyclo:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 60
min-complexity: 60
gosimple:
# Select the Go version to target.
# Default: 1.13
# Deprecated: use the global `run.go` instead.
go: "1.17"
# Sxxxx checks in https://staticcheck.io/docs/configuration/options/#checks
# Default: ["*"]
checks: ["all","-S1001","-S1000","-S1025"]
16 changes: 8 additions & 8 deletions apiserver/grpcserver/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ func (b *BaseGrpcServer) streamInterceptor(srv interface{}, ss grpc.ServerStream

err = handler(srv, stream)
if err != nil {
status, ok := status.FromError(err)
if ok && status.Code() == codes.Canceled {
fromError, ok := status.FromError(err)
if ok && fromError.Code() == codes.Canceled {
// 存在非EOF读错误或者写错误
b.log.Info("[API-Server][GRPC] handler stream is canceled by client",
zap.String("client-address", stream.ClientAddress),
zap.String("user-agent", stream.UserAgent),
zap.String("request-id", stream.RequestID),
utils.ZapRequestID(stream.RequestID),
zap.String("method", stream.Method),
zap.Error(err),
)
Expand All @@ -258,7 +258,7 @@ func (b *BaseGrpcServer) streamInterceptor(srv interface{}, ss grpc.ServerStream
b.log.Error("[API-Server][GRPC] handler stream",
zap.String("client-address", stream.ClientAddress),
zap.String("user-agent", stream.UserAgent),
zap.String("request-id", stream.RequestID),
utils.ZapRequestID(stream.RequestID),
zap.String("method", stream.Method),
zap.Error(err),
)
Expand All @@ -282,7 +282,7 @@ func (b *BaseGrpcServer) preprocess(stream *VirtualStream, isPrint bool) error {
b.log.Info("[API-Server][GRPC] receive request",
zap.String("client-address", stream.ClientAddress),
zap.String("user-agent", stream.UserAgent),
zap.String("request-id", stream.RequestID),
utils.ZapRequestID(stream.RequestID),
zap.String("method", stream.Method),
)
}
Expand All @@ -305,7 +305,7 @@ func (b *BaseGrpcServer) postprocess(stream *VirtualStream, m interface{}) {
b.log.Error("[API-Server][GRPC] send response",
zap.String("client-address", stream.ClientAddress),
zap.String("user-agent", stream.UserAgent),
zap.String("request-id", stream.RequestID),
utils.ZapRequestID(stream.RequestID),
zap.String("method", stream.Method),
zap.String("response", response.String()),
)
Expand All @@ -320,7 +320,7 @@ func (b *BaseGrpcServer) postprocess(stream *VirtualStream, m interface{}) {
b.log.Error("[API-Server][GRPC] send response",
zap.String("client-address", stream.ClientAddress),
zap.String("user-agent", stream.UserAgent),
zap.String("request-id", stream.RequestID),
utils.ZapRequestID(stream.RequestID),
zap.String("method", stream.Method),
zap.String("response", response.String()),
)
Expand All @@ -336,7 +336,7 @@ func (b *BaseGrpcServer) postprocess(stream *VirtualStream, m interface{}) {
b.log.Info("[API-Server][GRPC] handling time > 1s",
zap.String("client-address", stream.ClientAddress),
zap.String("user-agent", stream.UserAgent),
zap.String("request-id", stream.RequestID),
utils.ZapRequestID(stream.RequestID),
zap.String("method", stream.Method),
zap.Duration("handling-time", diff),
)
Expand Down
2 changes: 1 addition & 1 deletion apiserver/grpcserver/discover/v1/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (g *DiscoverServer) Discover(server api.PolarisGRPC_DiscoverServer) error {
zap.String("type", api.DiscoverRequest_DiscoverRequestType_name[int32(in.Type)]),
zap.String("client-address", clientAddress),
zap.String("user-agent", userAgent),
zap.String("request-id", requestID),
utils.ZapRequestID(requestID),
)

// 是否允许访问
Expand Down
2 changes: 1 addition & 1 deletion apiserver/grpcserver/discover/v2/client_access_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (g *DiscoverServer) Discover(server apiv2.PolarisGRPC_DiscoverServer) error
zap.String("type", apiv2.DiscoverRequest_DiscoverRequestType_name[int32(in.Type)]),
zap.String("client-address", clientAddress),
zap.String("user-agent", userAgent),
zap.String("request-id", requestID),
utils.ZapRequestID(requestID),
)

// 是否允许访问
Expand Down
19 changes: 9 additions & 10 deletions apiserver/httpserver/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/wrappers"
"go.uber.org/zap"

"github.com/polarismesh/polaris/apiserver/httpserver/i18n"
api "github.com/polarismesh/polaris/common/api/v1"
Expand All @@ -51,14 +50,14 @@ func (h *Handler) ParseArray(createMessage func() proto.Message) (context.Contex
// read open bracket
_, err := jsonDecoder.Token()
if err != nil {
log.Error(err.Error(), zap.String("request-id", requestID))
log.Error(err.Error(), utils.ZapRequestID(requestID))
return nil, err
}
for jsonDecoder.More() {
protoMessage := createMessage()
err := jsonpb.UnmarshalNext(jsonDecoder, protoMessage)
if err != nil {
log.Error(err.Error(), zap.String("request-id", requestID))
log.Error(err.Error(), utils.ZapRequestID(requestID))
return nil, err
}
}
Expand Down Expand Up @@ -101,7 +100,7 @@ func (h *Handler) postParseMessage(requestID string) (context.Context, error) {
func (h *Handler) Parse(message proto.Message) (context.Context, error) {
requestID := h.Request.HeaderParameter("Request-Id")
if err := jsonpb.Unmarshal(h.Request.Request.Body, message); err != nil {
log.Error(err.Error(), zap.String("request-id", requestID))
log.Error(err.Error(), utils.ZapRequestID(requestID))
return nil, err
}
return h.postParseMessage(requestID)
Expand Down Expand Up @@ -164,7 +163,7 @@ func (h *Handler) WriteHeaderAndProto(obj api.ResponseMessage) {
status := api.CalcCode(obj)

if status != http.StatusOK {
log.Error(obj.String(), zap.String("request-id", requestID))
log.Error(obj.String(), utils.ZapRequestID(requestID))
}
if code := obj.GetCode().GetValue(); code != api.ExecuteSuccess {
h.Response.AddHeader(utils.PolarisCode, fmt.Sprintf("%d", code))
Expand All @@ -176,18 +175,18 @@ func (h *Handler) WriteHeaderAndProto(obj api.ResponseMessage) {
m := jsonpb.Marshaler{Indent: " ", EmitDefaults: true}
err := m.Marshal(h.Response, h.i18n(obj))
if err != nil {
log.Error(err.Error(), zap.String("request-id", requestID))
log.Error(err.Error(), utils.ZapRequestID(requestID))
}
}

// WriteHeaderAndProto 返回Code和Proto
// WriteHeaderAndProtoV2 返回Code和Proto
func (h *Handler) WriteHeaderAndProtoV2(obj apiv2.ResponseMessage) {
requestID := h.Request.HeaderParameter(utils.PolarisRequestID)
h.Request.SetAttribute(utils.PolarisCode, obj.GetCode())
status := apiv2.CalcCode(obj)

if status != http.StatusOK {
log.Error(obj.String(), zap.String("request-id", requestID))
log.Error(obj.String(), utils.ZapRequestID(requestID))
}
if code := obj.GetCode(); code != api.ExecuteSuccess {
h.Response.AddHeader(utils.PolarisCode, fmt.Sprintf("%d", code))
Expand All @@ -200,7 +199,7 @@ func (h *Handler) WriteHeaderAndProtoV2(obj apiv2.ResponseMessage) {
m := jsonpb.Marshaler{Indent: " ", EmitDefaults: true}
err := m.Marshal(h.Response, obj)
if err != nil {
log.Error(err.Error(), zap.String("request-id", requestID))
log.Error(err.Error(), utils.ZapRequestID(requestID))
}
}

Expand Down Expand Up @@ -233,7 +232,7 @@ func (h *Handler) i18n(obj api.ResponseMessage) api.ResponseMessage {
return obj
}

// parseQueryParams 解析并获取HTTP的query params
// ParseQueryParams 解析并获取HTTP的query params
func ParseQueryParams(req *restful.Request) map[string]string {
queryParams := make(map[string]string)
for key, value := range req.Request.URL.Query() {
Expand Down
10 changes: 5 additions & 5 deletions apiserver/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (h *HTTPServer) preprocess(req *restful.Request, rsp *restful.Response) err
scope.Info("receive request",
zap.String("client-address", req.Request.RemoteAddr),
zap.String("user-agent", req.HeaderParameter("User-Agent")),
zap.String("request-id", requestID),
utils.ZapRequestID(requestID),
zap.String("platform-id", platformID),
zap.String("method", req.Request.Method),
zap.String("url", requestURL),
Expand Down Expand Up @@ -510,7 +510,7 @@ func (h *HTTPServer) postProcess(req *restful.Request, rsp *restful.Response) {
scope.Info("handling time > 1s",
zap.String("client-address", req.Request.RemoteAddr),
zap.String("user-agent", req.HeaderParameter("User-Agent")),
zap.String("request-id", req.HeaderParameter("Request-Id")),
utils.ZapRequestID(req.HeaderParameter("Request-Id")),
zap.String("method", req.Request.Method),
zap.String("url", req.Request.URL.String()),
zap.Duration("handling-time", diff),
Expand Down Expand Up @@ -542,7 +542,7 @@ func (h *HTTPServer) enterAuth(req *restful.Request, rsp *restful.Response) erro
if !h.auth.IsWhiteList(segments[0]) && !h.auth.Allow(pid, pToken) {
log.Error("http access is not allowed",
zap.String("client", address),
zap.String("request-id", rid),
utils.ZapRequestID(rid),
zap.String("platform-id", pid),
zap.String("platform-token", pToken))
httpcommon.HTTPResponse(req, rsp, api.NotAllowedAccess)
Expand All @@ -568,7 +568,7 @@ func (h *HTTPServer) enterRateLimit(req *restful.Request, rsp *restful.Response)
}
if ok := h.rateLimit.Allow(plugin.IPRatelimit, segments[0]); !ok {
log.Error("ip ratelimit is not allow", zap.String("client", address),
zap.String("request-id", rid))
utils.ZapRequestID(rid))
httpcommon.HTTPResponse(req, rsp, api.IPRateLimit)
return errors.New("ip ratelimit is not allow")
}
Expand All @@ -578,7 +578,7 @@ func (h *HTTPServer) enterRateLimit(req *restful.Request, rsp *restful.Response)
strings.TrimSuffix(req.Request.URL.Path, "/"))
if ok := h.rateLimit.Allow(plugin.APIRatelimit, apiName); !ok {
log.Error("api ratelimit is not allow", zap.String("client", address),
zap.String("request-id", rid), zap.String("api", apiName))
utils.ZapRequestID(rid), zap.String("api", apiName))
httpcommon.HTTPResponse(req, rsp, api.APIRateLimit)
return errors.New("api ratelimit is not allow")
}
Expand Down
3 changes: 2 additions & 1 deletion apiserver/httpserver/v1/naming_client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/polarismesh/polaris/apiserver"
httpcommon "github.com/polarismesh/polaris/apiserver/httpserver/http"
api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/utils"
)

// GetClientAccessServer get client access server
Expand Down Expand Up @@ -160,7 +161,7 @@ func (h *HTTPServerV1) Discover(req *restful.Request, rsp *restful.Response) {
zap.String("type", api.DiscoverRequest_DiscoverRequestType_name[int32(discoverRequest.Type)]),
zap.String("client-address", req.Request.RemoteAddr),
zap.String("user-agent", req.HeaderParameter("User-Agent")),
zap.String("request-id", req.HeaderParameter("Request-Id")),
utils.ZapRequestID(req.HeaderParameter("Request-Id")),
)

var ret *api.DiscoverResponse
Expand Down
3 changes: 2 additions & 1 deletion apiserver/httpserver/v2/naming_client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
httpcommon "github.com/polarismesh/polaris/apiserver/httpserver/http"
api "github.com/polarismesh/polaris/common/api/v1"
apiv2 "github.com/polarismesh/polaris/common/api/v2"
"github.com/polarismesh/polaris/common/utils"
)

// GetClientAccessServer get client access server
Expand Down Expand Up @@ -81,7 +82,7 @@ func (h *HTTPServerV2) Discover(req *restful.Request, rsp *restful.Response) {
zap.String("type", api.DiscoverRequest_DiscoverRequestType_name[int32(discoverRequest.Type)]),
zap.String("client-address", req.Request.RemoteAddr),
zap.String("user-agent", req.HeaderParameter("User-Agent")),
zap.String("request-id", req.HeaderParameter("Request-Id")),
utils.ZapRequestID(req.HeaderParameter("Request-Id")),
)

var ret *apiv2.DiscoverResponse
Expand Down
3 changes: 2 additions & 1 deletion apiserver/prometheussd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/polarismesh/polaris/apiserver"
"github.com/polarismesh/polaris/common/connlimit"
"github.com/polarismesh/polaris/common/log"
"github.com/polarismesh/polaris/common/utils"
"github.com/polarismesh/polaris/service"
)

Expand Down Expand Up @@ -256,7 +257,7 @@ func (h *PrometheusServer) postProcess(req *restful.Request, _ *restful.Response
log.Info("[API-Server][Prometheus] handling time > 1s",
zap.String("client-address", req.Request.RemoteAddr),
zap.String("user-agent", req.HeaderParameter("User-Agent")),
zap.String("request-id", req.HeaderParameter("Request-Id")),
utils.ZapRequestID(req.HeaderParameter("Request-Id")),
zap.String("method", req.Request.Method),
zap.String("url", path),
zap.Duration("handling-time", diff),
Expand Down
12 changes: 6 additions & 6 deletions auth/defaultauth/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func StoreCode2APICode(err error) uint32 {
// checkName 名称检查
func checkName(name *wrappers.StringValue) error {
if name == nil {
return errors.New("nil")
return errors.New(utils.NilErrString)
}

if name.GetValue() == "" {
return errors.New("empty")
return errors.New(utils.EmptyErrString)
}

if name.GetValue() == "polariadmin" {
Expand All @@ -98,11 +98,11 @@ func checkName(name *wrappers.StringValue) error {
// checkPassword 密码检查
func checkPassword(password *wrappers.StringValue) error {
if password == nil {
return errors.New("nil")
return errors.New(utils.NilErrString)
}

if password.GetValue() == "" {
return errors.New("empty")
return errors.New(utils.EmptyErrString)
}

if pLen := len(password.GetValue()); pLen < 6 || pLen > 17 {
Expand All @@ -115,11 +115,11 @@ func checkPassword(password *wrappers.StringValue) error {
// checkOwner 检查用户的 owner 信息
func checkOwner(owner *wrappers.StringValue) error {
if owner == nil {
return errors.New("nil")
return errors.New(utils.NilErrString)
}

if owner.GetValue() == "" {
return errors.New("empty")
return errors.New(utils.EmptyErrString)
}

if utf8.RuneCountInString(owner.GetValue()) > utils.MaxOwnersLength {
Expand Down
2 changes: 1 addition & 1 deletion common/log/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func formatDate(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
func updateScopes(typeName string, options *Options, cores []zapcore.Core, errSink zapcore.WriteSyncer) error {
scope := FindScope(typeName)
if scope == nil {
//对还没注册的日志配置进行注册
// 对还没注册的日志配置进行注册
scope = RegisterScope(typeName, fmt.Sprintf("%s logging messages.", typeName), 0)
}

Expand Down
5 changes: 5 additions & 0 deletions common/utils/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
* specific language governing permissions and limitations under the License.
*/

// Package utils contains common utility functions
package utils

// StringSet is a set of strings
type StringSet interface {
Add(val string)

Expand All @@ -27,6 +29,7 @@ type StringSet interface {
Range(func(val string) bool)
}

// NewStringSet returns a new StringSet
func NewStringSet() StringSet {
return &stringSet{
container: make(map[string]struct{}),
Expand All @@ -37,10 +40,12 @@ type stringSet struct {
container map[string]struct{}
}

// Add adds a string to the set
func (set *stringSet) Add(val string) {
set.container[val] = struct{}{}
}

// Remove removes a string from the set
func (set *stringSet) Remove(val string) {
delete(set.container, val)
}
Expand Down
Loading

0 comments on commit b052ca8

Please sign in to comment.