Skip to content

Commit

Permalink
add sonic
Browse files Browse the repository at this point in the history
  • Loading branch information
wubin48435 committed Aug 7, 2024
1 parent f88f682 commit 5b3b915
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 9 deletions.
43 changes: 43 additions & 0 deletions framework/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package config

import (
"fmt"
"github.com/unionj-cloud/go-doudou/v2/toolkit/constants"
"github.com/unionj-cloud/go-doudou/v2/toolkit/fileutils"
"log"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/apolloconfig/agollo/v4"
Expand Down Expand Up @@ -92,6 +97,18 @@ func CheckDev() bool {
return stringutils.IsEmpty(os.Getenv("GDD_ENV")) || os.Getenv("GDD_ENV") == "dev"
}

var G_LogFile *os.File
var lock sync.Mutex

func Shutdown() {
lock.Lock()
defer lock.Unlock()
if G_LogFile != nil {
G_LogFile.Close()
G_LogFile = nil
}
}

func init() {
LoadConfigFromLocal()
LoadConfigFromRemote()
Expand All @@ -106,6 +123,28 @@ func init() {
zlogger.WithDiscard(cast.ToBoolOrDefault(GddLogDiscard.Load(), DefaultGddLogDiscard)),
zlogger.WithZeroLogLevel(zl),
}
logPath := GddLogPath.LoadOrDefault(DefaultGddLogPath)
if stringutils.IsNotEmpty(logPath) {
fileutils.CreateDirectory(logPath)
logFile := filepath.Join(logPath, "app.log")
G_LogFile, err = os.OpenFile(
logFile,
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
0664,
)
if err != nil {
panic(err)
}
logStyle := GddLogStyle.LoadOrDefault(DefaultGddLogStyle)
switch logStyle {
case "console":
output := zerolog.ConsoleWriter{Out: G_LogFile, TimeFormat: constants.FORMAT, NoColor: true}
opts = append(opts, zlogger.WithWriter(output))
default:
opts = append(opts, zlogger.WithWriter(G_LogFile))
}
log.Printf("GDD_LOG_PATH is configured. Please view app log from %s", logFile)
}
zlogger.InitEntry(zlogger.NewLoggerConfig(opts...))
}

Expand Down Expand Up @@ -134,6 +173,10 @@ const (
GddLogReqEnable envVariable = "GDD_LOG_REQ_ENABLE"
GddLogCaller envVariable = "GDD_LOG_CALLER"
GddLogDiscard envVariable = "GDD_LOG_DISCARD"
GddLogPath envVariable = "GDD_LOG_PATH"
// GddLogStyle is only valid when GDD_LOG_PATH is specified, accepts json or console.
// Default is json
GddLogStyle envVariable = "GDD_LOG_STYLE"
// GddGraceTimeout sets graceful shutdown timeout
GddGraceTimeout envVariable = "GDD_GRACE_TIMEOUT"
// GddWriteTimeout sets http connection write timeout
Expand Down
2 changes: 2 additions & 0 deletions framework/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
DefaultGddLogReqEnable = false
DefaultGddLogCaller = true
DefaultGddLogDiscard = false
DefaultGddLogPath = ""
DefaultGddLogStyle = "json"
DefaultGddGraceTimeout = "15s"
DefaultGddWriteTimeout = "15s"
DefaultGddReadTimeout = "15s"
Expand Down
33 changes: 26 additions & 7 deletions framework/database/marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package database

import (
"context"
jsoniter "github.com/json-iterator/go"

"fmt"
"github.com/bytedance/sonic"
"github.com/samber/lo"
"github.com/unionj-cloud/go-doudou/v2/toolkit/caches"
"github.com/unionj-cloud/go-doudou/v2/toolkit/gocache/lib/cache"
"github.com/unionj-cloud/go-doudou/v2/toolkit/gocache/lib/store"
"github.com/unionj-cloud/go-doudou/v2/toolkit/reflectutils"
"reflect"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

// Marshaler is the struct that marshal and unmarshal cache values
type Marshaler struct {
cache cache.CacheInterface[any]
Expand All @@ -31,9 +33,9 @@ func (c *Marshaler) Get(ctx context.Context, key any, returnObj any) (any, error

switch v := result.(type) {
case []byte:
err = json.Unmarshal(v, returnObj)
err = sonic.Unmarshal(v, returnObj)
case string:
err = json.Unmarshal([]byte(v), returnObj)
err = sonic.Unmarshal([]byte(v), returnObj)
}

if err != nil {
Expand All @@ -45,7 +47,24 @@ func (c *Marshaler) Get(ctx context.Context, key any, returnObj any) (any, error

// Set sets a value in cache by marshaling value
func (c *Marshaler) Set(ctx context.Context, key, object any, options ...store.Option) error {
bytes, err := json.Marshal(object)
query := object.(*caches.Query)
source := reflectutils.ValueOf(query.Dest).Interface()
t := fmt.Sprintf("%T", source)
if t == "map[string]interface {}" {
query.Dest = lo.OmitBy[string, interface{}](source.(map[string]interface{}), func(key string, value interface{}) bool {
return value == nil || reflect.ValueOf(value).IsZero()
})
} else if t == "[]map[string]interface {}" {
rows := source.([]map[string]interface{})
_rows := make([]map[string]interface{}, len(rows))
lo.ForEach[map[string]interface{}](rows, func(item map[string]interface{}, index int) {
_rows[index] = lo.OmitBy[string, interface{}](item, func(key string, value interface{}) bool {
return value == nil || reflect.ValueOf(value).IsZero()
})
})
query.Dest = _rows
}
bytes, err := sonic.Marshal(query)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions framework/grpcx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (srv *GrpcServer) RunWithPipe(pipe net.Listener) {
// performed if the parent doesn't exit.
time.AfterFunc(config.GddConfig.GraceTimeout, func() {
logger.Error().Msg("Graceful shutdown timed out")
config.Shutdown()
os.Exit(1)
})
register.ShutdownGrpc()
Expand Down
3 changes: 2 additions & 1 deletion framework/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,13 @@ func (srv *RestServer) Run() {
}
srv.Serve(ln)
defer func() {
logger.Info().Msgf("Grpc server is gracefully shutting down in %s", config.GddConfig.GraceTimeout)
logger.Info().Msgf("Rest server is gracefully shutting down in %s", config.GddConfig.GraceTimeout)
// Make sure to set a deadline on exiting the process
// after upg.Exit() is closed. No new upgrades can be
// performed if the parent doesn't exit.
time.AfterFunc(config.GddConfig.GraceTimeout, func() {
logger.Error().Msg("Graceful shutdown timed out")
config.Shutdown()
os.Exit(1)
})
register.ShutdownRest()
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ require (
dario.cat/mergo v1.0.0 // indirect
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.23.2 // indirect
github.com/bytedance/sonic v1.12.1 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cockroachdb/apd v1.1.1-0.20181017181144-bced77f817b4 // indirect
github.com/cockroachdb/errors v1.8.2 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
Expand All @@ -102,6 +106,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand All @@ -117,8 +122,10 @@ require (
github.com/segmentio/asm v1.2.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
golang.org/x/arch v0.1.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
Expand Down
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@ github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/bytedance/sonic v1.12.1 h1:jWl5Qz1fy7X1ioY74WqO0KjAMtAGQs4sYnjiEBiyX24=
github.com/bytedance/sonic v1.12.1/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
Expand All @@ -749,6 +754,10 @@ github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -1297,11 +1306,14 @@ github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHU
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand Down Expand Up @@ -1690,6 +1702,8 @@ github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+F
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
Expand Down Expand Up @@ -1827,6 +1841,7 @@ go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/arch v0.1.0 h1:oMxhUYsO9VsR1dcoVUjJjIGhx1LXol3989T/yZ59Xsw=
golang.org/x/arch v0.1.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down Expand Up @@ -2701,6 +2716,7 @@ modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw
modernc.org/tcl v1.13.1/go.mod h1:XOLfOwzhkljL4itZkK6T72ckMgvj0BDsnKNdZVUOecw=
modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.5.1/go.mod h1:eWFB510QWW5Th9YGZT81s+LwvaAs3Q2yr4sP0rmLkv8=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
2 changes: 1 addition & 1 deletion toolkit/zlogger/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func InitEntry(lc LoggerConfig) {
if lc.Writer != nil {
output = lc.Writer
} else if lc.Dev {
output = zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: constants.FORMAT}
output = zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: constants.FORMAT, NoColor: true}
} else {
output = os.Stdout
}
Expand Down

0 comments on commit 5b3b915

Please sign in to comment.