Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
houseme committed Oct 21, 2022
1 parent 83aa3c4 commit 0a654b0
Show file tree
Hide file tree
Showing 21 changed files with 472 additions and 120 deletions.
67 changes: 44 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
run:
issues-exit-code: 1 #Default
tests: false #Default
skip-dirs: # 设置要忽略的目录
- test
- .*~
- api/swagger/docs
skip-files: # 设置不需要检查的go源码文件,支持正则匹配
- ".*.my.go$"
- ".*.pb.go$"
- _test.go

linters:
# Disable everything by default so upgrades to not include new "default
Expand All @@ -34,7 +42,7 @@ linters:
- typecheck
- misspell
- revive
- stylecheck
# - stylecheck
# - deadcode
# - errcheck
# - gocritic
Expand Down Expand Up @@ -93,40 +101,53 @@ linters-settings:
- name: atomic
- name: line-length-limit
severity: error
arguments: [195]
arguments: [480]
- name: unhandled-error
arguments : ["fmt.Printf", "myFunction"]
severity: warning
disabled: false
arguments : ["fmt.Printf", "myFunction", "builder.WriteString","buf.WriteByte","buf.WriteRune","buf.WriteString","buf.WriteTo","buf.Write","fmt.Println","iteratorInstancesProc","buffer.Write","buffer.WriteString","h.Write"]
funlen:
# Checks the number of lines in a function.
# If lower than 0, disable the check.
# Default: 60
lines: 120
lines: 210
# Checks the number of statements in a function.
# If lower than 0, disable the check.
# Default: 40
statements: 0
statements: -1
goconst:
# Minimal length of string constant.
# Default: 3
min-len: 2
# Minimum occurrences of constant string count to trigger issue.
# Default: 3
# For subsequent optimization, the value is reduced.
min-occurrences: 72
# Ignore test files.
# Default: false
ignore-tests: true
# Look for existing constants matching the values.
# Default: true
match-constant: false
# Search also for duplicated numbers.
# Default: false
numbers: true
# Minimum value, only works with goconst.numbers
# Default: 3
min: 5
# Maximum value, only works with goconst.numbers
# Default: 3
max: 20
# Ignore when constant is not used as function argument.
# Default: true
ignore-calls: false
gocritic:
disabled-checks:
- ifElseChain
stylecheck:
# Select the Go version to target.
# Default: 1.13
# Deprecated: use the global `run.go` instead.
go: "1.17"
# STxxxx checks in https://staticcheck.io/docs/configuration/options/#checks
# Default: ["*"]
checks: ["all", "-ST1000", "-ST1003", "-ST1016", "-ST1020", "-ST1021", "-ST1022"]
# https://staticcheck.io/docs/configuration/options/#dot_import_whitelist
# Default: ["github.com/mmcloughlin/avo/build", "github.com/mmcloughlin/avo/operand", "github.com/mmcloughlin/avo/reg"]
dot-import-whitelist:
- fmt
# https://staticcheck.io/docs/configuration/options/#initialisms
# Default: ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS"]
initialisms: ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS"]
# https://staticcheck.io/docs/configuration/options/#http_status_code_whitelist
# Default: ["200", "400", "404", "500"]
http-status-code-whitelist: ["200", "400", "404", "500"]
- assignOp
- appendAssign
- singleCaseSwitch
gocyclo:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 60
2 changes: 1 addition & 1 deletion apiserver/httpserver/maintain_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (h *HTTPServer) GetServerConnStats(req *restful.Request, rsp *restful.Respo
ctx := initContext(req)
params := httpcommon.ParseQueryParams(req)

var amount int = 0
var amount int
if amountStr, ok := params["amount"]; ok {
if n, err := strconv.Atoi(amountStr); err == nil {
amount = n
Expand Down
2 changes: 1 addition & 1 deletion apiserver/xdsserverv3/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func dumpSnapShotJSON(snapshot cache.ResourceSnapshot) []byte {
func yamlEncode(any interface{}) []byte {
data, _ := json.Marshal(any)
o := make(map[string]interface{})
json.Unmarshal(data, &o)
_ = json.Unmarshal(data, &o)
data, _ = yaml.Marshal(o)
return data
}
Expand Down
13 changes: 6 additions & 7 deletions apiserver/xdsserverv3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_extensions_common_ratelimit_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/common/ratelimit/v3"
Expand Down Expand Up @@ -139,7 +138,7 @@ func (x *XDSServer) Initialize(ctx context.Context, option map[string]interface{
return err
}

x.startSynTask(ctx)
_ = x.startSynTask(ctx)

return nil
}
Expand Down Expand Up @@ -251,7 +250,7 @@ func parseNodeID(nodeID string) (namespace string, uuid string, hostip string) {
}

// ID id 的格式是 namespace/uuid~hostIp
func (PolarisNodeHash) ID(node *envoy_config_core_v3.Node) string {
func (PolarisNodeHash) ID(node *core.Node) string {
if node == nil {
return ""
}
Expand Down Expand Up @@ -781,9 +780,9 @@ func (x *XDSServer) pushRegistryInfoToXDSCache(registryInfo map[string][]*Servic
versionLocal := time.Now().Format(time.RFC3339) + "/" + strconv.FormatUint(x.versionNum.Inc(), 10)

for ns, services := range registryInfo {
x.makeSnapshot(ns, versionLocal, services)
x.makePermissiveSnapshot(ns, versionLocal, services)
x.makeStrictSnapshot(ns, versionLocal, services)
_ = x.makeSnapshot(ns, versionLocal, services)
_ = x.makePermissiveSnapshot(ns, versionLocal, services)
_ = x.makeStrictSnapshot(ns, versionLocal, services)
}
return nil
}
Expand Down Expand Up @@ -1006,7 +1005,7 @@ func (x *XDSServer) startSynTask(ctx context.Context) error {
}

if len(needPush) > 0 {
x.pushRegistryInfoToXDSCache(needPush)
_ = x.pushRegistryInfoToXDSCache(needPush)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cache/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func iteratorInstancesProc(data *sync.Map, iterProc InstanceIterProc) error {

type servicePortsBucket struct {
lock sync.RWMutex
//servicePorts service-id -> []port
// servicePorts service-id -> []port
servicePorts map[string]map[string]struct{}
}

Expand Down
2 changes: 1 addition & 1 deletion cache/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (nsCache *namespaceCache) realUpdate(storeRollbackSec time.Duration) error
return err
}
nsCache.firstUpdate = false
nsCache.setNamespaces(ret)
_ = nsCache.setNamespaces(ret)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion common/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func CompareRoutingV2(a, b *v2.ExtendRoutingConfig) bool {
return a.CreateTime.Before(b.CreateTime)
}

// ConvertRoutingV1ToV2 v1 版本的路由规则转为 v2 版本进行存储
// ConvertRoutingV1ToExtendV2 v1 版本的路由规则转为 v2 版本进行存储
func ConvertRoutingV1ToExtendV2(svcName, svcNamespace string, rule *model.RoutingConfig) ([]*v2.ExtendRoutingConfig, []*v2.ExtendRoutingConfig, error) {
inRet := make([]*v2.ExtendRoutingConfig, 0, 4)
outRet := make([]*v2.ExtendRoutingConfig, 0, 4)
Expand Down
6 changes: 4 additions & 2 deletions common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ var (
BuildDate string
)

const defaultVersion = "v0.1.0"

// Get 获取版本号
func Get() string {
if Version == "" {
return "v0.1.0"
return defaultVersion
}

return Version
Expand All @@ -36,7 +38,7 @@ func Get() string {
// GetRevision 获取完整版本号信息,包括时间戳的
func GetRevision() string {
if Version == "" || BuildDate == "" {
return "v0.1.0"
return defaultVersion
}

return Version + "." + BuildDate
Expand Down
2 changes: 1 addition & 1 deletion config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (s *Server) GetConfigFileRichInfo(ctx context.Context, namespace, group, na
return api.NewConfigFileResponse(api.ExecuteSuccess, configFileBaseInfo)
}

// QueryConfigFilesByGroup querying configuration files
func (s *Server) QueryConfigFilesByGroup(ctx context.Context, namespace, group string,
offset, limit uint32) *api.ConfigBatchQueryResponse {

Expand Down Expand Up @@ -252,7 +253,6 @@ func (s *Server) SearchConfigFile(ctx context.Context, namespace, group, name, t
}

requestID, _ := ctx.Value(utils.StringContext("request-id")).(string)

count, files, err := s.queryConfigFileByTags(ctx, namespace, group, name, offset, limit, tagKVs...)
if err != nil {
log.ConfigScope().Error("[Config][Service] query config file tags error.",
Expand Down
3 changes: 1 addition & 2 deletions config/resource_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ func (s *serverAuthability) After(ctx context.Context, resourceType model.Resour
// onConfigGroupResource
func (s *serverAuthability) onConfigGroupResource(ctx context.Context, res *ResourceEvent) error {
authCtx := ctx.Value(utils.ContextAuthContextKey).(*model.AcquireContext)
ownerId := utils.ParseOwnerID(ctx)

authCtx.SetAttachment(model.ResourceAttachmentKey, map[api.ResourceType][]model.ResourceEntry{
api.ResourceType_ConfigGroups: {
{
ID: strconv.FormatUint(res.ConfigGroup.Id.GetValue(), 10),
Owner: ownerId,
Owner: utils.ParseOwnerID(ctx),
},
},
})
Expand Down
Loading

0 comments on commit 0a654b0

Please sign in to comment.