@@ -20,9 +20,12 @@ package client
20
20
21
21
import (
22
22
"context"
23
+ "crypto/tls"
24
+ "crypto/x509"
23
25
"errors"
24
26
"fmt"
25
27
"net/http"
28
+ "os"
26
29
"strings"
27
30
28
31
"connectrpc.com/connect"
@@ -80,8 +83,7 @@ type Client struct {
80
83
client v1connect.YorkieServiceClient
81
84
options Options
82
85
clientOptions []connect.ClientOption
83
- // dialOptions []grpc.DialOption
84
- logger * zap.Logger
86
+ logger * zap.Logger
85
87
86
88
id * time.ActorID
87
89
key string
@@ -120,29 +122,22 @@ func New(opts ...Option) (*Client, error) {
120
122
k = xid .New ().String ()
121
123
}
122
124
125
+ conn := & http.Client {}
126
+ if options .CertFile != "" {
127
+ tlsConfig , err := newTLSConfigFromFile (options .CertFile , options .ServerNameOverride )
128
+ if err != nil {
129
+ return nil , fmt .Errorf ("create client tls from file: %w" , err )
130
+ }
131
+
132
+ conn .Transport = & http.Transport {TLSClientConfig : tlsConfig }
133
+ }
134
+
123
135
var clientOptions []connect.ClientOption
124
136
125
137
clientOptions = append (clientOptions , connect .WithInterceptors (NewAuthInterceptor (options .APIKey , options .Token )))
126
-
127
- //var dialOptions []grpc.DialOption
128
- //
129
- //transportCreds := grpc.WithTransportCredentials(insecure.NewCredentials())
130
- //if options.CertFile != "" {
131
- // creds, err := credentials.NewClientTLSFromFile(options.CertFile, options.ServerNameOverride)
132
- // if err != nil {
133
- // return nil, fmt.Errorf("create client tls from file: %w", err)
134
- // }
135
- // transportCreds = grpc.WithTransportCredentials(creds)
136
- //}
137
- //dialOptions = append(dialOptions, transportCreds)
138
- //
139
- //authInterceptor := NewAuthInterceptor(options.APIKey, options.Token)
140
- //dialOptions = append(dialOptions, grpc.WithUnaryInterceptor(authInterceptor.Unary()))
141
- //dialOptions = append(dialOptions, grpc.WithStreamInterceptor(authInterceptor.Stream()))
142
- //
143
- //if options.MaxCallRecvMsgSize != 0 {
144
- // dialOptions = append(dialOptions, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(options.MaxCallRecvMsgSize)))
145
- //}
138
+ if options .MaxCallRecvMsgSize != 0 {
139
+ clientOptions = append (clientOptions , connect .WithReadMaxBytes (options .MaxCallRecvMsgSize ))
140
+ }
146
141
147
142
logger := options .Logger
148
143
if logger == nil {
@@ -154,10 +149,10 @@ func New(opts ...Option) (*Client, error) {
154
149
}
155
150
156
151
return & Client {
152
+ conn : conn ,
157
153
clientOptions : clientOptions ,
158
- //dialOptions: dialOptions,
159
- options : options ,
160
- logger : logger ,
154
+ options : options ,
155
+ logger : logger ,
161
156
162
157
key : k ,
163
158
status : deactivated ,
@@ -185,7 +180,6 @@ func (c *Client) Dial(rpcAddr string) error {
185
180
rpcAddr = "http://" + rpcAddr
186
181
}
187
182
188
- c .conn = http .DefaultClient
189
183
c .client = v1connect .NewYorkieServiceClient (c .conn , rpcAddr , c .clientOptions ... )
190
184
191
185
return nil
@@ -197,9 +191,7 @@ func (c *Client) Close() error {
197
191
return err
198
192
}
199
193
200
- //if err := c.conn.Close(); err != nil {
201
- // return fmt.Errorf("close connection: %w", err)
202
- //}
194
+ c .conn .CloseIdleConnections ()
203
195
204
196
return nil
205
197
}
@@ -726,6 +718,20 @@ func (c *Client) broadcast(ctx context.Context, doc *document.Document, topic st
726
718
return nil
727
719
}
728
720
721
+ // NewClientTLSFromFile
722
+ func newTLSConfigFromFile (certFile , serverNameOverride string ) (* tls.Config , error ) {
723
+ b , err := os .ReadFile (certFile )
724
+ if err != nil {
725
+ return nil , err
726
+ }
727
+ cp := x509 .NewCertPool ()
728
+ if ! cp .AppendCertsFromPEM (b ) {
729
+ return nil , fmt .Errorf ("credentials: failed to append certificates" )
730
+ }
731
+
732
+ return & tls.Config {ServerName : serverNameOverride , RootCAs : cp }, nil
733
+ }
734
+
729
735
/**
730
736
* withShardKey returns a context with the given shard key in metadata.
731
737
*/
0 commit comments