Skip to content

refactor(tars): abstract global variables under tars package as application structure #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed examples/GinHttpServer/HttpServer
Binary file not shown.
1 change: 0 additions & 1 deletion examples/GinHttpServer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
1 change: 0 additions & 1 deletion examples/OpentelemetryServer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ require (
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
Expand Down
297 changes: 297 additions & 0 deletions examples/trace/TarsTraceBackServer/go.sum

Large diffs are not rendered by default.

297 changes: 297 additions & 0 deletions examples/trace/TarsTraceFrontServer/go.sum

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions examples/zipkin/ZipkinTraceServer/ZipkinTraceImp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import "context"
import "time"
import (
"context"
"time"
)

// ZipkinTraceImp struct
type ZipkinTraceImp struct {
Expand Down
6 changes: 2 additions & 4 deletions tars/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/TarsCloud/TarsGo/tars/util/tools"
)

var reconnectMsg = "_reconnect_"

// AdapterProxy : Adapter proxy
type AdapterProxy struct {
resp sync.Map
Expand Down Expand Up @@ -61,10 +59,10 @@ func NewAdapterProxy(objName string, point *endpointf.EndpointF, comm *Communica
DialTimeout: comm.Client.ClientDialTimeout,
}
if point.Istcp == endpoint.SSL {
if tlsConfig, ok := clientObjTlsConfig[objName]; ok {
if tlsConfig, ok := comm.app.clientObjTlsConfig[objName]; ok {
conf.TlsConfig = tlsConfig
} else {
conf.TlsConfig = clientTlsConfig
conf.TlsConfig = comm.app.clientTlsConfig
}
}
c.conf = conf
Expand Down
48 changes: 26 additions & 22 deletions tars/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@ import (
"time"

"github.com/TarsCloud/TarsGo/tars/util/debug"
logger "github.com/TarsCloud/TarsGo/tars/util/rogger"
"github.com/TarsCloud/TarsGo/tars/util/rogger"
)

// Admin struct
type Admin struct {
app *application
}

var (
isShutdownByAdmin int32 = 0
)
type adminFn func(string) (string, error)

// RegisterAdmin register admin functions
func RegisterAdmin(name string, fn adminFn) {
defaultApp.RegisterAdmin(name, fn)
}

// RegisterAdmin register admin functions
func (a *application) RegisterAdmin(name string, fn adminFn) {
a.adminMethods[name] = fn
}

// Shutdown all servant by admin
func (a *Admin) Shutdown() error {
atomic.StoreInt32(&isShutdownByAdmin, 1)
go graceShutdown()
atomic.StoreInt32(&a.app.isShutdownByAdmin, 1)
go a.app.graceShutdown()
return nil
}

Expand All @@ -36,21 +45,21 @@ func (a *Admin) Notify(command string) (string, error) {
go ReportNotifyInfo(NotifyNormal, "AdminServant::notify:"+command)
switch cmd[0] {
case "tars.viewversion":
return GetServerConfig().Version, nil
return a.app.ServerConfig().Version, nil
case "tars.setloglevel":
if len(cmd) >= 2 {
appCache.LogLevel = cmd[1]
a.app.appCache.LogLevel = cmd[1]
switch cmd[1] {
case "INFO":
logger.SetLevel(logger.INFO)
rogger.SetLevel(rogger.INFO)
case "WARN":
logger.SetLevel(logger.WARN)
rogger.SetLevel(rogger.WARN)
case "ERROR":
logger.SetLevel(logger.ERROR)
rogger.SetLevel(rogger.ERROR)
case "DEBUG":
logger.SetLevel(logger.DEBUG)
rogger.SetLevel(rogger.DEBUG)
case "NONE":
logger.SetLevel(logger.OFF)
rogger.SetLevel(rogger.OFF)
default:
return fmt.Sprintf("%s failed: unknown log level [%s]!", cmd[0], cmd[1]), nil
}
Expand All @@ -61,7 +70,7 @@ func (a *Admin) Notify(command string) (string, error) {
debug.DumpStack(true, "stackinfo", "tars.dumpstack:")
return fmt.Sprintf("%s succ", command), nil
case "tars.loadconfig":
cfg := GetServerConfig()
cfg := a.app.ServerConfig()
remoteConf := NewRConf(cfg.App, cfg.Server, cfg.BasePath)
_, err := remoteConf.GetConfig(cmd[1])
if err != nil {
Expand All @@ -71,7 +80,7 @@ func (a *Admin) Notify(command string) (string, error) {
case "tars.connection":
return fmt.Sprintf("%s not support now!", command), nil
case "tars.gracerestart":
graceRestart()
a.app.graceRestart()
return "restart gracefully!", nil
case "tars.pprof":
port := ":8080"
Expand All @@ -85,7 +94,7 @@ func (a *Admin) Notify(command string) (string, error) {
timeout = time.Second * time.Duration(t)
}
}
cfg := GetServerConfig()
cfg := a.app.ServerConfig()
addr := cfg.LocalIP + port
go func() {
mux := http.NewServeMux()
Expand All @@ -103,14 +112,9 @@ func (a *Admin) Notify(command string) (string, error) {
}()
return "see http://" + addr + "/debug/pprof/", nil
default:
if fn, ok := adminMethods[cmd[0]]; ok {
if fn, ok := a.app.adminMethods[cmd[0]]; ok {
return fn(command)
}
return fmt.Sprintf("%s not support now!", command), nil
}
}

// RegisterAdmin register admin functions
func RegisterAdmin(name string, fn adminFn) {
adminMethods[name] = fn
}
10 changes: 5 additions & 5 deletions tars/appcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package tars

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

var (
appCache AppCache
)

type AppCache struct {
TarsVersion string
ModifyTime string
Expand All @@ -23,5 +19,9 @@ type ObjCache struct {
}

func GetAppCache() AppCache {
return appCache
return defaultApp.AppCache()
}

func (a *application) AppCache() AppCache {
return defaultApp.appCache
}
Loading