Skip to content

Commit

Permalink
upgrade connection base to be more hands off
Browse files Browse the repository at this point in the history
  • Loading branch information
pitabwire committed Jan 26, 2024
1 parent acc295c commit 951fdfe
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 148 deletions.
18 changes: 15 additions & 3 deletions go/common/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type GrpcClientBase struct {
xMetadata metadata.MD
}

// Connection obtains the connection to the API service. User should invoke this
// connection is required.
func (gbc *GrpcClientBase) Connection() *grpc.ClientConn {
return gbc.clientConn
}

// Close closes the connection to the API service. The user should invoke this when
// the client is no longer required.
func (gbc *GrpcClientBase) Close() error {
Expand All @@ -61,12 +67,18 @@ func (gbc *GrpcClientBase) GetInfo() metadata.MD {
return gbc.xMetadata
}

func NewClientBase(clientConn *grpc.ClientConn) GrpcClientBase {
func NewClientBase(ctx context.Context, opts ...ClientOption) (*GrpcClientBase, error) {

connPool, err := DialConnection(ctx, opts...)
if err != nil {
return nil, err
}

clientBase := GrpcClientBase{
clientConn: clientConn,
clientConn: connPool,
}
clientBase.SetInfo()
return clientBase
return &clientBase, nil
}

type JWTInterceptor struct {
Expand Down
17 changes: 8 additions & 9 deletions go/files/client_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ package file_v1

import (
"context"
apic "github.com/antinvestor/apis/go/common"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

const ctxKeyService = apic.CtxServiceKey("filesClientKey")
const ctxKeyService = common.CtxServiceKey("filesClientKey")

func defaultFilesClientOptions() []apic.ClientOption {
return []apic.ClientOption{
apic.WithEndpoint("https://files.api.antinvestor.com"),
func defaultFilesClientOptions() []common.ClientOption {
return []common.ClientOption{
common.WithEndpoint("https://files.api.antinvestor.com"),
}
}

Expand Down Expand Up @@ -53,10 +52,10 @@ type FilesClient struct {

// NewFilesClient creates a new file client.
// The service that an application uses to create and read files stored in the system
func NewFilesClient(ctx context.Context, opts ...apic.ClientOption) (*FilesClient, error) {
func NewFilesClient(ctx context.Context, opts ...common.ClientOption) (*FilesClient, error) {
clientOpts := defaultFilesClientOptions()

httClient, err := apic.HttpClient(ctx, append(clientOpts, opts...)...)
httClient, err := common.HttpClient(ctx, append(clientOpts, opts...)...)

if err != nil {
return nil, err
Expand All @@ -79,7 +78,7 @@ func NewFilesClient(ctx context.Context, opts ...apic.ClientOption) (*FilesClien
// the `x-goog-api-client` header passed on each request. Intended for
// use by Google-written clients.
func (fc *FilesClient) setClientInfo(keyval ...string) {
kv := append([]string{"gl-go", apic.VersionGo()}, keyval...)
kv := append([]string{"gl-go", common.VersionGo()}, keyval...)
kv = append(kv, "grpc", grpc.Version)
fc.xMetadata = metadata.Pairs("x-ai-api-client", apic.XAntHeader(kv...))
fc.xMetadata = metadata.Pairs("x-ai-api-client", common.XAntHeader(kv...))
}
4 changes: 3 additions & 1 deletion go/files/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module github.com/antinvestor/apis/go/files

go 1.21

replace github.com/antinvestor/apis/go/common => ../common

require (
github.com/antinvestor/apis/go/common v1.7.20
github.com/antinvestor/apis/go/common v1.7.21
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.61.0
)
Expand Down
4 changes: 3 additions & 1 deletion go/ledger/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module github.com/antinvestor/apis/go/ledger

go 1.21

replace github.com/antinvestor/apis/go/common => ../common

require (
github.com/antinvestor/apis/go/common v1.7.20
github.com/antinvestor/apis/go/common v1.7.21
go.uber.org/mock v0.4.0
google.golang.org/genproto v0.0.0-20240125205218-1f4bbc51befe
google.golang.org/grpc v1.61.0
Expand Down
2 changes: 0 additions & 2 deletions go/ledger/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/antinvestor/apis/go/common v1.7.20 h1:Vc0uloYy2cXNDuNCxheIoifHANyBexnhaJaumGhDHFA=
github.com/antinvestor/apis/go/common v1.7.20/go.mod h1:j4b9mKVLMGFFaA7TyHIFE/KfmOyuMBwBfoDqdBY+6X0=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
21 changes: 10 additions & 11 deletions go/ledger/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ package ledgerv1

import (
"context"
apic "github.com/antinvestor/apis/go/common"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"math"
)

const ctxKeyService = apic.CtxServiceKey("ledgerClientKey")
const ctxKeyService = common.CtxServiceKey("ledgerClientKey")

func defaultLedgerClientOptions() []apic.ClientOption {
return []apic.ClientOption{
apic.WithEndpoint("ledger.api.antinvestor.com:443"),
apic.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
apic.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
func defaultLedgerClientOptions() []common.ClientOption {
return []common.ClientOption{
common.WithEndpoint("ledger.api.antinvestor.com:443"),
common.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
common.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
}
}

Expand Down Expand Up @@ -77,10 +76,10 @@ func InstantiateLedgerClient(clientConnection *grpc.ClientConn, ledgerServiceCli
// NewLedgerClient creates a new notification client.
//
// The service that an application uses to send and access received messages
func NewLedgerClient(ctx context.Context, opts ...apic.ClientOption) (*LedgerClient, error) {
func NewLedgerClient(ctx context.Context, opts ...common.ClientOption) (*LedgerClient, error) {
clientOpts := defaultLedgerClientOptions()

connPool, err := apic.DialConnection(ctx, append(clientOpts, opts...)...)
connPool, err := common.DialConnection(ctx, append(clientOpts, opts...)...)
if err != nil {
return nil, err
}
Expand All @@ -99,7 +98,7 @@ func (nc *LedgerClient) Close() error {
// the `x-goog-api-client` header passed on each request. Intended for
// use by Google-written clients.
func (nc *LedgerClient) setClientInfo(keyval ...string) {
kv := append([]string{"gl-go", apic.VersionGo()}, keyval...)
kv := append([]string{"gl-go", common.VersionGo()}, keyval...)
kv = append(kv, "grpc", grpc.Version)
nc.xMetadata = metadata.Pairs("x-ai-api-client", apic.XAntHeader(kv...))
nc.xMetadata = metadata.Pairs("x-ai-api-client", common.XAntHeader(kv...))
}
2 changes: 2 additions & 0 deletions go/lostid/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/antinvestor/apis/go/lostid

go 1.21

replace github.com/antinvestor/apis/go/common => ../common

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1
go.uber.org/mock v0.4.0
Expand Down
4 changes: 3 additions & 1 deletion go/notification/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module github.com/antinvestor/apis/go/notification

go 1.21

replace github.com/antinvestor/apis/go/common => ../common

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1
github.com/antinvestor/apis/go/common v1.7.20
github.com/antinvestor/apis/go/common v1.7.21
go.uber.org/mock v0.4.0
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.32.0
Expand Down
2 changes: 0 additions & 2 deletions go/notification/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1 h1:u0olL4yf2p7Tl5jfsAK5keaFi+JFJuv1CDHrbiXkxkk=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1/go.mod h1:tiTMKD8j6Pd/D2WzREoweufjzaJKHZg35f/VGcZ2v3I=
github.com/antinvestor/apis/go/common v1.7.20 h1:Vc0uloYy2cXNDuNCxheIoifHANyBexnhaJaumGhDHFA=
github.com/antinvestor/apis/go/common v1.7.20/go.mod h1:j4b9mKVLMGFFaA7TyHIFE/KfmOyuMBwBfoDqdBY+6X0=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
21 changes: 10 additions & 11 deletions go/notification/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ package notificationv1

import (
"context"
apic "github.com/antinvestor/apis/go/common"
commonv1 "github.com/antinvestor/apis/go/common/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"math"
)

const ctxKeyService = apic.CtxServiceKey("notificationClientKey")
const ctxKeyService = common.CtxServiceKey("notificationClientKey")

func defaultNotificationClientOptions() []apic.ClientOption {
return []apic.ClientOption{
apic.WithEndpoint("notification.api.antinvestor.com:443"),
apic.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
apic.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
func defaultNotificationClientOptions() []common.ClientOption {
return []common.ClientOption{
common.WithEndpoint("notification.api.antinvestor.com:443"),
common.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
common.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
}
}

Expand Down Expand Up @@ -77,10 +76,10 @@ func InstantiateNotificationClient(clientConnection *grpc.ClientConn, notificati
// NewNotificationClient creates a new notification client.
//
// The service that an application uses to send and access received messages
func NewNotificationClient(ctx context.Context, opts ...apic.ClientOption) (*NotificationClient, error) {
func NewNotificationClient(ctx context.Context, opts ...common.ClientOption) (*NotificationClient, error) {
clientOpts := defaultNotificationClientOptions()

connPool, err := apic.DialConnection(ctx, append(clientOpts, opts...)...)
connPool, err := common.DialConnection(ctx, append(clientOpts, opts...)...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,9 +108,9 @@ func (nc *NotificationClient) Service() NotificationServiceClient {
// the `x-goog-api-client` header passed on each request. Intended for
// use by Google-written clients.
func (nc *NotificationClient) setClientInfo(keyval ...string) {
kv := append([]string{"gl-go", apic.VersionGo()}, keyval...)
kv := append([]string{"gl-go", common.VersionGo()}, keyval...)
kv = append(kv, "grpc", grpc.Version)
nc.xMetadata = metadata.Pairs("x-ai-api-client", apic.XAntHeader(kv...))
nc.xMetadata = metadata.Pairs("x-ai-api-client", common.XAntHeader(kv...))
}

func (nc *NotificationClient) Send(ctx context.Context, accessID string, contactId string, contactDetail string,
Expand Down
4 changes: 3 additions & 1 deletion go/ocr/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module github.com/antinvestor/apis/go/ocr

go 1.21

replace github.com/antinvestor/apis/go/common => ../common

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1
github.com/antinvestor/apis/go/common v1.7.20
github.com/antinvestor/apis/go/common v1.7.21
go.uber.org/mock v0.4.0
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.32.0
Expand Down
2 changes: 0 additions & 2 deletions go/ocr/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1 h1:u0olL4yf2p7Tl5jfsAK5keaFi+JFJuv1CDHrbiXkxkk=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1/go.mod h1:tiTMKD8j6Pd/D2WzREoweufjzaJKHZg35f/VGcZ2v3I=
github.com/antinvestor/apis/go/common v1.7.20 h1:Vc0uloYy2cXNDuNCxheIoifHANyBexnhaJaumGhDHFA=
github.com/antinvestor/apis/go/common v1.7.20/go.mod h1:j4b9mKVLMGFFaA7TyHIFE/KfmOyuMBwBfoDqdBY+6X0=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
27 changes: 11 additions & 16 deletions go/ocr/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ package ocrv1

import (
"context"
apic "github.com/antinvestor/apis/go/common"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"math"
)

const ctxKeyService = apic.CtxServiceKey("ocrClientKey")
const ctxKeyService = common.CtxServiceKey("ocrClientKey")

func defaultProfileClientOptions() []apic.ClientOption {
return []apic.ClientOption{
apic.WithEndpoint("ocr.api.antinvestor.com:443"),
apic.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
apic.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
func defaultProfileClientOptions() []common.ClientOption {
return []common.ClientOption{
common.WithEndpoint("ocr.api.antinvestor.com:443"),
common.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
common.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
}
}

Expand All @@ -49,22 +48,18 @@ func FromContext(ctx context.Context) *OCRClient {
//
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
type OCRClient struct {
// gRPC connection to the service.
clientConn *grpc.ClientConn
common.GrpcClientBase

// The gRPC API client.
ocrClient OCRServiceClient

// The x-ant-* metadata to be sent with each request.
xMetadata metadata.MD
}

// NewOCRClient creates a new ocr client.
// The service that an application uses to perform ocr requests
func NewOCRClient(ctx context.Context, opts ...apic.ClientOption) (*OCRClient, error) {
func NewOCRClient(ctx context.Context, opts ...common.ClientOption) (*OCRClient, error) {
clientOpts := defaultProfileClientOptions()

connPool, err := apic.DialConnection(ctx, append(clientOpts, opts...)...)
connPool, err := common.DialConnection(ctx, append(clientOpts, opts...)...)
if err != nil {
return nil, err
}
Expand All @@ -88,9 +83,9 @@ func (pc *OCRClient) Close() error {
// the `x-goog-api-client` header passed on each request. Intended for
// use by Google-written clients.
func (pc *OCRClient) setClientInfo(keyval ...string) {
kv := append([]string{"gl-go", apic.VersionGo()}, keyval...)
kv := append([]string{"gl-go", common.VersionGo()}, keyval...)
kv = append(kv, "grpc", grpc.Version)
pc.xMetadata = metadata.Pairs("x-ai-api-client", apic.XAntHeader(kv...))
pc.xMetadata = metadata.Pairs("x-ai-api-client", common.XAntHeader(kv...))
}

func (pc *OCRClient) Recognize(ctx context.Context, id string, language string, properties map[string]string, fileId ...string) (*RecognizeResponse, error) {
Expand Down
4 changes: 3 additions & 1 deletion go/partition/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ module github.com/antinvestor/apis/go/partition

go 1.21

replace github.com/antinvestor/apis/go/common => ../common

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1
github.com/antinvestor/apis/go/common v1.7.20
github.com/antinvestor/apis/go/common v1.7.21
go.uber.org/mock v0.4.0
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.32.0
Expand Down
2 changes: 0 additions & 2 deletions go/partition/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1 h1:u0olL4yf2p7Tl5jfsAK5keaFi+JFJuv1CDHrbiXkxkk=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.32.0-20231115204500-e097f827e652.1/go.mod h1:tiTMKD8j6Pd/D2WzREoweufjzaJKHZg35f/VGcZ2v3I=
github.com/antinvestor/apis/go/common v1.7.20 h1:Vc0uloYy2cXNDuNCxheIoifHANyBexnhaJaumGhDHFA=
github.com/antinvestor/apis/go/common v1.7.20/go.mod h1:j4b9mKVLMGFFaA7TyHIFE/KfmOyuMBwBfoDqdBY+6X0=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
21 changes: 10 additions & 11 deletions go/partition/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ package partitionv1

import (
"context"
apic "github.com/antinvestor/apis/go/common"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"math"
)

var ctxKeyService = apic.CtxServiceKey("partitionClientKey")
var ctxKeyService = common.CtxServiceKey("partitionClientKey")

func defaultPartitionClientOptions() []apic.ClientOption {
return []apic.ClientOption{
apic.WithEndpoint("partitions.api.antinvestor.com:443"),
apic.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
apic.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
func defaultPartitionClientOptions() []common.ClientOption {
return []common.ClientOption{
common.WithEndpoint("partitions.api.antinvestor.com:443"),
common.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
common.WithGRPCDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32))),
}
}

Expand Down Expand Up @@ -76,10 +75,10 @@ func InstantiatePartitionsClient(

// NewPartitionsClient creates a new partitions client.
// / The service that an application uses to access and manipulate partition information
func NewPartitionsClient(ctx context.Context, opts ...apic.ClientOption) (*PartitionClient, error) {
func NewPartitionsClient(ctx context.Context, opts ...common.ClientOption) (*PartitionClient, error) {
clientOpts := defaultPartitionClientOptions()

connPool, err := apic.DialConnection(ctx, append(clientOpts, opts...)...)
connPool, err := common.DialConnection(ctx, append(clientOpts, opts...)...)
if err != nil {
return nil, err
}
Expand All @@ -97,9 +96,9 @@ func (partCl *PartitionClient) Close() error {
// the `x-goog-api-client` header passed on each request. Intended for
// use by Google-written clients.
func (partCl *PartitionClient) setClientInfo(keyval ...string) {
kv := append([]string{"gl-go", apic.VersionGo()}, keyval...)
kv := append([]string{"gl-go", common.VersionGo()}, keyval...)
kv = append(kv, "grpc", grpc.Version)
partCl.xMetadata = metadata.Pairs("x-ai-api-client", apic.XAntHeader(kv...))
partCl.xMetadata = metadata.Pairs("x-ai-api-client", common.XAntHeader(kv...))
}

func (partCl *PartitionClient) getService() PartitionServiceClient {
Expand Down

0 comments on commit 951fdfe

Please sign in to comment.