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

Feat: add grpc gateway http rule #36

Merged
merged 42 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3f1ab99
fix: 2024-06-20 22:34:33
kooksee Jun 20, 2024
a5af67e
fix: 2024-06-21 11:59:51
kooksee Jun 21, 2024
9658fd2
fix: 2024-06-21 12:00:02
kooksee Jun 21, 2024
343aeb9
fix: 2024-06-21 14:23:34
kooksee Jun 21, 2024
0e87540
fix: 2024-06-21 22:38:28
kooksee Jun 21, 2024
5697478
fix: 2024-06-22 21:30:43
kooksee Jun 22, 2024
d47a633
fix: 2024-06-22 21:30:52
kooksee Jun 22, 2024
ab55c34
fix: 2024-06-29 23:16:10
kooksee Jun 29, 2024
1645c68
fix: 2024-06-30 17:14:41
kooksee Jun 30, 2024
ac25019
fix: 2024-06-30 17:14:54
kooksee Jun 30, 2024
64d02fe
fix: 2024-06-30 20:50:58
kooksee Jun 30, 2024
a5436f4
fix: 2024-06-30 20:55:00
kooksee Jun 30, 2024
9b9db20
fix: 2024-06-30 21:58:36
kooksee Jun 30, 2024
f7ac2c4
fix route match
kooksee Jul 7, 2024
feb448b
Merge branch 'master' of github.com:pubgo/lava into feat/http-rule
kooksee Jul 9, 2024
fe0709e
fix: barry 2024-07-11 22:55:24
kooksee Jul 11, 2024
e9cd352
fix: barry 2024-07-11 23:26:11
kooksee Jul 11, 2024
f62b709
fix: barry 2024-07-11 23:32:03
kooksee Jul 11, 2024
c02cc8c
fix: barry 2024-07-13 10:48:34
kooksee Jul 13, 2024
a58e527
fix: barry 2024-07-13 11:17:28
kooksee Jul 13, 2024
5ed78e6
fix: barry 2024-07-13 11:22:01
kooksee Jul 13, 2024
dcdf2a1
fix: barry 2024-07-13 11:24:17
kooksee Jul 13, 2024
7a1beff
fix: barry 2024-07-13 19:46:35
kooksee Jul 13, 2024
c4d8bda
fix: barry 2024-07-13 20:03:06
kooksee Jul 13, 2024
9da823a
fix: barry 2024-07-18 23:10:00
kooksee Jul 18, 2024
d433cf9
fix: barry 2024-07-19 11:43:30
kooksee Jul 19, 2024
9da71fd
fix: barry 2024-07-23 15:54:52
kooksee Jul 23, 2024
f6e2e0b
fix: barry 2024-07-24 13:39:43
kooksee Jul 24, 2024
3c8796f
fix: barry 2024-07-24 21:42:35
kooksee Jul 24, 2024
1f9e55b
fix: barry 2024-07-24 22:24:43
kooksee Jul 24, 2024
1aa8b6b
fix: barry 2024-07-24 22:34:38
kooksee Jul 24, 2024
08409d4
fix: barry 2024-07-26 01:10:27
kooksee Jul 25, 2024
19c5475
fix: barry 2024-07-26 13:43:46
kooksee Jul 26, 2024
fffd89e
fix: barry 2024-07-26 23:36:29
kooksee Jul 26, 2024
a04ba2f
fix: barry 2024-07-26 23:48:08
kooksee Jul 26, 2024
1e13fd4
fix: barry 2024-07-26 23:48:55
kooksee Jul 26, 2024
7798b1e
fix: barry 2024-07-26 23:56:26
kooksee Jul 26, 2024
1bb7221
fix: barry 2024-07-27 00:02:11
kooksee Jul 26, 2024
a648568
fix: barry 2024-07-27 00:08:02
kooksee Jul 26, 2024
23a83ee
fix: barry 2024-07-27 00:08:14
kooksee Jul 26, 2024
8ab209e
fix: barry 2024-07-27 13:36:41
kooksee Jul 27, 2024
77f2fd6
fix: barry 2024-07-29 21:19:41
kooksee Jul 29, 2024
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
68 changes: 34 additions & 34 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,44 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
skip-go-installation: true
args: --timeout 3m --verbose
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
skip-go-installation: true
args: --timeout 3m --verbose

test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22

- name: Collect dependencies
run: |
go mod download
go install gotest.tools/gotestsum@latest

- name: Run unit tests
run: gotestsum --junitfile unit-tests.xml -- -gcflags=-l ./...

- name: Test report
uses: dorny/test-reporter@v1
if: success() || failure()
with:
name: Test report
path: unit-tests.xml
reporter: java-junit
fail-on-error: false
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22

- name: Collect dependencies
run: |
go mod download
go install gotest.tools/gotestsum@latest

- name: Run unit tests
run: gotestsum --junitfile unit-tests.xml -- -gcflags=-l ./...

- name: Test report
uses: dorny/test-reporter@v1
if: success() || failure()
with:
name: Test report
path: unit-tests.xml
reporter: java-junit
fail-on-error: false
19 changes: 10 additions & 9 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ linters:
fast: true
disable-all: true
enable:
- bodyclose # checks whether HTTP response body is closed successfully
# - bodyclose # checks whether HTTP response body is closed successfully
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- gosimple # Linter for Go source code that specializes in simplifying a code
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- ineffassign # Detects when assignments to existing variables are not used
- dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f())
- goconst # Finds repeated strings that could be replaced by a constant
# - ineffassign # Detects when assignments to existing variables are not used
# - dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f())
# - goconst # Finds repeated strings that could be replaced by a constant
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
- goprintffuncname # Checks that printf-like functions are named with f at the end
- nolintlint # Reports ill-formed or insufficient nolint directives
- rowserrcheck # checks whether Err of rows is checked successfully
# - goprintffuncname # Checks that printf-like functions are named with f at the end
# - nolintlint # Reports ill-formed or insufficient nolint directives
# - rowserrcheck # checks whether Err of rows is checked successfully
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- unconvert # Remove unnecessary type conversions
- unparam # Reports unused function parameters
# - unconvert # Remove unnecessary type conversions
# - unparam # Reports unused function parameters
# - unused # Checks Go code for unused constants, variables, functions and types

issues:
exclude-dirs:
- internal/example
- cmds
- vendor
- pkg/proto
exclude-generated: strict
exclude-dirs-use-default: true
exclude-case-sensitive: false
Expand Down
49 changes: 6 additions & 43 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,20 @@ LDFLAGS=-ldflags " \
-X '${Base}/version.project=${Project}' \
"

.PHONY: build
build:
go build ${LDFLAGS} -mod vendor -v -o bin/main cmd/lava/main.go

.PHONY: install
install:
@go build ${LDFLAGS} -mod vendor -tags trace -o lava -v cmd/lava/*.go
@mv lava ${GOPATH}/bin/lava

build_hello_test:
@go build ${LDFLAGS} -mod vendor -v -o main example/hello/main.go

.PHONY: test./ma
.PHONY: test
test:
@go test -short -race -v ./... -cover

ci:
@golangci-lint run -v --timeout=5m

.PHONY: gen
proto-plugin-gen:
cd cmd/protoc-gen-errors && go install -v .

.PHONY: example
example:
go build ${LDFLAGS} -mod vendor -v -o main example/*.go

docker:
docker build -t lug .

build-all:
go build -tags "kcp quic" ./...

.PHONY: cover
cover:
gocov test -tags "kcp quic" ./... | gocov-html > cover.html
open cover.html

check-libs:
GIT_TERMINAL_PROMPT=1 GO111MODULE=on go list -m -u all | column -t

update-libs:
GIT_TERMINAL_PROMPT=1 GO111MODULE=on go get -u -v ./...

mod-tidy:
GIT_TERMINAL_PROMPT=1 GO111MODULE=on go mod tidy

.PHONY: vet
vet:
@go vet ./...

.PHONY: generate
generate:
@go generate ./...

Expand All @@ -71,14 +36,12 @@ deps:
@go install -v github.com/tinylib/msgp
@go install -v github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2

lint:
@golangci-lint run --skip-dirs-use-default --timeout 3m0s

.PHONY: protobuf
protobuf:
protobuild vendor
protobuild gen

.PHONY: lint
lint:
golangci-lint --version
golangci-lint run --timeout 3m --verbose
golangci-lint run --timeout 3m --verbose ./...
14 changes: 1 addition & 13 deletions clients/grpcc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
"github.com/pubgo/funk/vars"
"github.com/pubgo/lava/clients/grpcc/grpcc_config"
"github.com/pubgo/lava/core/metrics"
"github.com/pubgo/lava/internal/middlewares/middleware_accesslog"
"github.com/pubgo/lava/internal/middlewares/middleware_metric"
"github.com/pubgo/lava/internal/middlewares/middleware_recovery"
"github.com/pubgo/lava/internal/middlewares/middleware_service_info"
"github.com/pubgo/lava/lava"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
Expand All @@ -33,18 +29,10 @@ func New(cfg *grpcc_config.Cfg, p Params, middlewares ...lava.Middleware) Client
cfg = config.MergeR(grpcc_config.DefaultCfg(), cfg).Unwrap()
cfg.Resolvers = p.Resolvers

defaultMiddlewares := lava.Middlewares{
middleware_service_info.New(),
middleware_metric.New(p.Metric),
middleware_accesslog.New(p.Log.WithFields(log.Map{"service": cfg.Service.Name})),
middleware_recovery.New(),
}
defaultMiddlewares = append(defaultMiddlewares, middlewares...)

c := &clientImpl{
cfg: cfg,
log: p.Log,
middlewares: defaultMiddlewares,
middlewares: middlewares,
}

vars.RegisterValue(fmt.Sprintf("%s-grpc-client-config", cfg.Service.Name), cfg)
Expand Down
11 changes: 5 additions & 6 deletions clients/grpcc/grpcc_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package grpcc_config
import (
"time"

"github.com/pubgo/lava/clients/grpcc/grpcc_resolver"
"google.golang.org/grpc"
"google.golang.org/grpc/resolver"

"github.com/pubgo/lava/clients/grpcc/grpcc_resolver"
)

const (
Expand Down Expand Up @@ -57,20 +56,20 @@ func DefaultCfg() *Cfg {
MaxHeaderListSize: 1024 * 4,
MaxRecvMsgSize: 1024 * 1024 * 4,
// refer: https://github.com/grpc/grpc-go/blob/master/examples/features/keepalive/client/main.go
ClientParameters: clientParameters{
ClientParameters: ClientParameters{
PermitWithoutStream: true, // send pings even without active streams
Time: 10 * time.Second, // send pings every 10 seconds if there is no activity
Timeout: 5 * time.Second, // wait 2 second for ping ack before considering the connection dead
},
ConnectParams: connectParams{
Backoff: backoffConfig{
ConnectParams: ConnectParams{
Backoff: BackoffConfig{
Multiplier: 1.6,
Jitter: 0.2,
BaseDelay: 1.0 * time.Second,
MaxDelay: 120 * time.Second,
},
},
Call: callParameters{
Call: CallParameters{
MaxCallRecvMsgSize: 1024 * 1024 * 4,
// DefaultMaxSendMsgSize maximum message that Service can send (4 MB).
MaxCallSendMsgSize: 1024 * 1024 * 4,
Expand Down
22 changes: 11 additions & 11 deletions clients/grpcc/grpcc_config/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"google.golang.org/grpc/keepalive"
)

type callParameters struct {
type CallParameters struct {
Header map[string]string `json:"header"`
Trailer map[string]string `json:"trailer"`
WaitForReady bool `json:"wait_for_ready"`
Expand All @@ -23,34 +23,34 @@ type callParameters struct {
MaxRetryRPCBufferSize int `json:"max_retry_rpc_buffer_size"`
}

type clientParameters struct {
type ClientParameters struct {
PermitWithoutStream bool `json:"permit_without_stream"`
Time time.Duration `json:"time"`
Timeout time.Duration `json:"timeout"`
}

func (t clientParameters) toClientParameters() keepalive.ClientParameters {
func (t ClientParameters) toClientParameters() keepalive.ClientParameters {
return keepalive.ClientParameters{
PermitWithoutStream: t.PermitWithoutStream,
Time: t.Time,
Timeout: t.Timeout,
}
}

// backoffConfig defines the configuration options for backoff.
type backoffConfig struct {
// BackoffConfig defines the configuration options for backoff.
type BackoffConfig struct {
BaseDelay time.Duration `json:"base_delay"`
Multiplier float64 `json:"multiplier"`
Jitter float64 `json:"jitter"`
MaxDelay time.Duration `json:"max_delay"`
}

type connectParams struct {
Backoff backoffConfig `json:"backoff"`
type ConnectParams struct {
Backoff BackoffConfig `json:"backoff"`
MinConnectTimeout time.Duration `json:"min_connect_timeout"`
}

func (t connectParams) toConnectParams() grpc.ConnectParams {
func (t ConnectParams) toConnectParams() grpc.ConnectParams {
return grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: t.Backoff.BaseDelay,
Expand Down Expand Up @@ -92,9 +92,9 @@ type GrpcClientCfg struct {
MaxRecvMsgSize int `json:"max_recv_msg_size"`
NoProxy bool `json:"no_proxy"`
Proxy bool `json:"proxy"`
ConnectParams connectParams `json:"connect_params"`
ClientParameters clientParameters `json:"client_parameters"`
Call callParameters `json:"call"`
ConnectParams ConnectParams `json:"connect_params"`
ClientParameters ClientParameters `json:"client_parameters"`
Call CallParameters `json:"call"`
}

func (t GrpcClientCfg) ToOpts() []grpc.DialOption {
Expand Down
4 changes: 3 additions & 1 deletion clients/grpcc/grpcc_resolver/register.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package grpcc_resolver

import "google.golang.org/grpc/resolver"

func init() {
// resolver.Register(&directBuilder{})
resolver.Register(NewDirectBuilder())
// resolver.Register(&discoveryBuilder{})
// resolver.Register(kuberesolver.NewBuilder(nil, "kubernetes"))
}
12 changes: 6 additions & 6 deletions clients/grpcc/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strings"
"time"

"github.com/pubgo/funk/errors"

"github.com/pubgo/funk/convert"
"github.com/pubgo/funk/errors"
"github.com/pubgo/funk/strutil"
"github.com/pubgo/lava/clients/grpcc/grpcc_config"
"github.com/pubgo/lava/core/lavacontexts"
"github.com/pubgo/lava/lava"
"github.com/pubgo/lava/pkg/grpcutil"
"github.com/pubgo/lava/pkg/httputil"
Expand Down Expand Up @@ -106,12 +106,12 @@ func unaryInterceptor(middlewares []lava.Middleware) grpc.UnaryClientInterceptor
}

reqId := strutil.FirstFnNotEmpty(
func() string { return lava.GetReqID(ctx) },
func() string { return lavacontexts.GetReqID(ctx) },
func() string { return string(rpcReq.Header().Peek(httputil.HeaderXRequestID)) },
func() string { return xid.New().String() },
)
rpcReq.Header().Set(httputil.HeaderXRequestID, reqId)
ctx = lava.CreateCtxWithReqID(ctx, reqId)
ctx = lavacontexts.CreateCtxWithReqID(ctx, reqId)

_, err = unaryWrapper(ctx, rpcReq)
return errors.WrapCaller(err)
Expand Down Expand Up @@ -173,12 +173,12 @@ func streamInterceptor(middlewares []lava.Middleware) grpc.StreamClientIntercept
md2Head(md, header)

reqId := strutil.FirstFnNotEmpty(
func() string { return lava.GetReqID(ctx) },
func() string { return lavacontexts.GetReqID(ctx) },
func() string { return string(header.Peek(httputil.HeaderXRequestID)) },
func() string { return xid.New().String() },
)
header.Set(httputil.HeaderXRequestID, reqId)
ctx = lava.CreateCtxWithReqID(ctx, reqId)
ctx = lavacontexts.CreateCtxWithReqID(ctx, reqId)

rsp, err := wrapperStream(ctx,
&request{
Expand Down
2 changes: 1 addition & 1 deletion clients/resty/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/result"
"github.com/pubgo/funk/retry"
"github.com/pubgo/lava/core/metrics"
"github.com/valyala/fasthttp"

"github.com/pubgo/lava/core/metrics"
"github.com/pubgo/lava/internal/middlewares/middleware_accesslog"
"github.com/pubgo/lava/internal/middlewares/middleware_metric"
"github.com/pubgo/lava/internal/middlewares/middleware_recovery"
Expand Down
Loading
Loading