Disable HTTP/2 (#4)

We use [connect-go](https://github.com/bufbuild/connect-go) instead of [grpc-go](https://github.com/grpc/grpc-go) because connect-go support HTTP/1.1, that means we can mount the gRPC api on the Gitea server without change the protocol.

So it doesn't make sense that make the runner support both HTTP/1.1 and HTTP/2, and [upgrade the protocol used on Gitea](
ae018b6b48/modules/graceful/server_http.go (L23)) to support HTTP/2 and h2c. Although it works right now, I believe there'll be lots of problems when the Gitea server is behind a reverse proxy.

So let's KISS, we don't touch the http protocol of Gitea, and disable HTTP/2 for runner. And we would support HTTP/2 in the future if we really need it.

Co-authored-by: Jason Song <i@wolfogre.com>
Reviewed-on: https://gitea.com/gitea/act_runner/pulls/4
This commit is contained in:
Jason Song
2022-11-29 10:35:59 +08:00
parent 715d0e85ce
commit 8996b9b0e4
7 changed files with 27 additions and 162 deletions

View File

@ -1,69 +1,41 @@
package client
import (
"crypto/tls"
"net"
"net/http"
"strings"
"time"
"code.gitea.io/bots-proto-go/ping/v1/pingv1connect"
"code.gitea.io/bots-proto-go/runner/v1/runnerv1connect"
"golang.org/x/net/http2"
"context"
"gitea.com/gitea/act_runner/core"
"github.com/bufbuild/connect-go"
"net/http"
"strings"
)
// New returns a new runner client.
func New(endpoint string, opts ...Option) *HTTPClient {
cfg := &config{}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt.apply(cfg)
}
if cfg.httpClient == nil {
cfg.httpClient = &http.Client{
Timeout: 1 * time.Minute,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
},
},
}
}
if cfg.skipVerify {
cfg.httpClient = &http.Client{
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
func New(endpoint string, uuid, token string, opts ...connect.ClientOption) *HTTPClient {
baseURL := strings.TrimRight(endpoint, "/") + "/api/bots"
opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
if uuid != "" {
req.Header().Set(core.UUIDHeader, uuid)
}
if token != "" {
req.Header().Set(core.TokenHeader, token)
}
return next(ctx, req)
}
})))
return &HTTPClient{
PingServiceClient: pingv1connect.NewPingServiceClient(
cfg.httpClient,
http.DefaultClient,
baseURL,
cfg.opts...,
opts...,
),
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
cfg.httpClient,
http.DefaultClient,
baseURL,
cfg.opts...,
opts...,
),
endpoint: endpoint,
}

View File

@ -1,97 +0,0 @@
package client
import (
"context"
"net/http"
"gitea.com/gitea/act_runner/core"
"github.com/bufbuild/connect-go"
)
type config struct {
httpClient *http.Client
skipVerify bool
opts []connect.ClientOption
}
// An Option configures a mutex.
type Option interface {
apply(*config)
}
// OptionFunc is a function that configure a value.
type OptionFunc func(*config)
// Apply calls f(option)
func (f OptionFunc) apply(cfg *config) {
f(cfg)
}
func WithSkipVerify(c bool) Option {
return OptionFunc(func(cfg *config) {
cfg.skipVerify = c
})
}
func WithClientOptions(opts ...connect.ClientOption) Option {
return OptionFunc(func(cfg *config) {
cfg.opts = append(cfg.opts, opts...)
})
}
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
func WithGRPC(c bool) Option {
return OptionFunc(func(cfg *config) {
if !c {
return
}
cfg.opts = append(cfg.opts, connect.WithGRPC())
})
}
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
func WithGRPCWeb(c bool) Option {
return OptionFunc(func(cfg *config) {
if !c {
return
}
cfg.opts = append(cfg.opts, connect.WithGRPCWeb())
})
}
// WithUUIDHeader add runner uuid in header
func WithUUIDHeader(uuid string) Option {
return OptionFunc(func(cfg *config) {
if uuid == "" {
return
}
cfg.opts = append(
cfg.opts,
connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
req.Header().Set(core.UUIDHeader, uuid)
return next(ctx, req)
}
})),
)
})
}
// WithTokenHeader add runner token in header
func WithTokenHeader(token string) Option {
return OptionFunc(func(cfg *config) {
if token == "" {
return
}
cfg.opts = append(
cfg.opts,
connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
req.Header().Set(core.TokenHeader, token)
return next(ctx, req)
}
})),
)
})
}