mirror of https://github.com/zhufuyi/sponge
update doc
This commit is contained in:
parent
d74d4504b6
commit
45e77dde11
|
@ -1,4 +1,7 @@
|
|||
## Change log
|
||||
|
||||
- Fix template.
|
||||
- Support setting custom request id key name.
|
||||
- Add delayed queuing, publish-subscribe functionality to the pkg/rabbitmq library.
|
||||
- Add clustering, sentinel functionality to pkg/goredis.
|
||||
- Fix the discovered bugs.
|
||||
- Update Documentation.
|
||||
|
|
|
@ -183,10 +183,12 @@ service userExample {
|
|||
// (2) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
|
||||
// you must add annotations for snake case names, such as string foo_bar = 1 [json_name = "foo_bar"],
|
||||
// to ensure that the front end and back end JSON naming is consistent.
|
||||
// (3) If the declared route path includes a variable, such as /api/v1/userExample/{id},
|
||||
// the request parameter of the rpc method contains the route variable field and this field
|
||||
// must be annotated, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""]; If the get method is used,
|
||||
// the request parameters must be annotated with form, e.g. uint productID = 1 [(tagger.tags) = "form:\"productID\""].
|
||||
// (3) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
|
||||
// message must contain the name of the path parameter and the name should be
|
||||
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
|
||||
// (4) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
|
||||
// a form tag must be added when defining the query parameter in the message,
|
||||
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
|
||||
|
||||
|
||||
enum GenderType {
|
||||
|
|
|
@ -13,7 +13,7 @@ message Params {
|
|||
|
||||
message Column {
|
||||
string name = 1; // column name
|
||||
string exp = 2; // expressions, which default to = when the value is null, have =, !=, >, >=, <, <=, like
|
||||
string exp = 2; // expressions, which default to = when the value is null, have =, !=, >, >=, <, <=, like, in
|
||||
string value = 3; // column value
|
||||
string logic = 4; // logical type, defaults to and when value is null, only &(and), ||(or)
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ func {{.LowerName}}Router(
|
|||
middleware.ContextRequestIDKey: middleware.GCtxRequestID(c), // request_id
|
||||
//middleware.HeaderAuthorizationKey: c.GetHeader(middleware.HeaderAuthorizationKey), // authorization
|
||||
})
|
||||
return metadata.NewOutgoingContext(c, md)
|
||||
return metadata.NewOutgoingContext(c.Request.Context(), md)
|
||||
}
|
||||
|
||||
serverNameExampleV1.Register{{.Name}}Router(
|
||||
|
|
|
@ -204,12 +204,15 @@ func (c *protoCopier) copyProtoFile(srcProtoFile string, targetProtoFile string,
|
|||
// replace go_package
|
||||
pbContent, err := os.ReadFile(srcProtoFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read file %s error, %v\n", srcProtoFile, err)
|
||||
return fmt.Errorf("read file %s error, %v", srcProtoFile, err)
|
||||
}
|
||||
pbContent = c.replacePackage(pbContent, isDependency)
|
||||
|
||||
tmpFile := os.TempDir() + gofile.GetPathDelimiter() + gofile.GetFilename(srcProtoFile)
|
||||
err = os.WriteFile(tmpFile, pbContent, 0644)
|
||||
err = os.WriteFile(tmpFile, pbContent, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("copy \"%s\" --> \"%s\"\n", srcProtoFile, targetProtoFile)
|
||||
_, err = gobash.Exec("mv", "-f", tmpFile, targetProtoFile)
|
||||
|
|
|
@ -80,7 +80,6 @@ mysql:
|
|||
# dsn format, <user>:<pass>@(127.0.0.1:3306)/<db>?[k=v& ......]
|
||||
dsn: "root:123456@(192.168.3.37:3306)/account?parseTime=true&loc=Local&charset=utf8,utf8mb4"
|
||||
enableLog: true # whether to turn on printing of all logs
|
||||
slowThreshold: 0 # if greater than 0, only print logs with a time greater than the threshold, with a higher priority than enableLog, in (ms)
|
||||
maxIdleConns: 3 # set the maximum number of connections in the idle connection pool
|
||||
maxOpenConns: 100 # set the maximum number of open database connections
|
||||
connMaxLifetime: 30 # sets the maximum time for which the connection can be reused, in minutes
|
||||
|
|
2
go.sum
2
go.sum
|
@ -569,8 +569,6 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
|
|||
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
|
||||
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
|
||||
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
|
||||
github.com/rabbitmq/amqp091-go v1.8.1 h1:RejT1SBUim5doqcL6s7iN6SBmsQqyTgXb1xMlH0h1hA=
|
||||
github.com/rabbitmq/amqp091-go v1.8.1/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc=
|
||||
github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc3Aoo=
|
||||
github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc=
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
|
|
|
@ -35,7 +35,7 @@ func userExampleServiceRouter(
|
|||
middleware.ContextRequestIDKey: middleware.GCtxRequestID(c), // request_id
|
||||
//middleware.HeaderAuthorizationKey: c.GetHeader(middleware.HeaderAuthorizationKey), // authorization
|
||||
})
|
||||
return metadata.NewOutgoingContext(c, md)
|
||||
return metadata.NewOutgoingContext(c.Request.Context(), md)
|
||||
}
|
||||
|
||||
serverNameExampleV1.RegisterUserExampleRouter(
|
||||
|
|
|
@ -130,11 +130,13 @@ func HeaderRequestIDField(c *gin.Context) zap.Field {
|
|||
|
||||
// -------------------------------------------------------------------------------------------
|
||||
|
||||
type metaData struct{}
|
||||
// RequestHeaderKey request header key
|
||||
var RequestHeaderKey = "request_header_key"
|
||||
|
||||
// WrapCtx wrap context, put the Keys and Header of gin.Context into context
|
||||
func WrapCtx(c *gin.Context) context.Context {
|
||||
return context.WithValue(c, metaData{}, c.Request.Header)
|
||||
ctx := context.WithValue(c.Request.Context(), ContextRequestIDKey, c.GetString(ContextRequestIDKey)) //nolint
|
||||
return context.WithValue(ctx, RequestHeaderKey, c.Request.Header) //nolint
|
||||
}
|
||||
|
||||
// GetFromCtx get value from context
|
||||
|
@ -158,7 +160,7 @@ func CtxRequestIDField(ctx context.Context) zap.Field {
|
|||
|
||||
// GetFromHeader get value from header
|
||||
func GetFromHeader(ctx context.Context, key string) string {
|
||||
header, ok := ctx.Value(metaData{}).(http.Header)
|
||||
header, ok := ctx.Value(RequestHeaderKey).(http.Header)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
@ -167,7 +169,7 @@ func GetFromHeader(ctx context.Context, key string) string {
|
|||
|
||||
// GetFromHeaders get values from header
|
||||
func GetFromHeaders(ctx context.Context, key string) []string {
|
||||
header, ok := ctx.Value(metaData{}).(http.Header)
|
||||
header, ok := ctx.Value(RequestHeaderKey).(http.Header)
|
||||
if !ok {
|
||||
return []string{}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,11 @@ type CtxKeyString string
|
|||
// RequestIDKey request_id
|
||||
var RequestIDKey = CtxKeyString(ContextRequestIDKey)
|
||||
|
||||
// CtxRequestIDField get request id field from context.Context
|
||||
func CtxRequestIDField(ctx context.Context) zap.Field {
|
||||
return zap.String(ContextRequestIDKey, metautils.ExtractOutgoing(ctx).Get(ContextRequestIDKey))
|
||||
}
|
||||
|
||||
// ---------------------------------- client interceptor ----------------------------------
|
||||
|
||||
// ClientCtxRequestID get request id from rpc client context.Context
|
||||
|
|
|
@ -559,6 +559,8 @@ func TestCtxRequestIDField(t *testing.T) {
|
|||
assert.NotNil(t, field)
|
||||
field = ServerCtxRequestIDField(context.Background())
|
||||
assert.NotNil(t, field)
|
||||
field = CtxRequestIDField(context.Background())
|
||||
assert.NotNil(t, field)
|
||||
}
|
||||
|
||||
func TestSetContextRequestIDKey(t *testing.T) {
|
||||
|
|
|
@ -157,13 +157,13 @@ func (p *profile) checkTimeout() {
|
|||
ctx, _ := context.WithTimeout(context.Background(), time.Second*time.Duration(durationSecond)) //nolint
|
||||
select {
|
||||
case <-p.stopCh:
|
||||
fmt.Println("[profile] reason for stopping: manual")
|
||||
fmt.Println("[profile] stop collecting profiles: manual")
|
||||
return
|
||||
case <-ctx.Done():
|
||||
if isStop() {
|
||||
p.stopProfile()
|
||||
}
|
||||
fmt.Println("[profile] reason for stopping: timeout")
|
||||
fmt.Println("[profile] stop collecting profiles: time is up")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,15 +22,22 @@ type CPU interface {
|
|||
}
|
||||
|
||||
func init() {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
var err error
|
||||
stats, err = newCgroupCPU()
|
||||
if err != nil {
|
||||
// fmt.Printf("cgroup cpu init failed(%v),switch to psutil cpu\n", err)
|
||||
errStr := err.Error()
|
||||
stats, err = newPsutilCPU(interval)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("cgroup cpu init failed!err:=%v", err))
|
||||
errStr += " | " + err.Error()
|
||||
fmt.Printf(`
|
||||
[ERROR] cgroup cpu init and psutil cpu init are all failed, %s.
|
||||
the dependency library https://github.com/shirou/gopsutil does not support getting this CPU information.
|
||||
|
||||
[NOTE] After using the project code generated by sponge, please set "enableStat", "enableLimit" and "enableCircuitBreaker"
|
||||
in configs/xxx.yml to false, which will not affect the normal use of the application.
|
||||
|
||||
`, errStr)
|
||||
return
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
|
|
|
@ -370,10 +370,12 @@ service {{.TName}} {
|
|||
// (2) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
|
||||
// you must add annotations for snake case names, such as string foo_bar = 1 [json_name = "foo_bar"],
|
||||
// to ensure that the front end and back end JSON naming is consistent.
|
||||
// (3) If the declared route path includes a variable, such as /api/v1/userExample/{id},
|
||||
// the request parameter of the rpc method contains the route variable field and this field
|
||||
// must be annotated, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""]; If the get method is used,
|
||||
// the request parameters must be annotated with form, e.g. uint productID = 1 [(tagger.tags) = "form:\"productID\""].
|
||||
// (3) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
|
||||
// message must contain the name of the path parameter and the name should be
|
||||
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
|
||||
// (4) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
|
||||
// a form tag must be added when defining the query parameter in the message,
|
||||
// such as string name = 1 [(tagger.tags) = "form:\"name\""];
|
||||
|
||||
|
||||
// protoMessageCreateCode
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
|
@ -13,7 +12,7 @@ import (
|
|||
|
||||
// System cpu information
|
||||
type System struct {
|
||||
UsagePercent float64 `json:"usage_percent"` // cpu usage, unit(%)
|
||||
UsagePercent float64 `json:"usage_percent"` // cpu usage, unit(%), current logical CPU usage, total usage is cores*UsagePercent
|
||||
CPUInfo []CPUInfo `json:"cpu_info"`
|
||||
}
|
||||
|
||||
|
@ -26,7 +25,7 @@ type CPUInfo struct {
|
|||
|
||||
// Process information
|
||||
type Process struct {
|
||||
UsagePercent float64 `json:"usage_percent"` // cpu usage, unit(%)
|
||||
UsagePercent float64 `json:"usage_percent"` // cpu usage, unit(%), current process occupies current logical CPU, total usage is cores*UsagePercent
|
||||
|
||||
RSS uint64 `json:"rss"` // use of physical memory, unit(M)
|
||||
VMS uint64 `json:"vms"` // use of virtual memory, unit(M)
|
||||
|
@ -35,12 +34,12 @@ type Process struct {
|
|||
// GetSystemCPU get system cpu info
|
||||
func GetSystemCPU() *System {
|
||||
sysUsagePercent := 0.0
|
||||
vs, err := cpu.Percent(time.Millisecond*10, false)
|
||||
vs, err := cpu.Percent(0, false) // total cpu Percent
|
||||
if err != nil {
|
||||
fmt.Printf("cpu.Percent error, %v\n", err)
|
||||
}
|
||||
if len(vs) == 1 {
|
||||
sysUsagePercent = vs[0]
|
||||
sysUsagePercent = vs[0] * 10
|
||||
}
|
||||
|
||||
var cpuInfos []CPUInfo
|
||||
|
|
Loading…
Reference in New Issue