github.com/cloudwego/kitex@v0.9.0/client/streamclient/client_option.go (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package streamclient
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/cloudwego/kitex/client"
    24  	"github.com/cloudwego/kitex/pkg/discovery"
    25  	"github.com/cloudwego/kitex/pkg/endpoint"
    26  	"github.com/cloudwego/kitex/pkg/http"
    27  	"github.com/cloudwego/kitex/pkg/loadbalance"
    28  	"github.com/cloudwego/kitex/pkg/loadbalance/lbcache"
    29  	"github.com/cloudwego/kitex/pkg/remote"
    30  	"github.com/cloudwego/kitex/pkg/stats"
    31  	"github.com/cloudwego/kitex/pkg/warmup"
    32  	"github.com/cloudwego/kitex/pkg/xds"
    33  )
    34  
    35  // These options are directly translated from client.Option(s). If you can't find the option with the
    36  // same name in client.Option(s), most probably it means it's not for streaming clients.
    37  
    38  // WithSuite adds an option suite for client.
    39  func WithSuite(suite client.Suite) Option {
    40  	return ConvertOptionFrom(client.WithSuite(suite))
    41  }
    42  
    43  // WithMiddleware adds middleware for client to handle request.
    44  // NOTE: for streaming APIs (bidirectional, client, server), req is not valid, resp is *streaming.Result
    45  // If you want to intercept recv/send calls, please use Recv/Send Middleware
    46  func WithMiddleware(mw endpoint.Middleware) Option {
    47  	return ConvertOptionFrom(client.WithMiddleware(mw))
    48  }
    49  
    50  // WithMiddlewareBuilder adds middleware that depend on a per-client context for client to handle request
    51  // NOTE: for streaming APIs (bidirectional, client, server), req is not valid, resp is *streaming.Result
    52  // If you want to intercept recv/send calls, please use Recv/Send MiddlewareBuilder
    53  func WithMiddlewareBuilder(mwb endpoint.MiddlewareBuilder) Option {
    54  	return ConvertOptionFrom(client.WithMiddlewareBuilder(mwb))
    55  }
    56  
    57  // WithInstanceMW adds middleware for client to handle request after service discovery and loadbalance process.
    58  func WithInstanceMW(mw endpoint.Middleware) Option {
    59  	return ConvertOptionFrom(client.WithInstanceMW(mw))
    60  }
    61  
    62  // WithDestService specifies the name of target service.
    63  func WithDestService(svr string) Option {
    64  	return ConvertOptionFrom(client.WithDestService(svr))
    65  }
    66  
    67  // WithHostPorts sets the host ports for a stream client.
    68  func WithHostPorts(hostPorts ...string) Option {
    69  	return ConvertOptionFrom(client.WithHostPorts(hostPorts...))
    70  }
    71  
    72  // WithResolver provides the Resolver for kitex client.
    73  func WithResolver(r discovery.Resolver) Option {
    74  	return ConvertOptionFrom(client.WithResolver(r))
    75  }
    76  
    77  // WithHTTPResolver specifies resolver for url (which specified by WithURL).
    78  func WithHTTPResolver(r http.Resolver) Option {
    79  	return ConvertOptionFrom(client.WithHTTPResolver(r))
    80  }
    81  
    82  // WithLoadBalancer sets the loadbalancer for client.
    83  func WithLoadBalancer(lb loadbalance.Loadbalancer, opts ...*lbcache.Options) Option {
    84  	return ConvertOptionFrom(client.WithLoadBalancer(lb, opts...))
    85  }
    86  
    87  // WithConnectTimeout specifies the connection timeout.
    88  func WithConnectTimeout(d time.Duration) Option {
    89  	return ConvertOptionFrom(client.WithConnectTimeout(d))
    90  }
    91  
    92  // WithTag sets the customize tag for service discovery, eg: idc, cluster.
    93  func WithTag(key, val string) Option {
    94  	return ConvertOptionFrom(client.WithTag(key, val))
    95  }
    96  
    97  // WithTracer adds a tracer to client.
    98  func WithTracer(c stats.Tracer) Option {
    99  	return ConvertOptionFrom(client.WithTracer(c))
   100  }
   101  
   102  // WithStatsLevel sets the stats level for client.
   103  func WithStatsLevel(level stats.Level) Option {
   104  	return ConvertOptionFrom(client.WithStatsLevel(level))
   105  }
   106  
   107  // WithPayloadCodec to set a payloadCodec that handle other payload which not support by kitex
   108  func WithPayloadCodec(c remote.PayloadCodec) Option {
   109  	return ConvertOptionFrom(client.WithPayloadCodec(c))
   110  }
   111  
   112  // WithConnReporterEnabled to enable reporting connection pool stats.
   113  func WithConnReporterEnabled() Option {
   114  	return ConvertOptionFrom(client.WithConnReporterEnabled())
   115  }
   116  
   117  // WithWarmingUp forces the client to do some warm-ups at the end of the initialization.
   118  func WithWarmingUp(wuo *warmup.ClientOption) Option {
   119  	return ConvertOptionFrom(client.WithWarmingUp(wuo))
   120  }
   121  
   122  // WithXDSSuite is used to set the xds suite for the client.
   123  func WithXDSSuite(suite xds.ClientSuite) Option {
   124  	return ConvertOptionFrom(client.WithXDSSuite(suite))
   125  }
   126  
   127  // WithContextBackup enables local-session to retrieve context backuped by server,
   128  // in case of user don't correctly pass context into next RPC call.
   129  //   - backupHandler pass a handler to check and handler user-defined key-values according to current context, returning backup==false means no need further operations.
   130  func WithContextBackup(backupHandler func(prev, cur context.Context) (ctx context.Context, backup bool)) Option {
   131  	return ConvertOptionFrom(client.WithContextBackup(backupHandler))
   132  }