Skip to content
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

support gzip and h2c #362

Closed
wants to merge 4 commits into from
Closed
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
20 changes: 9 additions & 11 deletions client/ehttp/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 设置包名
Expand All @@ -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)
}
Expand Down
30 changes: 14 additions & 16 deletions client/ehttp/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ehttp

import (
"net/http"
"regexp"
"runtime"
"time"
Expand All @@ -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 ...
Expand Down
13 changes: 1 addition & 12 deletions client/ehttp/options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ehttp

import (
"net/http"
"time"
)
import "time"

// WithAddr 设置HTTP地址
func WithAddr(addr string) Option {
Expand Down Expand Up @@ -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
}
}
28 changes: 0 additions & 28 deletions core/elog/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion core/elog/elog_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
33 changes: 0 additions & 33 deletions core/elog/stdout_writer_builder.go

This file was deleted.

26 changes: 17 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading