Skip to content

Commit 8324356

Browse files
authored
Merge pull request polarismesh#424 from daheige/feature/daheige/maintain-adjust
feat: maintain code adjust and default refactor
2 parents 4632805 + b626163 commit 8324356

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

maintain/default.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package maintain
2020
import (
2121
"context"
2222
"errors"
23-
"sync"
2423

2524
"github.com/polarismesh/polaris-server/auth"
2625
"github.com/polarismesh/polaris-server/service"
@@ -29,18 +28,17 @@ import (
2928

3029
var (
3130
server MaintainOperateServer
32-
maintainServer *Server = new(Server)
33-
once = sync.Once{}
34-
finishInit = false
31+
maintainServer = &Server{}
32+
finishInit bool
3533
)
3634

3735
// Initialize 初始化
3836
func Initialize(ctx context.Context, namingService service.DiscoverServer, healthCheckServer *healthcheck.Server) error {
39-
var err error
40-
once.Do(func() {
41-
err = initialize(ctx, namingService, healthCheckServer)
42-
})
37+
if finishInit {
38+
return nil
39+
}
4340

41+
err := initialize(ctx, namingService, healthCheckServer)
4442
if err != nil {
4543
return err
4644
}
@@ -49,14 +47,12 @@ func Initialize(ctx context.Context, namingService service.DiscoverServer, healt
4947
return nil
5048
}
5149

52-
func initialize(ctx context.Context, namingService service.DiscoverServer, healthCheckServer *healthcheck.Server) error {
53-
50+
func initialize(_ context.Context, namingService service.DiscoverServer, healthCheckServer *healthcheck.Server) error {
5451
authServer, err := auth.GetAuthServer()
5552
if err != nil {
5653
return err
5754
}
5855

59-
maintainServer.freeMemMu = new(sync.Mutex)
6056
maintainServer.namingServer = namingService
6157
maintainServer.healthCheckServer = healthCheckServer
6258

maintain/maintain.go

+24-21
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
commonlog "github.com/polarismesh/polaris-server/common/log"
2929
)
3030

31-
func (s *Server) GetServerConnections(ctx context.Context, req *ConnReq) (*ConnCountResp, error) {
31+
func (s *Server) GetServerConnections(_ context.Context, req *ConnReq) (*ConnCountResp, error) {
3232
if req.Protocol == "" {
3333
return nil, errors.New("missing param protocol")
3434
}
@@ -38,11 +38,11 @@ func (s *Server) GetServerConnections(ctx context.Context, req *ConnReq) (*ConnC
3838
return nil, errors.New("not found the protocol")
3939
}
4040

41-
var resp ConnCountResp
42-
43-
resp.Protocol = req.Protocol
44-
resp.Total = lis.GetListenerConnCount()
45-
resp.Host = make(map[string]int32)
41+
var resp = ConnCountResp{
42+
Protocol: req.Protocol,
43+
Total: lis.GetListenerConnCount(),
44+
Host: map[string]int32{},
45+
}
4646
if req.Host != "" {
4747
resp.Host[req.Host] = lis.GetHostConnCount(req.Host)
4848
} else {
@@ -55,7 +55,7 @@ func (s *Server) GetServerConnections(ctx context.Context, req *ConnReq) (*ConnC
5555
return &resp, nil
5656
}
5757

58-
func (s *Server) GetServerConnStats(ctx context.Context, req *ConnReq) (*ConnStatsResp, error) {
58+
func (s *Server) GetServerConnStats(_ context.Context, req *ConnReq) (*ConnStatsResp, error) {
5959
if req.Protocol == "" {
6060
return nil, errors.New("missing param protocol")
6161
}
@@ -83,22 +83,23 @@ func (s *Server) GetServerConnStats(ctx context.Context, req *ConnReq) (*ConnSta
8383
} else {
8484
resp.Stats = stats
8585
}
86-
resp.StatsSize = len(resp.Stats)
8786

88-
if resp.Stats == nil {
87+
resp.StatsSize = len(resp.Stats)
88+
if len(resp.Stats) == 0 {
8989
resp.Stats = make([]*connlimit.HostConnStat, 0)
9090
}
9191

9292
return &resp, nil
9393
}
9494

95-
func (s *Server) CloseConnections(ctx context.Context, reqs []ConnReq) error {
95+
func (s *Server) CloseConnections(_ context.Context, reqs []ConnReq) error {
9696
for _, entry := range reqs {
9797
listener := connlimit.GetLimitListener(entry.Protocol)
9898
if listener == nil {
9999
log.Warnf("[MAINTAIN] not found listener for protocol(%s)", entry.Protocol)
100100
continue
101101
}
102+
102103
if entry.Port != 0 {
103104
if conn := listener.GetHostConnection(entry.Host, entry.Port); conn != nil {
104105
log.Infof("[MAINTAIN] address(%s:%d) to be closed", entry.Host, entry.Port)
@@ -109,22 +110,23 @@ func (s *Server) CloseConnections(ctx context.Context, reqs []ConnReq) error {
109110

110111
log.Infof("[MAINTAIN] host(%s) connections to be closed", entry.Host)
111112
activeConns := listener.GetHostActiveConns(entry.Host)
112-
for _, conn := range activeConns {
113-
if conn != nil {
114-
_ = conn.Close()
113+
for k := range activeConns {
114+
if activeConns[k] != nil {
115+
_ = activeConns[k].Close()
115116
}
116117
}
117118
}
119+
118120
return nil
119121
}
120122

121-
func (s *Server) FreeOSMemory(ctx context.Context) error {
123+
func (s *Server) FreeOSMemory(_ context.Context) error {
122124
log.Info("[MAINTAIN] start doing free os memory")
123125
// 防止并发释放
124126
start := time.Now()
125-
s.freeMemMu.Lock()
127+
s.mu.Lock()
126128
debug.FreeOSMemory()
127-
s.freeMemMu.Unlock()
129+
s.mu.Unlock()
128130
log.Infof("[MAINTAIN] finish doing free os memory, used time: %v", time.Since(start))
129131
return nil
130132
}
@@ -133,19 +135,20 @@ func (s *Server) CleanInstance(ctx context.Context, req *api.Instance) *api.Resp
133135
return s.namingServer.CleanInstance(ctx, req)
134136
}
135137

136-
func (s *Server) GetLastHeartbeat(ctx context.Context, req *api.Instance) *api.Response {
138+
func (s *Server) GetLastHeartbeat(_ context.Context, req *api.Instance) *api.Response {
137139
return s.healthCheckServer.GetLastHeartbeat(req)
138140
}
139141

140-
func (s *Server) GetLogOutputLevel(ctx context.Context) (map[string]string, error) {
142+
func (s *Server) GetLogOutputLevel(_ context.Context) (map[string]string, error) {
141143
scopes := commonlog.Scopes()
142144
out := make(map[string]string, len(scopes))
143-
for k, v := range scopes {
144-
out[k] = v.GetOutputLevel().Name()
145+
for k := range scopes {
146+
out[k] = scopes[k].GetOutputLevel().Name()
145147
}
148+
146149
return out, nil
147150
}
148151

149-
func (s *Server) SetLogOutputLevel(ctx context.Context, scope string, level string) error {
152+
func (s *Server) SetLogOutputLevel(_ context.Context, scope string, level string) error {
150153
return commonlog.SetLogOutputLevel(scope, level)
151154
}

maintain/maintain_authability.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/polarismesh/polaris-server/common/model"
2525
)
2626

27+
var _ MaintainOperateServer = (*serverAuthAbility)(nil)
28+
2729
func (svr *serverAuthAbility) GetServerConnections(ctx context.Context, req *ConnReq) (*ConnCountResp, error) {
2830
authCtx := svr.collectMaintainAuthContext(ctx, model.Read, "GetServerConnections")
2931
_, err := svr.authMgn.CheckConsolePermission(authCtx)

maintain/server.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import (
2424
"github.com/polarismesh/polaris-server/service/healthcheck"
2525
)
2626

27+
var _ MaintainOperateServer = (*Server)(nil)
28+
2729
type Server struct {
28-
freeMemMu *sync.Mutex
30+
mu sync.Mutex
2931
namingServer service.DiscoverServer
3032
healthCheckServer *healthcheck.Server
3133
}

maintain/server_authability.go

+2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ func convertToErrCode(err error) uint32 {
5959
if errors.Is(err, model.ErrorTokenNotExist) {
6060
return api.TokenNotExisted
6161
}
62+
6263
if errors.Is(err, model.ErrorTokenDisabled) {
6364
return api.TokenDisabled
6465
}
66+
6567
return api.NotAllowedAccess
6668
}

0 commit comments

Comments
 (0)