diff --git a/certificate/authorization/authorizations_test.go b/certificate/authorization/authorizations_test.go index 5ad6c0b3c..ee5173b54 100644 --- a/certificate/authorization/authorizations_test.go +++ b/certificate/authorization/authorizations_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/require" "storj.io/storj/certificate/certificateclient" + "storj.io/storj/pkg/pb" "storj.io/storj/pkg/peertls/tlsopts" "storj.io/storj/pkg/rpc" "storj.io/storj/pkg/storj" @@ -230,7 +231,7 @@ func TestNewClient(t *testing.T) { defer ctx.Check(conn.Close) - client := certificateclient.NewClientFrom(conn.CertificatesClient()) + client := certificateclient.NewClientFrom(pb.NewDRPCCertificatesClient(conn.Raw())) assert.NoError(t, err) assert.NotNil(t, client) diff --git a/certificate/certificateclient/client.go b/certificate/certificateclient/client.go index eadc30a80..2e35841d7 100644 --- a/certificate/certificateclient/client.go +++ b/certificate/certificateclient/client.go @@ -41,7 +41,7 @@ func New(ctx context.Context, dialer rpc.Dialer, address string) (_ *Client, err return &Client{ conn: conn, - client: conn.CertificatesClient(), + client: pb.NewDRPCCertificatesClient(conn.Raw()), }, nil } diff --git a/cmd/inspector/main.go b/cmd/inspector/main.go index 308462071..f4974d787 100644 --- a/cmd/inspector/main.go +++ b/cmd/inspector/main.go @@ -132,10 +132,10 @@ func NewInspector(address, path string) (*Inspector, error) { return &Inspector{ conn: conn, identity: id, - overlayclient: conn.OverlayInspectorClient(), - irrdbclient: conn.IrreparableInspectorClient(), - healthclient: conn.HealthInspectorClient(), - paymentsClient: conn.PaymentsClient(), + overlayclient: pb.NewDRPCOverlayInspectorClient(conn.Raw()), + irrdbclient: pb.NewDRPCIrreparableInspectorClient(conn.Raw()), + healthclient: pb.NewDRPCHealthInspectorClient(conn.Raw()), + paymentsClient: pb.NewDRPCPaymentsClient(conn.Raw()), }, nil } diff --git a/cmd/storagenode/dashboard.go b/cmd/storagenode/dashboard.go index 286500945..5f06f547a 100644 --- a/cmd/storagenode/dashboard.go +++ b/cmd/storagenode/dashboard.go @@ -40,7 +40,7 @@ func dialDashboardClient(ctx context.Context, address string) (*dashboardClient, } func (dash *dashboardClient) dashboard(ctx context.Context) (*pb.DashboardResponse, error) { - return dash.conn.PieceStoreInspectorClient().Dashboard(ctx, &pb.DashboardRequest{}) + return pb.NewDRPCPieceStoreInspectorClient(dash.conn.Raw()).Dashboard(ctx, &pb.DashboardRequest{}) } func (dash *dashboardClient) close() error { diff --git a/cmd/storagenode/gracefulexit.go b/cmd/storagenode/gracefulexit.go index c15fb64af..ca7060368 100644 --- a/cmd/storagenode/gracefulexit.go +++ b/cmd/storagenode/gracefulexit.go @@ -37,15 +37,15 @@ func dialGracefulExitClient(ctx context.Context, address string) (*gracefulExitC } func (client *gracefulExitClient) getNonExitingSatellites(ctx context.Context) (*pb.GetNonExitingSatellitesResponse, error) { - return client.conn.NodeGracefulExitClient().GetNonExitingSatellites(ctx, &pb.GetNonExitingSatellitesRequest{}) + return pb.NewDRPCNodeGracefulExitClient(client.conn.Raw()).GetNonExitingSatellites(ctx, &pb.GetNonExitingSatellitesRequest{}) } func (client *gracefulExitClient) initGracefulExit(ctx context.Context, req *pb.InitiateGracefulExitRequest) (*pb.ExitProgress, error) { - return client.conn.NodeGracefulExitClient().InitiateGracefulExit(ctx, req) + return pb.NewDRPCNodeGracefulExitClient(client.conn.Raw()).InitiateGracefulExit(ctx, req) } func (client *gracefulExitClient) getExitProgress(ctx context.Context) (*pb.GetExitProgressResponse, error) { - return client.conn.NodeGracefulExitClient().GetExitProgress(ctx, &pb.GetExitProgressRequest{}) + return pb.NewDRPCNodeGracefulExitClient(client.conn.Raw()).GetExitProgress(ctx, &pb.GetExitProgressRequest{}) } func (client *gracefulExitClient) close() error { diff --git a/pkg/rpc/alias.go b/pkg/rpc/alias.go deleted file mode 100644 index d10dc3558..000000000 --- a/pkg/rpc/alias.go +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (C) 2019 Storj Labs, Inc. -// See LICENSE for copying information. - -package rpc - -import ( - "storj.io/storj/pkg/pb" - "storj.io/storj/pkg/rpc/rpcpool" -) - -// RawConn is a type alias to a drpc client connection -type RawConn = rpcpool.Conn - -// CertificatesClient returns a CertificatesClient for this connection -func (c *Conn) CertificatesClient() pb.DRPCCertificatesClient { - return pb.NewDRPCCertificatesClient(c.raw) -} - -// ContactClient returns a ContactClient for this connection -func (c *Conn) ContactClient() pb.DRPCContactClient { - return pb.NewDRPCContactClient(c.raw) -} - -// HealthInspectorClient returns a HealthInspectorClient for this connection -func (c *Conn) HealthInspectorClient() pb.DRPCHealthInspectorClient { - return pb.NewDRPCHealthInspectorClient(c.raw) -} - -// IrreparableInspectorClient returns a IrreparableInspectorClient for this connection -func (c *Conn) IrreparableInspectorClient() pb.DRPCIrreparableInspectorClient { - return pb.NewDRPCIrreparableInspectorClient(c.raw) -} - -// MetainfoClient returns a MetainfoClient for this connection -func (c *Conn) MetainfoClient() pb.DRPCMetainfoClient { - return pb.NewDRPCMetainfoClient(c.raw) -} - -// NodeClient returns a NodeClient for this connection -func (c *Conn) NodeClient() pb.DRPCNodeClient { - return pb.NewDRPCNodeClient(c.raw) -} - -// NodeGracefulExitClient returns a NodeGracefulExitClient for this connection -func (c *Conn) NodeGracefulExitClient() pb.DRPCNodeGracefulExitClient { - return pb.NewDRPCNodeGracefulExitClient(c.raw) -} - -// NodeStatsClient returns a NodeStatsClient for this connection -func (c *Conn) NodeStatsClient() pb.DRPCNodeStatsClient { - return pb.NewDRPCNodeStatsClient(c.raw) -} - -// OrdersClient returns a OrdersClient for this connection -func (c *Conn) OrdersClient() pb.DRPCOrdersClient { - return pb.NewDRPCOrdersClient(c.raw) -} - -// OverlayInspectorClient returns a OverlayInspectorClient for this connection -func (c *Conn) OverlayInspectorClient() pb.DRPCOverlayInspectorClient { - return pb.NewDRPCOverlayInspectorClient(c.raw) -} - -// PaymentsClient returns a PaymentsClient for this connection -func (c *Conn) PaymentsClient() pb.DRPCPaymentsClient { - return pb.NewDRPCPaymentsClient(c.raw) -} - -// PieceStoreInspectorClient returns a PieceStoreInspectorClient for this connection -func (c *Conn) PieceStoreInspectorClient() pb.DRPCPieceStoreInspectorClient { - return pb.NewDRPCPieceStoreInspectorClient(c.raw) -} - -// PiecestoreClient returns a PiecestoreClient for this connection -func (c *Conn) PiecestoreClient() pb.DRPCPiecestoreClient { - return pb.NewDRPCPiecestoreClient(c.raw) -} - -// ReferralManagerClient returns a ReferralManagerClient for this connection -func (c *Conn) ReferralManagerClient() pb.DRPCReferralManagerClient { - return pb.NewDRPCReferralManagerClient(c.raw) -} - -// SatelliteGracefulExitClient returns a SatelliteGracefulExitClient for this connection -func (c *Conn) SatelliteGracefulExitClient() pb.DRPCSatelliteGracefulExitClient { - return pb.NewDRPCSatelliteGracefulExitClient(c.raw) -} - -// VouchersClient returns a VouchersClient for this connection -func (c *Conn) VouchersClient() pb.DRPCVouchersClient { - return pb.NewDRPCVouchersClient(c.raw) -} diff --git a/pkg/rpc/conn.go b/pkg/rpc/conn.go index 6ea2a533d..e17919832 100644 --- a/pkg/rpc/conn.go +++ b/pkg/rpc/conn.go @@ -7,8 +7,12 @@ import ( "crypto/tls" "storj.io/storj/pkg/identity" + "storj.io/storj/pkg/rpc/rpcpool" ) +// RawConn is a type alias to a drpc client connection +type RawConn = rpcpool.Conn + // Conn is a wrapper around a drpc client connection. type Conn struct { state tls.ConnectionState @@ -25,3 +29,8 @@ func (c *Conn) ConnectionState() tls.ConnectionState { return c.state } func (c *Conn) PeerIdentity() (*identity.PeerIdentity, error) { return identity.PeerIdentityFromChain(c.state.PeerCertificates) } + +// Raw returns the underlying connection. +func (c *Conn) Raw() *RawConn { + return c.raw +} diff --git a/pkg/rpc/gen.go b/pkg/rpc/gen.go deleted file mode 100644 index a6609b689..000000000 --- a/pkg/rpc/gen.go +++ /dev/null @@ -1,192 +0,0 @@ -// Copyright (C) 2019 Storj Labs, Inc. -// See LICENSE for copying information. -// -// This code generates the compat_drpc and compat_grpc files by reading in -// protobuf definitions. Its purpose is to generate a bunch of type aliases -// and forwarding functions so that a build tag transparently swaps out the -// concrete implementations of the rpcs. - -// +build ignore - -package main - -import ( - "bytes" - "fmt" - "go/format" - "io" - "io/ioutil" - "log" - "os" - "path/filepath" - "regexp" - "sort" - "strings" - - "github.com/zeebo/errs" -) - -func main() { - if err := run(); err != nil { - log.Fatalf("%+v", err) - } -} - -func usage() error { - return errs.New("usage: %s drpc ", os.Args[0]) -} - -func run() error { - if len(os.Args) < 4 { - return usage() - } - clients, err := findClientsInDir(os.Args[1]) - if err != nil { - return errs.Wrap(err) - } - info, ok := infos[os.Args[2]] - if !ok { - return usage() - } - return generate(clients, info, os.Args[3]) -} - -// -// info about the difference between generated files -// - -type generateInfo struct { - Name string - Import string - Prefix string - Conn string -} - -var infos = map[string]generateInfo{ - "drpc": { - Name: "drpc", - Import: "storj.io/storj/pkg/rpc/rpcpool", - Prefix: "DRPC", - Conn: "rpcpool.Conn", - }, -} - -// -// main code to generate a compatability file -// - -func generate(clients []string, info generateInfo, output string) (err error) { - var buf bytes.Buffer - p := printer{w: &buf} - P := p.P - Pf := p.Pf - - P("// Copyright (C) 2019 Storj Labs, Inc.") - P("// See LICENSE for copying information.") - P() - P("package rpc") - P() - P("import (") - Pf("%q", info.Import) - if !strings.HasPrefix(info.Import, "storj.io/") { - P() - } - Pf("%q", "storj.io/storj/pkg/pb") - P(")") - P() - P("// RawConn is a type alias to a", info.Name, "client connection") - P("type RawConn =", info.Conn) - P() - P("type (") - for _, client := range clients { - P("//", client, "is an alias to the", info.Name, "client interface") - Pf("%s = pb.%s%s", client, info.Prefix, client) - P() - } - P(")") - for _, client := range clients { - P() - Pf("// New%s returns the %s version of a %s", client, info.Name, client) - Pf("func New%s(rc *RawConn) %s {", client, client) - Pf("return pb.New%s%s(rc)", info.Prefix, client) - P("}") - P() - Pf("// %s returns a %s for this connection", client, client) - Pf("func (c *Conn) %s() %s {", client, client) - Pf("return New%s(c.raw)", client) - P("}") - } - - if err := p.Err(); err != nil { - return errs.Wrap(err) - } - fmtd, err := format.Source(buf.Bytes()) - if err != nil { - return errs.Wrap(err) - } - return errs.Wrap(ioutil.WriteFile(output, fmtd, 0644)) -} - -// -// hacky code to find all the rpc clients in a go package -// - -var clientRegex = regexp.MustCompile("^type (.*Client) interface {$") - -func findClientsInDir(dir string) (clients []string, err error) { - files, err := filepath.Glob(filepath.Join(dir, "*.pb.go")) - if err != nil { - return nil, errs.Wrap(err) - } - for _, file := range files { - fileClients, err := findClientsInFile(file) - if err != nil { - return nil, errs.Wrap(err) - } - clients = append(clients, fileClients...) - } - sort.Strings(clients) - return clients, nil -} - -func findClientsInFile(file string) (clients []string, err error) { - data, err := ioutil.ReadFile(file) - if err != nil { - return nil, errs.Wrap(err) - } - for _, line := range bytes.Split(data, []byte("\n")) { - switch client := clientRegex.FindSubmatch(line); { - case client == nil: - case bytes.HasPrefix(client[1], []byte("DRPC")): - case bytes.Contains(client[1], []byte("_")): - default: - clients = append(clients, string(client[1])) - } - } - return clients, nil -} - -// -// helper to check errors while printing -// - -type printer struct { - w io.Writer - err error -} - -func (p *printer) P(args ...interface{}) { - if p.err == nil { - _, p.err = fmt.Fprintln(p.w, args...) - } -} - -func (p *printer) Pf(format string, args ...interface{}) { - if p.err == nil { - _, p.err = fmt.Fprintf(p.w, format+"\n", args...) - } -} - -func (p *printer) Err() error { - return p.err -} diff --git a/private/testplanet/planet_test.go b/private/testplanet/planet_test.go index 4b2bc9e44..080a319cf 100644 --- a/private/testplanet/planet_test.go +++ b/private/testplanet/planet_test.go @@ -41,7 +41,7 @@ func TestBasic(t *testing.T) { conn, err := sn.Dialer.DialNode(ctx, &satellite) require.NoError(t, err) defer ctx.Check(conn.Close) - _, err = conn.NodeClient().CheckIn(ctx, &pb.CheckInRequest{ + _, err = pb.NewDRPCNodeClient(conn.Raw()).CheckIn(ctx, &pb.CheckInRequest{ Address: node.GetAddress().GetAddress(), Version: &node.Version, Capacity: &node.Capacity, diff --git a/satellite/contact/client.go b/satellite/contact/client.go index 548a907e4..16a158af3 100644 --- a/satellite/contact/client.go +++ b/satellite/contact/client.go @@ -25,7 +25,7 @@ func newClient(ctx context.Context, dialer rpc.Dialer, address string, id storj. return &client{ conn: conn, - client: conn.ContactClient(), + client: pb.NewDRPCContactClient(conn.Raw()), }, nil } diff --git a/satellite/gracefulexit/endpoint_test.go b/satellite/gracefulexit/endpoint_test.go index 0d3c11b1d..366defecc 100644 --- a/satellite/gracefulexit/endpoint_test.go +++ b/satellite/gracefulexit/endpoint_test.go @@ -189,7 +189,7 @@ func TestConcurrentConnections(t *testing.T) { err = errs.Combine(err, conn.Close()) }() - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) // wait for "main" call to begin wg.Wait() @@ -209,7 +209,7 @@ func TestConcurrentConnections(t *testing.T) { require.NoError(t, err) defer ctx.Check(conn.Close) - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) // this connection will immediately return since graceful exit has not been initiated yet c, err := client.Process(ctx) require.NoError(t, err) @@ -407,7 +407,7 @@ func TestExitDisqualifiedNodeFailOnStart(t *testing.T) { require.NoError(t, err) defer ctx.Check(conn.Close) - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) processClient, err := client.Process(ctx) require.NoError(t, err) @@ -944,7 +944,7 @@ func TestExitDisabled(t *testing.T) { require.NoError(t, err) defer ctx.Check(conn.Close) - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) processClient, err := client.Process(ctx) require.NoError(t, err) @@ -1025,7 +1025,7 @@ func TestPointerChangedOrDeleted(t *testing.T) { require.NoError(t, err) defer ctx.Check(conn.Close) - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) c, err := client.Process(ctx) require.NoError(t, err) @@ -1260,7 +1260,7 @@ func TestFailureStorageNodeIgnoresTransferMessages(t *testing.T) { require.NoError(t, err) defer ctx.Check(conn.Close) - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) c, err := client.Process(ctx) require.NoError(t, err) @@ -1387,7 +1387,7 @@ func testTransfers(t *testing.T, objects int, verifier func(ctx *testcontext.Con require.NoError(t, err) defer ctx.Check(conn.Close) - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) c, err := client.Process(ctx) require.NoError(t, err) diff --git a/satellite/referrals/service.go b/satellite/referrals/service.go index 5b678b901..6af9e18eb 100644 --- a/satellite/referrals/service.go +++ b/satellite/referrals/service.go @@ -72,7 +72,7 @@ func (service *Service) GetTokens(ctx context.Context, userID *uuid.UUID) (token err = conn.Close() }() - client := conn.ReferralManagerClient() + client := pb.NewDRPCReferralManagerClient(conn.Raw()) response, err := client.GetTokens(ctx, &pb.GetTokensRequest{ OwnerUserId: userID[:], OwnerSatelliteId: service.signer.ID(), @@ -166,7 +166,7 @@ func (service *Service) redeemToken(ctx context.Context, userID *uuid.UUID, toke return errs.Wrap(err) } - client := conn.ReferralManagerClient() + client := pb.NewDRPCReferralManagerClient(conn.Raw()) _, err = client.RedeemToken(ctx, &pb.RedeemTokenRequest{ Token: referralToken[:], RedeemUserId: userID[:], diff --git a/satellite/vouchers/vouchers_test.go b/satellite/vouchers/vouchers_test.go index 746bee270..ff6c76c6f 100644 --- a/satellite/vouchers/vouchers_test.go +++ b/satellite/vouchers/vouchers_test.go @@ -23,7 +23,7 @@ func TestVouchers(t *testing.T) { require.NoError(t, err) defer ctx.Check(conn.Close) - client := conn.VouchersClient() + client := pb.NewDRPCVouchersClient(conn.Raw()) resp, err := client.Request(ctx, &pb.VoucherRequest{}) require.Nil(t, resp) diff --git a/storagenode/contact/chore.go b/storagenode/contact/chore.go index f7a8f2476..106aac33c 100644 --- a/storagenode/contact/chore.go +++ b/storagenode/contact/chore.go @@ -159,7 +159,7 @@ func (chore *Chore) pingSatelliteOnce(ctx context.Context, id storj.NodeID) (err } defer func() { err = errs.Combine(err, conn.Close()) }() - _, err = conn.NodeClient().CheckIn(ctx, &pb.CheckInRequest{ + _, err = pb.NewDRPCNodeClient(conn.Raw()).CheckIn(ctx, &pb.CheckInRequest{ Address: self.Address.GetAddress(), Version: &self.Version, Capacity: &self.Capacity, diff --git a/storagenode/contact/contact_test.go b/storagenode/contact/contact_test.go index 7b15b7b9c..c5ecb2ea2 100644 --- a/storagenode/contact/contact_test.go +++ b/storagenode/contact/contact_test.go @@ -26,7 +26,7 @@ func TestStoragenodeContactEndpoint(t *testing.T) { require.NoError(t, err) defer ctx.Check(conn.Close) - resp, err := conn.ContactClient().PingNode(ctx, &pb.ContactPingRequest{}) + resp, err := pb.NewDRPCContactClient(conn.Raw()).PingNode(ctx, &pb.ContactPingRequest{}) require.NotNil(t, resp) require.NoError(t, err) @@ -34,7 +34,7 @@ func TestStoragenodeContactEndpoint(t *testing.T) { time.Sleep(time.Second) //HACKFIX: windows has large time granularity - resp, err = conn.ContactClient().PingNode(ctx, &pb.ContactPingRequest{}) + resp, err = pb.NewDRPCContactClient(conn.Raw()).PingNode(ctx, &pb.ContactPingRequest{}) require.NotNil(t, resp) require.NoError(t, err) diff --git a/storagenode/gracefulexit/worker.go b/storagenode/gracefulexit/worker.go index 75c76cb55..487b383c4 100644 --- a/storagenode/gracefulexit/worker.go +++ b/storagenode/gracefulexit/worker.go @@ -72,7 +72,7 @@ func (worker *Worker) Run(ctx context.Context, done func()) (err error) { err = errs.Combine(err, conn.Close()) }() - client := conn.SatelliteGracefulExitClient() + client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw()) c, err := client.Process(ctx) if err != nil { diff --git a/storagenode/nodestats/service.go b/storagenode/nodestats/service.go index c1e0fff6d..2da54c69d 100644 --- a/storagenode/nodestats/service.go +++ b/storagenode/nodestats/service.go @@ -131,7 +131,7 @@ func (s *Service) dial(ctx context.Context, satelliteID storj.NodeID) (_ *Client return &Client{ conn: conn, - DRPCNodeStatsClient: conn.NodeStatsClient(), + DRPCNodeStatsClient: pb.NewDRPCNodeStatsClient(conn.Raw()), }, nil } diff --git a/storagenode/orders/service.go b/storagenode/orders/service.go index fa5a42d84..e8da7dec1 100644 --- a/storagenode/orders/service.go +++ b/storagenode/orders/service.go @@ -257,7 +257,7 @@ func (service *Service) settle(ctx context.Context, log *zap.Logger, satelliteID } defer func() { err = errs.Combine(err, conn.Close()) }() - stream, err := conn.OrdersClient().Settlement(ctx) + stream, err := pb.NewDRPCOrdersClient(conn.Raw()).Settlement(ctx) if err != nil { return OrderError.New("failed to start settlement: %v", err) } diff --git a/uplink/metainfo/client.go b/uplink/metainfo/client.go index 62dce46bc..23f5c29f9 100644 --- a/uplink/metainfo/client.go +++ b/uplink/metainfo/client.go @@ -62,7 +62,7 @@ func Dial(ctx context.Context, dialer rpc.Dialer, address string, apiKey *macaro return &Client{ conn: conn, - client: conn.MetainfoClient(), + client: pb.NewDRPCMetainfoClient(conn.Raw()), apiKeyRaw: apiKey.SerializeRaw(), userAgent: userAgent, }, nil diff --git a/uplink/piecestore/client.go b/uplink/piecestore/client.go index 18ac14aa0..b1efa919a 100644 --- a/uplink/piecestore/client.go +++ b/uplink/piecestore/client.go @@ -55,7 +55,7 @@ func Dial(ctx context.Context, dialer rpc.Dialer, target *pb.Node, log *zap.Logg return &Client{ log: log, - client: conn.PiecestoreClient(), + client: pb.NewDRPCPiecestoreClient(conn.Raw()), conn: conn, config: config, }, nil