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)
}
})),
)
})
}

View File

@ -40,11 +40,8 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg
cli := client.New(
cfg.Client.Address,
client.WithSkipVerify(cfg.Client.SkipVerify),
client.WithGRPC(cfg.Client.GRPC),
client.WithGRPCWeb(cfg.Client.GRPCWeb),
client.WithUUIDHeader(cfg.Runner.UUID),
client.WithTokenHeader(cfg.Runner.Token),
cfg.Runner.UUID,
cfg.Runner.Token,
)
runner := &runtime.Runner{

View File

@ -264,9 +264,7 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error {
// initial http client
cli := client.New(
inputs.InstanceAddr,
client.WithSkipVerify(cfg.Client.SkipVerify),
client.WithGRPC(cfg.Client.GRPC),
client.WithGRPCWeb(cfg.Client.GRPCWeb),
"", "",
)
for {

View File

@ -23,10 +23,7 @@ type (
}
Client struct {
Address string `ignored:"true"`
SkipVerify bool `envconfig:"GITEA_RPC_SKIP_VERIFY"`
GRPC bool `envconfig:"GITEA_RPC_GRPC" default:"true"`
GRPCWeb bool `envconfig:"GITEA_RPC_GRPC_WEB"`
Address string `ignored:"true"`
}
Runner struct {

3
go.mod
View File

@ -13,7 +13,6 @@ require (
github.com/nektos/act v0.0.0
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde
google.golang.org/protobuf v1.28.1
)
@ -69,9 +68,9 @@ require (
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

1
go.sum
View File

@ -974,7 +974,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=