1- // Package basehertz contains a base transport which is used by hertz transports .
2- package basehertz
1+ // Package hertz contains the hertz transport for the orb client .
2+ package hertz
33
44import (
55 "bytes"
@@ -11,6 +11,8 @@ import (
1111 hclient "github.com/cloudwego/hertz/pkg/app/client"
1212 "github.com/cloudwego/hertz/pkg/protocol"
1313 "github.com/cloudwego/hertz/pkg/protocol/consts"
14+ "github.com/hertz-contrib/http2/config"
15+ "github.com/hertz-contrib/http2/factory"
1416
1517 "github.com/go-orb/go-orb/client"
1618 "github.com/go-orb/go-orb/codecs"
@@ -20,6 +22,11 @@ import (
2022 "github.com/go-orb/plugins/client/orb"
2123)
2224
25+ func init () {
26+ orb .RegisterTransport ("hertzh2c" , NewH2CTransport )
27+ orb .RegisterTransport ("hertzhttp" , NewHTTPTransport )
28+ }
29+
2330//nolint:gochecknoglobals
2431var stdHeaders = []string {"Content-Length" , "Content-Type" , "Date" , "Server" }
2532
@@ -59,33 +66,29 @@ func (t *Transport) Name() string {
5966// Request does the actual rpc request to the server.
6067func (t * Transport ) Request (
6168 ctx context.Context ,
62- req * client.Req [any , any ],
69+ infos client.RequestInfos ,
70+ req any ,
6371 result any ,
6472 opts * client.CallOptions ,
6573) error {
66- codec , err := codecs .GetEncoder (opts .ContentType , req . Req () )
74+ codec , err := codecs .GetEncoder (opts .ContentType , req )
6775 if err != nil {
6876 return orberrors .ErrBadRequest .Wrap (err )
6977 }
7078
7179 // Encode the request into a *bytes.Buffer{}.
7280 buff := bytes .NewBuffer (nil )
73- if err := codec .NewEncoder (buff ).Encode (req . Req () ); err != nil {
81+ if err := codec .NewEncoder (buff ).Encode (req ); err != nil {
7482 return orberrors .ErrBadRequest .Wrap (err )
7583 }
7684
77- node , err := req .Node (ctx , opts )
78- if err != nil {
79- return orberrors .From (err )
80- }
81-
8285 // Create a hertz request.
8386 hReq := & protocol.Request {}
8487 hReq .SetMethod (consts .MethodPost )
8588 hReq .SetBodyStream (buff , buff .Len ())
8689 hReq .Header .SetContentTypeBytes ([]byte (opts .ContentType ))
8790 hReq .Header .Set ("Accept" , opts .ContentType )
88- hReq .SetRequestURI (fmt .Sprintf ("%s://%s%s" , t .scheme , node .Address , req .Endpoint () ))
91+ hReq .SetRequestURI (fmt .Sprintf ("%s://%s%s" , t .scheme , infos .Address , infos .Endpoint ))
8992
9093 // Set metadata key=value to request headers.
9194 md , ok := metadata .Outgoing (ctx )
@@ -142,6 +145,12 @@ func (t *Transport) Request(
142145 return nil
143146}
144147
148+ // Stream creates a bidirectional stream to the service endpoint.
149+ // Hertz transport does not support streaming operations by default.
150+ func (t * Transport ) Stream (_ context.Context , _ client.RequestInfos , _ * client.CallOptions ) (client.StreamIface [any , any ], error ) {
151+ return nil , orberrors .HTTP (501 ).Wrap (client .ErrStreamNotSupported )
152+ }
153+
145154// NewTransport creates a Transport with a custom http.Client.
146155func NewTransport (name string , logger log.Logger , scheme string , clientCreator TransportClientCreator ,
147156) (orb.TransportType , error ) {
@@ -152,3 +161,40 @@ func NewTransport(name string, logger log.Logger, scheme string, clientCreator T
152161 clientCreator : clientCreator ,
153162 }}, nil
154163}
164+
165+ // NewH2CTransport creates a new hertz http transport for the orb client.
166+ func NewH2CTransport (logger log.Logger , cfg * orb.Config ) (orb.TransportType , error ) {
167+ return NewTransport (
168+ "hertzh2c" ,
169+ logger ,
170+ "http" ,
171+ func () (* hclient.Client , error ) {
172+ c , err := hclient .NewClient (
173+ hclient .WithNoDefaultUserAgentHeader (true ),
174+ hclient .WithMaxConnsPerHost (cfg .PoolSize ),
175+ )
176+ if err != nil {
177+ return nil , err
178+ }
179+
180+ c .SetClientFactory (factory .NewClientFactory (config .WithAllowHTTP (true )))
181+
182+ return c , nil
183+ },
184+ )
185+ }
186+
187+ // NewHTTPTransport creates a new hertz http transport for the orb client.
188+ func NewHTTPTransport (logger log.Logger , cfg * orb.Config ) (orb.TransportType , error ) {
189+ return NewTransport (
190+ "hertzhttp" ,
191+ logger ,
192+ "http" ,
193+ func () (* hclient.Client , error ) {
194+ return hclient .NewClient (
195+ hclient .WithNoDefaultUserAgentHeader (true ),
196+ hclient .WithMaxConnsPerHost (cfg .PoolSize ),
197+ )
198+ },
199+ )
200+ }
0 commit comments