Skip to content

Commit 8d679b9

Browse files
authored
Merge pull request #449 from TarsCloud/perf/app
refactor(tars): abstract global variables under tars package as application structure
2 parents f919505 + bbc9747 commit 8d679b9

File tree

27 files changed

+1297
-526
lines changed

27 files changed

+1297
-526
lines changed

examples/GinHttpServer/HttpServer

-11.1 MB
Binary file not shown.

examples/GinHttpServer/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/go-playground/universal-translator v0.18.0 // indirect
1414
github.com/go-playground/validator/v10 v10.10.0 // indirect
1515
github.com/goccy/go-json v0.9.7 // indirect
16-
github.com/google/uuid v1.3.0 // indirect
1716
github.com/json-iterator/go v1.1.12 // indirect
1817
github.com/leodido/go-urn v1.2.1 // indirect
1918
github.com/mattn/go-isatty v0.0.14 // indirect

examples/OpentelemetryServer/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ require (
3434
github.com/go-playground/validator/v10 v10.10.0 // indirect
3535
github.com/goccy/go-json v0.9.7 // indirect
3636
github.com/golang/protobuf v1.5.2 // indirect
37-
github.com/google/uuid v1.3.0 // indirect
3837
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
3938
github.com/json-iterator/go v1.1.12 // indirect
4039
github.com/leodido/go-urn v1.2.1 // indirect

examples/trace/TarsTraceBackServer/go.sum

Lines changed: 297 additions & 0 deletions
Large diffs are not rendered by default.

examples/trace/TarsTraceFrontServer/go.sum

Lines changed: 297 additions & 0 deletions
Large diffs are not rendered by default.

examples/zipkin/ZipkinTraceServer/ZipkinTraceImp.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

3-
import "context"
4-
import "time"
3+
import (
4+
"context"
5+
"time"
6+
)
57

68
// ZipkinTraceImp struct
79
type ZipkinTraceImp struct {

tars/adapter.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/TarsCloud/TarsGo/tars/util/tools"
1717
)
1818

19-
var reconnectMsg = "_reconnect_"
20-
2119
// AdapterProxy : Adapter proxy
2220
type AdapterProxy struct {
2321
resp sync.Map
@@ -61,10 +59,10 @@ func NewAdapterProxy(objName string, point *endpointf.EndpointF, comm *Communica
6159
DialTimeout: comm.Client.ClientDialTimeout,
6260
}
6361
if point.Istcp == endpoint.SSL {
64-
if tlsConfig, ok := clientObjTlsConfig[objName]; ok {
62+
if tlsConfig, ok := comm.app.clientObjTlsConfig[objName]; ok {
6563
conf.TlsConfig = tlsConfig
6664
} else {
67-
conf.TlsConfig = clientTlsConfig
65+
conf.TlsConfig = comm.app.clientTlsConfig
6866
}
6967
}
7068
c.conf = conf

tars/admin.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,30 @@ import (
1111
"time"
1212

1313
"github.com/TarsCloud/TarsGo/tars/util/debug"
14-
logger "github.com/TarsCloud/TarsGo/tars/util/rogger"
14+
"github.com/TarsCloud/TarsGo/tars/util/rogger"
1515
)
1616

1717
// Admin struct
1818
type Admin struct {
19+
app *application
1920
}
2021

21-
var (
22-
isShutdownByAdmin int32 = 0
23-
)
22+
type adminFn func(string) (string, error)
23+
24+
// RegisterAdmin register admin functions
25+
func RegisterAdmin(name string, fn adminFn) {
26+
defaultApp.RegisterAdmin(name, fn)
27+
}
28+
29+
// RegisterAdmin register admin functions
30+
func (a *application) RegisterAdmin(name string, fn adminFn) {
31+
a.adminMethods[name] = fn
32+
}
2433

2534
// Shutdown all servant by admin
2635
func (a *Admin) Shutdown() error {
27-
atomic.StoreInt32(&isShutdownByAdmin, 1)
28-
go graceShutdown()
36+
atomic.StoreInt32(&a.app.isShutdownByAdmin, 1)
37+
go a.app.graceShutdown()
2938
return nil
3039
}
3140

@@ -36,21 +45,21 @@ func (a *Admin) Notify(command string) (string, error) {
3645
go ReportNotifyInfo(NotifyNormal, "AdminServant::notify:"+command)
3746
switch cmd[0] {
3847
case "tars.viewversion":
39-
return GetServerConfig().Version, nil
48+
return a.app.ServerConfig().Version, nil
4049
case "tars.setloglevel":
4150
if len(cmd) >= 2 {
42-
appCache.LogLevel = cmd[1]
51+
a.app.appCache.LogLevel = cmd[1]
4352
switch cmd[1] {
4453
case "INFO":
45-
logger.SetLevel(logger.INFO)
54+
rogger.SetLevel(rogger.INFO)
4655
case "WARN":
47-
logger.SetLevel(logger.WARN)
56+
rogger.SetLevel(rogger.WARN)
4857
case "ERROR":
49-
logger.SetLevel(logger.ERROR)
58+
rogger.SetLevel(rogger.ERROR)
5059
case "DEBUG":
51-
logger.SetLevel(logger.DEBUG)
60+
rogger.SetLevel(rogger.DEBUG)
5261
case "NONE":
53-
logger.SetLevel(logger.OFF)
62+
rogger.SetLevel(rogger.OFF)
5463
default:
5564
return fmt.Sprintf("%s failed: unknown log level [%s]!", cmd[0], cmd[1]), nil
5665
}
@@ -61,7 +70,7 @@ func (a *Admin) Notify(command string) (string, error) {
6170
debug.DumpStack(true, "stackinfo", "tars.dumpstack:")
6271
return fmt.Sprintf("%s succ", command), nil
6372
case "tars.loadconfig":
64-
cfg := GetServerConfig()
73+
cfg := a.app.ServerConfig()
6574
remoteConf := NewRConf(cfg.App, cfg.Server, cfg.BasePath)
6675
_, err := remoteConf.GetConfig(cmd[1])
6776
if err != nil {
@@ -71,7 +80,7 @@ func (a *Admin) Notify(command string) (string, error) {
7180
case "tars.connection":
7281
return fmt.Sprintf("%s not support now!", command), nil
7382
case "tars.gracerestart":
74-
graceRestart()
83+
a.app.graceRestart()
7584
return "restart gracefully!", nil
7685
case "tars.pprof":
7786
port := ":8080"
@@ -85,7 +94,7 @@ func (a *Admin) Notify(command string) (string, error) {
8594
timeout = time.Second * time.Duration(t)
8695
}
8796
}
88-
cfg := GetServerConfig()
97+
cfg := a.app.ServerConfig()
8998
addr := cfg.LocalIP + port
9099
go func() {
91100
mux := http.NewServeMux()
@@ -103,14 +112,9 @@ func (a *Admin) Notify(command string) (string, error) {
103112
}()
104113
return "see http://" + addr + "/debug/pprof/", nil
105114
default:
106-
if fn, ok := adminMethods[cmd[0]]; ok {
115+
if fn, ok := a.app.adminMethods[cmd[0]]; ok {
107116
return fn(command)
108117
}
109118
return fmt.Sprintf("%s not support now!", command), nil
110119
}
111120
}
112-
113-
// RegisterAdmin register admin functions
114-
func RegisterAdmin(name string, fn adminFn) {
115-
adminMethods[name] = fn
116-
}

tars/appcache.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ package tars
22

33
import "github.com/TarsCloud/TarsGo/tars/protocol/res/endpointf"
44

5-
var (
6-
appCache AppCache
7-
)
8-
95
type AppCache struct {
106
TarsVersion string
117
ModifyTime string
@@ -23,5 +19,9 @@ type ObjCache struct {
2319
}
2420

2521
func GetAppCache() AppCache {
26-
return appCache
22+
return defaultApp.AppCache()
23+
}
24+
25+
func (a *application) AppCache() AppCache {
26+
return defaultApp.appCache
2727
}

0 commit comments

Comments
 (0)