Skip to content

Commit

Permalink
Feat: add grpc gateway http rule (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee authored Aug 28, 2024
1 parent 3a11176 commit 416e654
Show file tree
Hide file tree
Showing 85 changed files with 3,079 additions and 717 deletions.
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

0 comments on commit 416e654

Please sign in to comment.