Skip to content

Commit

Permalink
grpc deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Sep 11, 2024
1 parent 4e1eb47 commit c923cc4
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions cmd/ensign/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ func status(c *cli.Context) (err error) {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var cc *grpc.ClientConn
if cc, err = grpc.DialContext(ctx, endpoint, opts...); err != nil {
if cc, err = grpc.NewClient(endpoint, opts...); err != nil {
return cli.Exit(err, 1)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var rep *api.ServiceState
client := api.NewEnsignClient(cc)
if rep, err = client.Status(ctx, &api.HealthCheck{}); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions pkg/ensign/interceptors/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ func TestAuthenticator(t *testing.T) {

// Create a client to trigger requests
ctx := context.Background()
client, err := srv.ResetClient(ctx)
client, err := srv.ResetClient()
require.NoError(t, err, "could not connect client to mock")

// Should not be able to connect to RPC without authentication
_, err = client.ListTopics(ctx, &api.PageInfo{})
require.EqualError(t, err, "rpc error: code = Unauthenticated desc = missing credentials")

// Should not be able to connect with an invalid JWT token
client, err = srv.ResetClient(ctx, mock.WithPerRPCToken("notarealjwtoken"), grpc.WithTransportCredentials(insecure.NewCredentials()))
client, err = srv.ResetClient(mock.WithPerRPCToken("notarealjwtoken"), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "could not connect client to mock")
_, err = client.ListTopics(ctx, &api.PageInfo{})
require.EqualError(t, err, "rpc error: code = Unauthenticated desc = invalid credentials")

// Should be able to connect with a valid auth token and claims should be in context
token, err := auth.CreateAccessToken(&tokens.Claims{Email: "[email protected]"})
require.NoError(t, err, "could not create access token")
client, err = srv.ResetClient(ctx, mock.WithPerRPCToken(token), grpc.WithTransportCredentials(insecure.NewCredentials()))
client, err = srv.ResetClient(mock.WithPerRPCToken(token), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "could not connect client to mock")

_, err = client.ListTopics(ctx, &api.PageInfo{})
Expand All @@ -81,7 +81,7 @@ func TestAuthenticator(t *testing.T) {

// Create a client to trigger requests
ctx := context.Background()
client, err := srv.ResetClient(ctx)
client, err := srv.ResetClient()
require.NoError(t, err, "could not connect client to mock")

// Handle stream RPC
Expand All @@ -104,7 +104,7 @@ func TestAuthenticator(t *testing.T) {
require.EqualError(t, err, "rpc error: code = Unauthenticated desc = missing credentials")

// Should not be able to connect with an invalid JWT token
client, err = srv.ResetClient(ctx, mock.WithPerRPCToken("notarealjwtoken"), grpc.WithTransportCredentials(insecure.NewCredentials()))
client, err = srv.ResetClient(mock.WithPerRPCToken("notarealjwtoken"), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "could not connect client to mock")

// Should be able to connect to RPC without authentication
Expand All @@ -118,7 +118,7 @@ func TestAuthenticator(t *testing.T) {
// Should be able to connect with a valid auth token and claims should be in context
token, err := auth.CreateAccessToken(&tokens.Claims{Email: "[email protected]"})
require.NoError(t, err, "could not create access token")
client, err = srv.ResetClient(ctx, mock.WithPerRPCToken(token), grpc.WithTransportCredentials(insecure.NewCredentials()))
client, err = srv.ResetClient(mock.WithPerRPCToken(token), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "could not connect client to mock")

// Should be able to connect to RPC without authentication
Expand All @@ -133,10 +133,10 @@ func TestAuthenticator(t *testing.T) {
t.Run("Public", func(t *testing.T) {
// Create a client to trigger requests
ctx := context.Background()
client, err := srv.ResetClient(ctx)
client, err := srv.ResetClient()
require.NoError(t, err, "could not connect client to mock")

probe, err := srv.HealthClient(ctx)
probe, err := srv.HealthClient()
require.NoError(t, err, "could not connect client to mock")

srv.OnStatus = func(context.Context, *api.HealthCheck) (*api.ServiceState, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ensign/interceptors/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func TestMaintenance(t *testing.T) {

// Create client to trigger requests
ctx := context.Background()
client, err := srv.Client(ctx)
client, err := srv.Client()
require.NoError(t, err, "could not connect client to mock")

// Create health probe
probe, err := srv.HealthClient(ctx)
probe, err := srv.HealthClient()
require.NoError(t, err)

t.Run("UnaryMaintenance", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ensign/interceptors/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestRecovery(t *testing.T) {
srv := mock.New(nil, opts...)

// Create client to trigger requests
client, err := srv.Client(context.Background())
client, err := srv.Client()
require.NoError(t, err, "could not connect client to mock")

t.Run("UnaryPanic", func(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/ensign/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ type Ensign struct {
}

// Create and connect an Ensign client to the mock server
func (s *Ensign) Client(ctx context.Context, opts ...grpc.DialOption) (client api.EnsignClient, err error) {
func (s *Ensign) Client(opts ...grpc.DialOption) (client api.EnsignClient, err error) {
if s.client == nil {
if len(opts) == 0 {
opts = make([]grpc.DialOption, 0, 1)
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

var cc *grpc.ClientConn
if cc, err = s.bufnet.Connect(ctx, opts...); err != nil {
if cc, err = s.bufnet.Connect(opts...); err != nil {
return nil, err
}
s.client = api.NewEnsignClient(cc)
Expand All @@ -100,19 +100,19 @@ func (s *Ensign) Client(ctx context.Context, opts ...grpc.DialOption) (client ap
}

// Reset the client with the new dial options
func (s *Ensign) ResetClient(ctx context.Context, opts ...grpc.DialOption) (api.EnsignClient, error) {
func (s *Ensign) ResetClient(opts ...grpc.DialOption) (api.EnsignClient, error) {
s.client = nil
return s.Client(ctx, opts...)
return s.Client(opts...)
}

func (s *Ensign) HealthClient(ctx context.Context, opts ...grpc.DialOption) (client health.HealthClient, err error) {
func (s *Ensign) HealthClient(opts ...grpc.DialOption) (client health.HealthClient, err error) {
if len(opts) == 0 {
opts = make([]grpc.DialOption, 0, 1)
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

var cc *grpc.ClientConn
if cc, err = s.bufnet.Connect(ctx, opts...); err != nil {
if cc, err = s.bufnet.Connect(opts...); err != nil {
return nil, err
}
return health.NewHealthClient(cc), nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/ensign/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *serverTestSuite) SetupSuite() {
time.Sleep(750 * time.Millisecond)

// Create a client for testing purposes
cc, err := s.conn.Connect(context.Background(), grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := s.conn.Connect(grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(err, "could not connect to bufconn")
s.client = api.NewEnsignClient(cc)

Expand Down
2 changes: 1 addition & 1 deletion pkg/ensign/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestMaintenanceMode(t *testing.T) {
t.Cleanup(cancel)

// Create a client for testing purposes
cc, err := conn.Connect(ctx, grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := conn.Connect(grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "could not connect to bufconn")
client := api.NewEnsignClient(cc)

Expand Down
2 changes: 1 addition & 1 deletion pkg/tenant/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Connect(conf config.DatabaseConfig) (err error) {
opts = append(opts, creds)
}

if cc, err = grpc.Dial(endpoint, opts...); err != nil {
if cc, err = grpc.NewClient(endpoint, opts...); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/uptime/health/ensign.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewEnsignMonitor(endpoint string, opts ...MonitorOption) (mon *EnsignMonito
}

mon = &EnsignMonitor{}
if mon.cc, err = grpc.Dial(endpoint, dialer...); err != nil {
if mon.cc, err = grpc.NewClient(endpoint, dialer...); err != nil {
return nil, err
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/utils/bufconn/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
bufsize = 1024 * 1024
buftarget = "bufnet"
bufsize = 1024 * 1024
Endpoint = "passthrough://bufnet"
)

// Listener handles gRPC connections using an in-memory buffer that is useful for
Expand All @@ -34,7 +34,7 @@ func New(opts ...DialOption) *Listener {
}

if sock.target == "" {
sock.target = buftarget
sock.target = Endpoint
}

if sock.sock == nil {
Expand All @@ -55,9 +55,9 @@ func (l *Listener) Close() error {
}

// Connect returns the client side of the bufconn connection.
func (l *Listener) Connect(ctx context.Context, opts ...grpc.DialOption) (cc *grpc.ClientConn, err error) {
func (l *Listener) Connect(opts ...grpc.DialOption) (cc *grpc.ClientConn, err error) {
opts = append([]grpc.DialOption{grpc.WithContextDialer(l.Dialer)}, opts...)
if cc, err = grpc.DialContext(ctx, l.target, opts...); err != nil {
if cc, err = grpc.NewClient(l.target, opts...); err != nil {
return nil, err
}
return cc, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/probez/grpc/v1/probez_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestServer(t *testing.T) {
bufnet.Close()
}()

cc, err := bufnet.Connect(context.Background(), grpc.WithTransportCredentials(insecure.NewCredentials()))
cc, err := bufnet.Connect(grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err, "could not connect to probe server")

var wg sync.WaitGroup
Expand Down

0 comments on commit c923cc4

Please sign in to comment.