From d359276fe1d4203f84f21249f40bb942a29fc7a5 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 14 Aug 2022 10:59:09 +0800 Subject: [PATCH] chore(client): support gRPC and gRPC web protocol Signed-off-by: Bo-Yi Wu --- client/http.go | 11 ++++++++++- client/options.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/config.go | 2 ++ cmd/damon.go | 2 ++ poller/poller.go | 1 + 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 client/options.go diff --git a/client/http.go b/client/http.go index d7efbde..de2bae1 100644 --- a/client/http.go +++ b/client/http.go @@ -15,13 +15,19 @@ import ( ) // New returns a new runner client. -func New(endpoint, secret string, skipverify bool) *HTTPClient { +func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient { client := &HTTPClient{ Endpoint: endpoint, Secret: secret, SkipVerify: skipverify, } + // Loop through each option + for _, opt := range opts { + // Call the option giving the instantiated + opt.Apply(client) + } + client.Client = &http.Client{ Timeout: 5 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { @@ -57,6 +63,8 @@ type HTTPClient struct { Endpoint string Secret string SkipVerify bool + + opts []connect.ClientOption } // Ping sends a ping message to the server to test connectivity. @@ -64,6 +72,7 @@ func (p *HTTPClient) Ping(ctx context.Context, machine string) error { client := v1connect.NewPingServiceClient( p.Client, p.Endpoint, + p.opts..., ) req := connect.NewRequest(&v1.PingRequest{ Data: machine, diff --git a/client/options.go b/client/options.go new file mode 100644 index 0000000..f45d7ab --- /dev/null +++ b/client/options.go @@ -0,0 +1,36 @@ +package client + +import "github.com/bufbuild/connect-go" + +// An Option configures a mutex. +type Option interface { + Apply(*HTTPClient) +} + +// OptionFunc is a function that configure a value. +type OptionFunc func(*HTTPClient) + +// Apply calls f(option) +func (f OptionFunc) Apply(cli *HTTPClient) { + f(cli) +} + +// WithGRPC configures clients to use the HTTP/2 gRPC protocol. +func WithGRPC(c bool) Option { + return OptionFunc(func(cli *HTTPClient) { + if !c { + return + } + cli.opts = append(cli.opts, connect.WithGRPC()) + }) +} + +// WithGRPCWeb configures clients to use the gRPC-Web protocol. +func WithGRPCWeb(c bool) Option { + return OptionFunc(func(cli *HTTPClient) { + if !c { + return + } + cli.opts = append(cli.opts, connect.WithGRPCWeb()) + }) +} diff --git a/cmd/config.go b/cmd/config.go index 6ea263b..0b266f9 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -20,6 +20,8 @@ type ( Host string `envconfig:"GITEA_RPC_HOST" required:"true"` Secret string `envconfig:"GITEA_RPC_SECRET" required:"true"` SkipVerify bool `envconfig:"GITEA_RPC_SKIP_VERIFY"` + GRPC bool `envconfig:"GITEA_RPC_GRPC"` + GRPCWeb bool `envconfig:"GITEA_RPC_GRPC_WEB"` } Runner struct { diff --git a/cmd/damon.go b/cmd/damon.go index f3b43ee..d00c61f 100644 --- a/cmd/damon.go +++ b/cmd/damon.go @@ -176,6 +176,8 @@ func runDaemon(ctx context.Context, input *Input) func(cmd *cobra.Command, args cfg.Client.Address, cfg.Client.Secret, cfg.Client.SkipVerify, + client.WithGRPC(cfg.Client.GRPC), + client.WithGRPCWeb(cfg.Client.GRPCWeb), ) for { diff --git a/poller/poller.go b/poller/poller.go index bab63e4..c320d48 100644 --- a/poller/poller.go +++ b/poller/poller.go @@ -4,6 +4,7 @@ import ( "context" "gitea.com/gitea/act_runner/client" + log "github.com/sirupsen/logrus" )