diff --git a/client/ehttp/component.go b/client/ehttp/component.go index 63ee1783..cfe0c0d2 100644 --- a/client/ehttp/component.go +++ b/client/ehttp/component.go @@ -3,15 +3,18 @@ package ehttp import ( "net" "net/http" + "net/http/cookiejar" "net/url" "strings" "time" "github.com/go-resty/resty/v2" "github.com/gotomicro/ego/client/ehttp/resolver" + "github.com/gotomicro/ego/core/eregistry" + "golang.org/x/net/publicsuffix" + "github.com/gotomicro/ego/core/eapp" "github.com/gotomicro/ego/core/elog" - "github.com/gotomicro/ego/core/eregistry" ) // PackageName 设置包名 @@ -33,28 +36,23 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen if err != nil { elog.Panic("parse addr error", elog.FieldErr(err), elog.FieldKey(config.Addr)) } - // 这里的目的是为了,将k8s:// 替换为 http://,所以需要判断下是否为非HTTP,HTTPS。 - // 因为resty默认只要http和https的协议 - addr := config.Addr - if egoTarget.Scheme != "http" && egoTarget.Scheme != "https" { - // 因为内部协议,都是内网,所以直接替换为HTTP - addr = strings.ReplaceAll(config.Addr, egoTarget.Scheme+"://", "http://") - } + addr := strings.ReplaceAll(config.Addr, egoTarget.Scheme+"://", "http://") builder := resolver.Get(egoTarget.Scheme) - resolverBuild, err := builder.Build(addr) + resolver, err := builder.Build(addr) if err != nil { elog.Panic("build resolver error", elog.FieldErr(err), elog.FieldKey(config.Addr)) } // resty的默认方法,无法设置长连接个数,和是否开启长连接,这里重新构造http client。 + cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) interceptors := []interceptor{fixedInterceptor, logInterceptor, metricInterceptor, traceInterceptor} - cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: config.cookieJar}). + cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: cookieJar}). SetDebug(config.RawDebug). SetTimeout(config.ReadTimeout). SetHeader("app", eapp.Name()). SetBaseURL(addr) for _, interceptor := range interceptors { - onBefore, onAfter, onErr := interceptor(name, config, logger, resolverBuild) + onBefore, onAfter, onErr := interceptor(name, config, logger, resolver) if onBefore != nil { cli.OnBeforeRequest(onBefore) } diff --git a/client/ehttp/config.go b/client/ehttp/config.go index 4b5d2a41..e34b2b0e 100644 --- a/client/ehttp/config.go +++ b/client/ehttp/config.go @@ -1,7 +1,6 @@ package ehttp import ( - "net/http" "regexp" "runtime" "time" @@ -11,21 +10,20 @@ import ( // Config HTTP配置选项 type Config struct { - Addr string // 连接地址 - Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据 - RawDebug bool // 是否开启原生调试,默认不开启 - ReadTimeout time.Duration // 读超时,默认2s - SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms - IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second - MaxIdleConns int // 设置最大空闲连接数 - MaxIdleConnsPerHost int // 设置长连接个数 - EnableTraceInterceptor bool // 是否开启链路追踪,默认开启 - EnableKeepAlives bool // 是否开启长连接,默认打开 - EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启 - EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启 - PathRelabel []Relabel // path 重命名 (metric 用) - cookieJar http.CookieJar // 用于缓存cookie - EnableMetricsInterceptor bool // 是否开启监控,默认开启 + Addr string // 连接地址 + Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据 + RawDebug bool // 是否开启原生调试,默认不开启 + ReadTimeout time.Duration // 读超时,默认2s + SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms + IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second + MaxIdleConns int // 设置最大空闲连接数 + MaxIdleConnsPerHost int // 设置长连接个数 + EnableTraceInterceptor bool // 是否开启链路追踪,默认开启 + EnableKeepAlives bool // 是否开启长连接,默认打开 + EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启 + EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启 + PathRelabel []Relabel // path 重命名 (metric 用) + EnableMetricsInterceptor bool // 是否开启监控,默认开启 } // Relabel ... diff --git a/client/ehttp/options.go b/client/ehttp/options.go index d88cb575..49962683 100644 --- a/client/ehttp/options.go +++ b/client/ehttp/options.go @@ -1,9 +1,6 @@ package ehttp -import ( - "net/http" - "time" -) +import "time" // WithAddr 设置HTTP地址 func WithAddr(addr string) Option { @@ -102,11 +99,3 @@ func WithPathRelabel(match string, replacement string) Option { c.config.PathRelabel = append(c.config.PathRelabel, Relabel{Match: match, Replacement: replacement}) } } - -// WithJar 设置Cookie,设置后,请求第一次接口后获取Cookie,第二次请求会带上Cookie,适合一些登录场景 -// 例子:cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) -func WithJar(jar http.CookieJar) Option { - return func(c *Container) { - c.config.cookieJar = jar - } -} diff --git a/core/elog/component_test.go b/core/elog/component_test.go index 29f37bfc..8430874c 100644 --- a/core/elog/component_test.go +++ b/core/elog/component_test.go @@ -73,21 +73,6 @@ writer = "stderr" return Load("stderr").Build() } -func newStdoutLogger() *Component { - conf := ` -[stdout] -level = "info" -writer = "stdout" -` - var err error - if err = econf.LoadFromReader(strings.NewReader(conf), toml.Unmarshal); err != nil { - log.Println("load conf fail", err) - return nil - } - log.Println("start to send logs to stdout") - return Load("stdout").Build() -} - func newAliLogger() *Component { conf := ` [ali] @@ -169,19 +154,6 @@ func BenchmarkStderrWriter(b *testing.B) { }) } -func BenchmarkStdoutWriter(b *testing.B) { - b.Logf("Logging at a disabled level with some accumulated context.") - logger := newStdoutLogger() - b.Run("stdout\n", func(b *testing.B) { - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - logger.Info(getMessage(0)) - } - }) - }) -} - func BenchmarkZapWriter(b *testing.B) { b.Logf("Logging at a disabled level with some accumulated context.") logger := newZapLogger() diff --git a/core/elog/elog_api.go b/core/elog/elog_api.go index 1575be76..a3938e62 100644 --- a/core/elog/elog_api.go +++ b/core/elog/elog_api.go @@ -13,7 +13,6 @@ func init() { registry = make(map[string]WriterBuilder) Register(&stderrWriterBuilder{}) Register(&rotateWriterBuilder{}) - Register(&stdoutWriterBuilder{}) DefaultLogger = DefaultContainer().Build(WithFileName(DefaultLoggerName), WithCallSkip(2)) // DefaultLogger 默认为2层 EgoLogger = DefaultContainer().Build(WithFileName(EgoLoggerName)) } diff --git a/core/elog/stdout_writer_builder.go b/core/elog/stdout_writer_builder.go deleted file mode 100644 index db00e9d3..00000000 --- a/core/elog/stdout_writer_builder.go +++ /dev/null @@ -1,33 +0,0 @@ -package elog - -import ( - "go.uber.org/zap/zapcore" - "io" - "os" -) - -const ( - writerStdout = "stdout" -) - -var _ WriterBuilder = &stdoutWriterBuilder{} - -// fileDataSource file Provider. -type stdoutWriterBuilder struct{} - -type stdoutWriter struct { - zapcore.Core - io.Closer -} - -func (s *stdoutWriterBuilder) Build(key string, c *Config) Writer { - // Debug output to console and file by default - w := &stdoutWriter{} - w.Core = zapcore.NewCore(zapcore.NewJSONEncoder(*c.EncoderConfig()), os.Stdout, c.AtomicLevel()) - w.Closer = CloseFunc(noopCloseFunc) - return w -} - -func (*stdoutWriterBuilder) Scheme() string { - return writerStdout -} diff --git a/go.mod b/go.mod index 7f914dc8..70323584 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/fasthttp/websocket v1.5.2 github.com/felixge/fgprof v0.9.2 github.com/fsnotify/fsnotify v1.5.4 - github.com/gin-gonic/gin v1.7.7 + github.com/gin-gonic/gin v1.9.1 github.com/go-resty/resty/v2 v2.7.0 github.com/google/cel-go v0.11.3 github.com/gotomicro/logrotate v0.0.0-20211108034117-46d53eedc960 @@ -34,12 +34,11 @@ require ( go.opentelemetry.io/otel/trace v1.18.0 go.uber.org/automaxprocs v1.5.1 go.uber.org/zap v1.21.0 - golang.org/x/net v0.12.0 golang.org/x/sync v0.3.0 golang.org/x/tools v0.9.1 google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 - google.golang.org/grpc v1.58.1 + google.golang.org/grpc v1.58.0 google.golang.org/protobuf v1.31.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -49,25 +48,31 @@ require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed // indirect github.com/beorn7/perks v1.0.1 // indirect + github.com/bytedance/sonic v1.9.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-logr/logr v1.2.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect - github.com/go-playground/locales v0.13.0 // indirect - github.com/go-playground/universal-translator v0.17.0 // indirect - github.com/go-playground/validator/v10 v10.4.1 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect github.com/google/uuid v1.3.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect github.com/klauspost/compress v1.16.3 // indirect - github.com/leodido/go-urn v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect @@ -78,15 +83,18 @@ require ( github.com/stoewer/go-strcase v1.2.0 // indirect github.com/tklauser/go-sysconf v0.3.6 // indirect github.com/tklauser/numcpus v0.2.2 // indirect - github.com/ugorji/go/codec v1.1.7 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.45.0 // indirect go.opentelemetry.io/otel/metric v1.18.0 // indirect go.opentelemetry.io/proto/otlp v1.0.0 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect + golang.org/x/arch v0.3.0 // indirect golang.org/x/crypto v0.11.0 // indirect golang.org/x/mod v0.10.0 // indirect + golang.org/x/net v0.12.0 // indirect golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.11.0 // indirect google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect diff --git a/go.sum b/go.sum index f4728e10..b3e8028e 100644 --- a/go.sum +++ b/go.sum @@ -71,6 +71,9 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= +github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= @@ -80,6 +83,9 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -125,11 +131,13 @@ github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs= -github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -148,18 +156,19 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= -github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= -github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= -github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= -github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= -github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= -github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= -github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= +github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -279,7 +288,6 @@ github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= @@ -293,6 +301,9 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= +github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= 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/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= @@ -301,16 +312,16 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= -github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -358,6 +369,8 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= +github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac= github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= @@ -438,13 +451,20 @@ github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3 github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4= @@ -452,9 +472,10 @@ github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITn github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA= github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= -github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +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/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -515,6 +536,9 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= golang.org/x/arch v0.0.0-20180920145803-b19384d3c130/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= +golang.org/x/arch v0.3.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= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -642,7 +666,6 @@ golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -669,6 +692,8 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -816,8 +841,8 @@ google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3Iji google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.58.1 h1:OL+Vz23DTtrrldqHK49FUOPHyY75rvFqJfXC84NYW58= -google.golang.org/grpc v1.58.1/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/grpc v1.58.0 h1:32JY8YpPMSR45K+c3o6b8VL73V+rR8k+DeMIr4vRH8o= +google.golang.org/grpc v1.58.0/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -867,6 +892,7 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= 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= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= diff --git a/server/egin/component.go b/server/egin/component.go index 651b922f..e0ccb1b7 100644 --- a/server/egin/component.go +++ b/server/egin/component.go @@ -52,10 +52,6 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen listener: nil, routerCommentMap: make(map[string]string), } - // 判断是否存在自定义listener - if config.listener != nil { - comp.listener = config.listener - } if config.EmbedPath != "" { comp.embedWrapper = &EmbedWrapper{ @@ -66,6 +62,7 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen // 设置信任的header头 comp.Engine.TrustedPlatform = config.TrustedPlatform + comp.UseH2C = config.EnableH2C return comp } @@ -92,18 +89,6 @@ func (c *Component) Prepare() error { // Init 初始化 func (c *Component) Init() error { - // 如果没有配置listener - if c.config.listener == nil { - if err := c.defaultListener(); err != nil { - return err - } - } - - c.config.Port = c.listener.Addr().(*net.TCPAddr).Port - return nil -} - -func (c *Component) defaultListener() error { var err error if c.config.Network == "local" { c.listener = newLocalListener() @@ -113,6 +98,7 @@ func (c *Component) defaultListener() error { c.logger.Panic("new egin server err", elog.FieldErrKind("listen err"), elog.FieldErr(err)) } } + c.config.Port = c.listener.Addr().(*net.TCPAddr).Port return nil } diff --git a/server/egin/config.go b/server/egin/config.go index 23951547..d73dbe10 100644 --- a/server/egin/config.go +++ b/server/egin/config.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "embed" "fmt" - "net" "net/http" "sync" "time" @@ -49,6 +48,7 @@ type Config struct { TLSClientCAs []string // https client的ca,当需要双向认证的时候指定可以倒入自签证书 TrustedPlatform string // 需要用户换成自己的CDN名字,获取客户端IP地址 EmbedPath string // 嵌入embed path数据 + EnableH2C bool // 开启HTTP2 embedFs embed.FS // 需要在build时候注入embed.Fs TLSSessionCache tls.ClientSessionCache blockFallback func(*gin.Context) @@ -56,7 +56,6 @@ type Config struct { aiReqResCelPrg cel.Program mu sync.RWMutex // mutex for EnableAccessInterceptorReq、EnableAccessInterceptorRes、AccessInterceptorReqResFilter、aiReqResCelPrg recoveryFunc gin.RecoveryFunc // recoveryFunc 处理接口没有被 recover 的 panic,默认返回 500 并且没有任何 response body - listener net.Listener // a generic network listener 默认是net.Listen()方法生成,如果有需要自行传入可采用option方式进行替换 } // DefaultConfig ... diff --git a/server/egin/interceptor.go b/server/egin/interceptor.go index 89fe3287..392d7365 100644 --- a/server/egin/interceptor.go +++ b/server/egin/interceptor.go @@ -312,17 +312,16 @@ func function(pc uintptr) []byte { return name } -func (c *Container) metricServerInterceptor(ctx *gin.Context, cost time.Duration) { +func (c *Container) metricServerInterceptor(ctx *gin.Context, value time.Duration) { if !c.config.EnableMetricInterceptor { return } - + cost := float64(value.Microseconds()) / 1000 host := ctx.Request.Host method := ctx.Request.Method + "." + ctx.FullPath() app := extractAPP(ctx) emetric.ServerStartedCounter.Inc(emetric.TypeHTTP, method, app, host) - // HandleHistogram的单位是s,需要用s单位 - emetric.ServerHandleHistogram.ObserveWithExemplar(cost.Seconds(), prometheus.Labels{ + emetric.ServerHandleHistogram.ObserveWithExemplar(cost, prometheus.Labels{ "tid": etrace.ExtractTraceID(ctx.Request.Context()), }, emetric.TypeHTTP, method, app, host) emetric.ServerHandleCounter.Inc(emetric.TypeHTTP, method, app, http.StatusText(ctx.Writer.Status()), strconv.Itoa(ctx.Writer.Status()), host) diff --git a/server/egin/interceptor_gzip.go b/server/egin/interceptor_gzip.go new file mode 100644 index 00000000..07f2aeaa --- /dev/null +++ b/server/egin/interceptor_gzip.go @@ -0,0 +1,226 @@ +package egin + +import ( + "compress/gzip" + "fmt" + "io" + "net/http" + "path/filepath" + "regexp" + "strings" + "sync" + + "github.com/gin-gonic/gin" +) + +const ( + BestCompression = gzip.BestCompression + BestSpeed = gzip.BestSpeed + DefaultCompression = gzip.DefaultCompression + NoCompression = gzip.NoCompression +) + +func Gzip(level int, options ...GzipOption) gin.HandlerFunc { + return newGzipHandler(level, options...).Handle +} + +type gzipWriter struct { + gin.ResponseWriter + writer *gzip.Writer +} + +func (g *gzipWriter) WriteString(s string) (int, error) { + g.Header().Del("Content-Length") + return g.writer.Write([]byte(s)) +} + +func (g *gzipWriter) Write(data []byte) (int, error) { + g.Header().Del("Content-Length") + return g.writer.Write(data) +} + +// Fix: https://github.com/mholt/caddy/issues/38 +func (g *gzipWriter) WriteHeader(code int) { + g.Header().Del("Content-Length") + g.ResponseWriter.WriteHeader(code) +} + +// ---------------- + +type gzipHandler struct { + *GzipOptions + gzPool sync.Pool +} + +func newGzipHandler(level int, options ...GzipOption) *gzipHandler { + handler := &gzipHandler{ + GzipOptions: DefaultOptions, + gzPool: sync.Pool{ + New: func() interface{} { + gz, err := gzip.NewWriterLevel(io.Discard, level) + if err != nil { + panic(err) + } + return gz + }, + }, + } + for _, setter := range options { + setter(handler.GzipOptions) + } + return handler +} + +func (g *gzipHandler) Handle(c *gin.Context) { + if fn := g.DecompressFn; fn != nil && c.Request.Header.Get("Content-Encoding") == "gzip" { + fn(c) + } + + if !g.shouldCompress(c.Request) { + return + } + + gz := g.gzPool.Get().(*gzip.Writer) + defer g.gzPool.Put(gz) + defer gz.Reset(io.Discard) + gz.Reset(c.Writer) + + c.Header("Content-Encoding", "gzip") + c.Header("Vary", "Accept-Encoding") + c.Writer = &gzipWriter{c.Writer, gz} + defer func() { + gz.Close() + c.Header("Content-Length", fmt.Sprint(c.Writer.Size())) + }() + c.Next() +} + +func (g *gzipHandler) shouldCompress(req *http.Request) bool { + if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") || + strings.Contains(req.Header.Get("Connection"), "Upgrade") || + strings.Contains(req.Header.Get("Accept"), "text/event-stream") { + return false + } + + extension := filepath.Ext(req.URL.Path) + if g.ExcludedExtensions.Contains(extension) { + return false + } + + if g.ExcludedPaths.Contains(req.URL.Path) { + return false + } + if g.ExcludedPathesRegexs.Contains(req.URL.Path) { + return false + } + + return true +} + +// --------- ----- + +var ( + DefaultExcludedExtentions = NewExcludedExtensions([]string{ + ".png", ".gif", ".jpeg", ".jpg", + }) + DefaultOptions = &GzipOptions{ + ExcludedExtensions: DefaultExcludedExtentions, + } +) + +type GzipOptions struct { + ExcludedExtensions ExcludedExtensions + ExcludedPaths ExcludedPaths + ExcludedPathesRegexs ExcludedPathesRegexs + DecompressFn func(c *gin.Context) +} + +type GzipOption func(*GzipOptions) + +func WithGzipExcludedExtensions(args []string) GzipOption { + return func(o *GzipOptions) { + o.ExcludedExtensions = NewExcludedExtensions(args) + } +} + +func WithGzipExcludedPaths(args []string) GzipOption { + return func(o *GzipOptions) { + o.ExcludedPaths = NewExcludedPaths(args) + } +} + +func WithGzipExcludedPathsRegexs(args []string) GzipOption { + return func(o *GzipOptions) { + o.ExcludedPathesRegexs = NewExcludedPathesRegexs(args) + } +} + +func WithGzipDecompressFn(decompressFn func(c *gin.Context)) GzipOption { + return func(o *GzipOptions) { + o.DecompressFn = decompressFn + } +} + +// Using map for better lookup performance +type ExcludedExtensions map[string]bool + +func NewExcludedExtensions(extensions []string) ExcludedExtensions { + res := make(ExcludedExtensions) + for _, e := range extensions { + res[e] = true + } + return res +} + +func (e ExcludedExtensions) Contains(target string) bool { + _, ok := e[target] + return ok +} + +type ExcludedPaths []string + +func NewExcludedPaths(paths []string) ExcludedPaths { + return ExcludedPaths(paths) +} + +func (e ExcludedPaths) Contains(requestURI string) bool { + for _, path := range e { + if strings.HasPrefix(requestURI, path) { + return true + } + } + return false +} + +type ExcludedPathesRegexs []*regexp.Regexp + +func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs { + result := make([]*regexp.Regexp, len(regexs)) + for i, reg := range regexs { + result[i] = regexp.MustCompile(reg) + } + return result +} + +func (e ExcludedPathesRegexs) Contains(requestURI string) bool { + for _, reg := range e { + if reg.MatchString(requestURI) { + return true + } + } + return false +} + +func DefaultDecompressHandle(c *gin.Context) { + if c.Request.Body == nil { + return + } + r, err := gzip.NewReader(c.Request.Body) + if err != nil { + _ = c.AbortWithError(http.StatusBadRequest, err) + return + } + c.Request.Header.Del("Content-Encoding") + c.Request.Header.Del("Content-Length") + c.Request.Body = r +} diff --git a/server/egin/interceptor_test.go b/server/egin/interceptor_test.go index d74fd61b..79f8786b 100644 --- a/server/egin/interceptor_test.go +++ b/server/egin/interceptor_test.go @@ -103,21 +103,6 @@ func TestPanicInCustomHandler(t *testing.T) { os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) } -func TestBind(t *testing.T) { - container := DefaultContainer() - cmp := container.Build() - type User struct { - Name string `json:"name" form:"name"` - } - // 异常header会导致EOF问题 - cmp.POST("/hello", func(c *gin.Context) { - err := c.Bind(&User{}) - assert.NoError(t, err) - }) - // RUN - performRequest(cmp, "POST", "/hello") -} - func TestPanicWithBrokenPipe(t *testing.T) { const expectCode = 204 diff --git a/server/egin/options.go b/server/egin/options.go index cccb4756..dd3fc0b5 100644 --- a/server/egin/options.go +++ b/server/egin/options.go @@ -3,7 +3,6 @@ package egin import ( "crypto/tls" "embed" - "net" "time" "github.com/gin-gonic/gin" @@ -114,9 +113,3 @@ func WithRecoveryFunc(f gin.RecoveryFunc) Option { c.config.recoveryFunc = f } } - -func WithListener(listener net.Listener) Option { - return func(c *Container) { - c.config.listener = listener - } -} diff --git a/server/egin/options_test.go b/server/egin/options_test.go index c40dfcd3..2c0ab77f 100644 --- a/server/egin/options_test.go +++ b/server/egin/options_test.go @@ -1,9 +1,6 @@ package egin import ( - "io" - "net/http" - "net/http/httptest" "testing" "time" @@ -95,15 +92,3 @@ func TestWithContextTimeout(t *testing.T) { comp := DefaultContainer().Build(WithContextTimeout(timeout)) assert.Equal(t, timeout, comp.config.ContextTimeout) } - -func TestWithListener(t *testing.T) { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // 处理http请求 - io.WriteString(w, "Hello,Client") - })) - defer server.Close() - - lister := server.Listener - comp := DefaultContainer().Build(WithListener(lister)) - assert.Equal(t, lister, comp.config.listener) -} diff --git a/server/egrpc/interceptor.go b/server/egrpc/interceptor.go index f436c6ef..f140a7e6 100644 --- a/server/egrpc/interceptor.go +++ b/server/egrpc/interceptor.go @@ -198,11 +198,11 @@ func (c *Container) defaultStreamServerInterceptor() grpc.StreamServerIntercepto } } -func (c *Container) prometheusStreamServerInterceptor(ss grpc.ServerStream, info *grpc.StreamServerInfo, pbStatus *status.Status, cost time.Duration) { +func (c *Container) prometheusStreamServerInterceptor(ss grpc.ServerStream, info *grpc.StreamServerInfo, pbStatus *status.Status, dur time.Duration) { + cost := float64(dur.Microseconds()) / 1000 serviceName, _ := egrpcinteceptor.SplitMethodName(info.FullMethod) emetric.ServerStartedCounter.Inc(emetric.TypeGRPCStream, info.FullMethod, getPeerName(ss.Context()), serviceName) - // HandleHistogram的单位是s,需要用s单位 - emetric.ServerHandleHistogram.Observe(cost.Seconds(), emetric.TypeGRPCStream, info.FullMethod, getPeerName(ss.Context()), serviceName) + emetric.ServerHandleHistogram.Observe(cost, emetric.TypeGRPCStream, info.FullMethod, getPeerName(ss.Context()), serviceName) emetric.ServerHandleCounter.Inc(emetric.TypeGRPCStream, info.FullMethod, getPeerName(ss.Context()), pbStatus.Message(), strconv.Itoa(ecode.GrpcToHTTPStatusCode(pbStatus.Code())), serviceName) } @@ -335,14 +335,14 @@ func (c *Container) defaultUnaryServerInterceptor() grpc.UnaryServerInterceptor } } -func (c *Container) prometheusUnaryServerInterceptor(ctx context.Context, info *grpc.UnaryServerInfo, pbStatus *status.Status, cost time.Duration) { +func (c *Container) prometheusUnaryServerInterceptor(ctx context.Context, info *grpc.UnaryServerInfo, pbStatus *status.Status, dur time.Duration) { if !c.config.EnableMetricInterceptor { return } + cost := float64(dur.Microseconds()) / 1000 serviceName, _ := egrpcinteceptor.SplitMethodName(info.FullMethod) emetric.ServerStartedCounter.Inc(emetric.TypeGRPCUnary, info.FullMethod, getPeerName(ctx), serviceName) - // HandleHistogram的单位是s,需要用s单位 - emetric.ServerHandleHistogram.ObserveWithExemplar(cost.Seconds(), prometheus.Labels{ + emetric.ServerHandleHistogram.ObserveWithExemplar(cost, prometheus.Labels{ "tid": etrace.ExtractTraceID(ctx), }, emetric.TypeGRPCUnary, info.FullMethod, getPeerName(ctx), serviceName) emetric.ServerHandleCounter.Inc(emetric.TypeGRPCUnary, info.FullMethod, getPeerName(ctx), pbStatus.Code().String(), strconv.Itoa(ecode.GrpcToHTTPStatusCode(pbStatus.Code())), serviceName)