├── .codecov.yml ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ ├── pr-check.yml │ └── tests.yml ├── .gitignore ├── .golangci.yaml ├── .licenserc.yaml ├── .typos.toml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE ├── README.md ├── README_cn.md ├── ROADMAP.md ├── client ├── callopt │ ├── options.go │ ├── options_test.go │ └── streamcall │ │ ├── call_options.go │ │ ├── call_options_test.go │ │ ├── definition.go │ │ └── grpc_options.go ├── client.go ├── client_test.go ├── context.go ├── context_middleware.go ├── context_middleware_test.go ├── context_test.go ├── deprecated.go ├── doc.go ├── genericclient │ ├── client.go │ ├── generic_stream_service.go │ └── stream.go ├── middlewares.go ├── middlewares_test.go ├── mocks_test.go ├── option.go ├── option_advanced.go ├── option_stream.go ├── option_test.go ├── option_ttstream.go ├── option_unary.go ├── rpctimeout.go ├── rpctimeout_pool.go ├── rpctimeout_pool_test.go ├── rpctimeout_test.go ├── service_inline.go ├── service_inline_test.go ├── stream.go ├── stream_test.go └── streamclient │ ├── client_option.go │ ├── client_option_advanced.go │ ├── definition.go │ ├── grpc_option.go │ └── option.go ├── go.mod ├── go.sum ├── images ├── lark_group.png └── lark_group_cn.png ├── internal ├── client │ ├── lock.go │ ├── option.go │ ├── option_ttstream.go │ ├── remote_option.go │ └── remote_option_windows.go ├── configutil │ ├── config.go │ ├── config_test.go │ ├── once.go │ └── once_test.go ├── generic │ ├── generic_service.go │ └── proto │ │ └── type.go ├── mocks │ ├── README.md │ ├── conn.go │ ├── diagnosis │ │ └── service.go │ ├── discovery │ │ └── discovery.go │ ├── doc.go │ ├── generic │ │ ├── generic_service.go │ │ └── thrift.go │ ├── klog │ │ └── log.go │ ├── limiter │ │ └── limiter.go │ ├── loadbalance │ │ └── loadbalancer.go │ ├── net │ │ └── net.go │ ├── netpoll │ │ ├── connection.go │ │ └── nocopy.go │ ├── proto │ │ ├── kitex_gen │ │ │ └── pbapi │ │ │ │ └── mock │ │ │ │ ├── client.go │ │ │ │ ├── invoker.go │ │ │ │ ├── mock.go │ │ │ │ ├── pbapi.pb.fast.go │ │ │ │ ├── pbapi.pb.go │ │ │ │ └── server.go │ │ └── pbapi.proto │ ├── proxy │ │ └── proxy.go │ ├── remote │ │ ├── bytebuf.go │ │ ├── codec.go │ │ ├── conn_wrapper.go │ │ ├── connpool.go │ │ ├── dialer.go │ │ ├── payload_codec.go │ │ ├── servicesearcher.go │ │ ├── trans_handler.go │ │ ├── trans_meta.go │ │ └── trans_pipeline.go │ ├── rpc_info │ │ └── timeout_provider.go │ ├── serviceinfo.go │ ├── stats │ │ └── tracer.go │ ├── thrift │ │ ├── gen.sh │ │ ├── k-consts.go │ │ ├── k-stream.go │ │ ├── k-test.go │ │ ├── stream.go │ │ ├── stream.thrift │ │ ├── test.go │ │ └── test.thrift │ ├── transhandlerclient.go │ ├── transhandlerserver.go │ ├── update.sh │ └── utils │ │ └── sharedticker.go ├── reusable.go ├── server │ ├── config.go │ ├── option.go │ ├── register_option.go │ ├── remote_option.go │ └── remote_option_windows.go ├── stream │ ├── stream_option.go │ └── stream_option_test.go ├── test │ ├── assert.go │ ├── assert_test.go │ └── port.go └── utils │ └── safemcache │ ├── safemcache.go │ └── safemcache_test.go ├── licenses ├── LICENSE-gjson ├── LICENSE-go-genproto-main ├── LICENSE-grpc.txt ├── LICENSE-httprouter ├── LICENSE-json-iterator.txt ├── LICENSE-pid ├── LICENSE-protobuf.txt ├── LICENSE-thrift ├── LICENSE-xxhash.txt └── LICENSE-yaml.v3 ├── pkg ├── acl │ ├── acl.go │ └── acl_test.go ├── circuitbreak │ ├── cbsuite.go │ ├── cbsuite_option.go │ ├── cbsuite_option_test.go │ ├── cbsuite_test.go │ ├── circuitbreak.go │ ├── circuitbreak_test.go │ ├── default.go │ ├── default_test.go │ ├── doc.go │ └── item_circuit_breaker.go ├── connpool │ ├── config.go │ └── config_test.go ├── consts │ └── ctx.go ├── diagnosis │ ├── interface.go │ └── interface_test.go ├── discovery │ ├── constants.go │ ├── discovery.go │ └── discovery_test.go ├── endpoint │ ├── cep │ │ └── endpoint.go │ ├── deprecated.go │ ├── endpoint.go │ ├── endpoint_test.go │ ├── sep │ │ └── endpoint.go │ └── unary_endpoint.go ├── event │ ├── bus.go │ ├── bus_test.go │ ├── event.go │ ├── queue.go │ └── queue_test.go ├── exception │ ├── deprecated.go │ └── deprecated_test.go ├── fallback │ ├── fallback.go │ └── fallback_test.go ├── generic │ ├── binary_test │ │ ├── generic_init.go │ │ └── generic_test.go │ ├── binarypb_codec.go │ ├── binarypb_codec_test.go │ ├── binarythrift_codec.go │ ├── binarythrift_codec_test.go │ ├── closer.go │ ├── descriptor │ │ ├── annotation.go │ │ ├── descriptor.go │ │ ├── field_mapping.go │ │ ├── field_mapping_test.go │ │ ├── http.go │ │ ├── http_mapping.go │ │ ├── http_test.go │ │ ├── render.go │ │ ├── route.go │ │ ├── router.go │ │ ├── tree.go │ │ ├── tree_test.go │ │ ├── type.go │ │ ├── type_test.go │ │ ├── util.go │ │ ├── value_mapping.go │ │ └── value_mapping_test.go │ ├── descriptor_provider.go │ ├── generic.go │ ├── generic_service.go │ ├── generic_service_test.go │ ├── generic_test.go │ ├── grpcjson_test │ │ ├── generic_init.go │ │ ├── generic_test.go │ │ └── idl │ │ │ └── api.thrift │ ├── grpcjsonpb_test │ │ ├── generic_init.go │ │ ├── generic_test.go │ │ └── idl │ │ │ ├── pbapi.proto │ │ │ └── pbapi_multi_service.proto │ ├── http_test │ │ ├── conf │ │ │ └── kitex.yml │ │ ├── generic_init.go │ │ ├── generic_test.go │ │ └── idl │ │ │ ├── baseline.thrift │ │ │ ├── binary_echo.thrift │ │ │ ├── dynamicgo_go_tag_error.thrift │ │ │ ├── http_annotation.thrift │ │ │ └── mock.thrift │ ├── httppb_test │ │ ├── conf │ │ │ └── kitex.yml │ │ ├── generic_init.go │ │ ├── generic_test.go │ │ └── idl │ │ │ ├── echo.pb.go │ │ │ ├── echo.proto │ │ │ └── echo.thrift │ ├── httppbthrift_codec.go │ ├── httppbthrift_codec_test.go │ ├── httpthrift_codec.go │ ├── httpthrift_codec_test.go │ ├── json_test │ │ ├── conf │ │ │ └── kitex.yml │ │ ├── generic_init.go │ │ ├── generic_test.go │ │ └── idl │ │ │ ├── base.thrift │ │ │ ├── baseline.thrift │ │ │ ├── binary_echo.thrift │ │ │ ├── example.thrift │ │ │ ├── example_multi_service.thrift │ │ │ ├── extend.thrift │ │ │ ├── mock.thrift │ │ │ ├── mock_unknown_method.thrift │ │ │ └── self_ref.thrift │ ├── jsonpb_codec.go │ ├── jsonpb_codec_test.go │ ├── jsonpb_test │ │ ├── data │ │ │ └── example2_pb.bin │ │ ├── generic_init.go │ │ ├── generic_test.go │ │ └── idl │ │ │ ├── echo.proto │ │ │ ├── echo_import.proto │ │ │ ├── example.proto │ │ │ └── example2.proto │ ├── jsonthrift_codec.go │ ├── jsonthrift_codec_test.go │ ├── map_test │ │ ├── conf │ │ │ └── kitex.yml │ │ ├── generic_init.go │ │ ├── generic_test.go │ │ └── idl │ │ │ ├── base.thrift │ │ │ ├── example.thrift │ │ │ ├── mock.thrift │ │ │ └── self_ref.thrift │ ├── mapthrift_codec.go │ ├── mapthrift_codec_test.go │ ├── option.go │ ├── option_test.go │ ├── pb_descriptor_provider.go │ ├── pbidl_provider.go │ ├── pbidl_provider_test.go │ ├── proto │ │ ├── json.go │ │ ├── json_test.go │ │ ├── protobuf.go │ │ ├── raw.go │ │ ├── raw_test.go │ │ └── type.go │ ├── reflect_test │ │ ├── idl │ │ │ ├── base.thrift │ │ │ ├── example.thrift │ │ │ └── self_ref.thrift │ │ ├── map_test.go │ │ └── reflect_test.go │ ├── thrift │ │ ├── base.go │ │ ├── base_test.go │ │ ├── binary.go │ │ ├── http.go │ │ ├── http_pb.go │ │ ├── json.go │ │ ├── parse.go │ │ ├── parse_option.go │ │ ├── parse_test.go │ │ ├── read.go │ │ ├── read_test.go │ │ ├── struct.go │ │ ├── thrift.go │ │ ├── util.go │ │ ├── util_test.go │ │ ├── write.go │ │ └── write_test.go │ ├── thriftidl_provider.go │ ├── thriftidl_provider_option.go │ └── thriftidl_provider_test.go ├── gofunc │ ├── go.go │ └── go_test.go ├── http │ ├── resolver.go │ └── resolver_test.go ├── kerrors │ ├── bizerrors.go │ ├── bizerrors_test.go │ ├── kerrors.go │ ├── kerrors_test.go │ └── streaming_errors.go ├── klog │ ├── default.go │ ├── default_test.go │ └── log.go ├── limit │ ├── limit.go │ └── limit_test.go ├── limiter │ ├── connection_limiter.go │ ├── connection_limiter_test.go │ ├── dummy.go │ ├── item_limiter.go │ ├── limiter.go │ ├── limiter_test.go │ ├── qps_limiter.go │ └── qps_limiter_test.go ├── loadbalance │ ├── consist.go │ ├── consist_test.go │ ├── dummy_picker.go │ ├── interleaved_weighted_round_robin.go │ ├── iterator.go │ ├── lbcache │ │ ├── cache.go │ │ ├── cache_test.go │ │ ├── hookable.go │ │ ├── hookable_test.go │ │ └── shared_ticker.go │ ├── loadbalancer.go │ ├── weighted_balancer.go │ ├── weighted_balancer_test.go │ ├── weighted_random.go │ ├── weighted_random_with_alias_method.go │ ├── weighted_round_robin.go │ └── weighted_round_robin_test.go ├── logid │ ├── logid.go │ ├── streaming.go │ └── streaming_test.go ├── mem │ ├── span.go │ └── span_test.go ├── profiler │ ├── profiler.go │ └── profiler_test.go ├── protocol │ └── bthrift │ │ ├── README.md │ │ ├── apache │ │ ├── apache.go │ │ ├── apache_test.go │ │ ├── binary_protocol.go │ │ ├── binary_protocol_test.go │ │ └── thrift.go │ │ ├── binary.go │ │ ├── binary_test.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── interface.go │ │ ├── internal │ │ └── test │ │ │ ├── README.md │ │ │ ├── assert.go │ │ │ └── assert_test.go │ │ ├── unknown.go │ │ └── unknown_test.go ├── proxy │ └── proxy.go ├── registry │ └── registry.go ├── remote │ ├── bound │ │ ├── limiter_inbound.go │ │ ├── limiter_inbound_test.go │ │ ├── transmeta_bound.go │ │ └── transmeta_bound_test.go │ ├── bufiox2buffer.go │ ├── bytebuf.go │ ├── codec.go │ ├── codec │ │ ├── bytebuf_util.go │ │ ├── default_codec.go │ │ ├── default_codec_test.go │ │ ├── grpc │ │ │ ├── grpc.go │ │ │ └── grpc_compress.go │ │ ├── header_codec.go │ │ ├── header_codec_test.go │ │ ├── perrors │ │ │ ├── protocol_error.go │ │ │ └── protocol_error_test.go │ │ ├── protobuf │ │ │ ├── encoding │ │ │ │ ├── encoding.go │ │ │ │ └── gzip │ │ │ │ │ └── gzip.go │ │ │ ├── error.pb.go │ │ │ ├── error.proto │ │ │ ├── pberror.go │ │ │ ├── protobuf.go │ │ │ ├── protobuf_test.go │ │ │ ├── test.pb.go │ │ │ └── test.proto │ │ ├── thrift │ │ │ ├── codec.go │ │ │ ├── codec_apache.go │ │ │ ├── codec_apache_test.go │ │ │ ├── codec_fast.go │ │ │ ├── codec_frugal.go │ │ │ ├── codec_frugal_test.go │ │ │ ├── thrift.go │ │ │ ├── thrift_data.go │ │ │ ├── thrift_data_test.go │ │ │ └── thrift_test.go │ │ ├── util.go │ │ ├── util_test.go │ │ ├── validate.go │ │ └── validate_test.go │ ├── compression.go │ ├── connpool.go │ ├── connpool │ │ ├── dummy.go │ │ ├── long_pool.go │ │ ├── long_pool_test.go │ │ ├── reporter.go │ │ ├── short_pool.go │ │ ├── short_pool_test.go │ │ └── utils.go │ ├── custom_meta_handler.go │ ├── custom_meta_handler_test.go │ ├── default_bytebuf.go │ ├── default_bytebuf_test.go │ ├── dialer.go │ ├── doc.go │ ├── message.go │ ├── option.go │ ├── payload_codec.go │ ├── profiler_meta.go │ ├── remotecli │ │ ├── client.go │ │ ├── client_test.go │ │ ├── conn_wrapper.go │ │ ├── conn_wrapper_test.go │ │ ├── stream.go │ │ └── stream_test.go │ ├── remotesvr │ │ ├── server.go │ │ └── server_test.go │ ├── role.go │ ├── trans │ │ ├── common.go │ │ ├── default_client_handler.go │ │ ├── default_client_handler_test.go │ │ ├── default_server_handler.go │ │ ├── default_server_handler_test.go │ │ ├── detection │ │ │ ├── noop.go │ │ │ ├── server_handler.go │ │ │ └── server_handler_test.go │ │ ├── gonet │ │ │ ├── bytebuffer.go │ │ │ ├── bytebuffer_test.go │ │ │ ├── client_handler.go │ │ │ ├── conn_extension.go │ │ │ ├── dialer.go │ │ │ ├── mocks_test.go │ │ │ ├── server_handler.go │ │ │ ├── server_handler_test.go │ │ │ ├── trans_server.go │ │ │ └── trans_server_test.go │ │ ├── invoke │ │ │ ├── conn_extension.go │ │ │ ├── conn_extension_test.go │ │ │ ├── invoke.go │ │ │ ├── invoke_handler.go │ │ │ ├── invoke_handler_test.go │ │ │ ├── message.go │ │ │ └── message_test.go │ │ ├── listen_config.go │ │ ├── listen_config_windows.go │ │ ├── mocks_test.go │ │ ├── netpoll │ │ │ ├── bytebuf.go │ │ │ ├── bytebuf_test.go │ │ │ ├── client_handler.go │ │ │ ├── conn_extension.go │ │ │ ├── dialer.go │ │ │ ├── http_client.go │ │ │ ├── http_client_handler.go │ │ │ ├── http_client_handler_test.go │ │ │ ├── mocks_test.go │ │ │ ├── server_handler.go │ │ │ ├── server_handler_test.go │ │ │ ├── trans_server.go │ │ │ └── trans_server_test.go │ │ ├── netpollmux │ │ │ ├── client_handler.go │ │ │ ├── client_handler_test.go │ │ │ ├── control_frame.go │ │ │ ├── mocks_test.go │ │ │ ├── mux_conn.go │ │ │ ├── mux_conn_test.go │ │ │ ├── mux_pool.go │ │ │ ├── mux_pool_test.go │ │ │ ├── mux_transport.go │ │ │ ├── mux_transport_test.go │ │ │ ├── server_handler.go │ │ │ ├── server_handler_test.go │ │ │ ├── shard_map.go │ │ │ └── shard_map_test.go │ │ ├── nphttp2 │ │ │ ├── buffer.go │ │ │ ├── buffer_test.go │ │ │ ├── client_conn.go │ │ │ ├── client_conn_test.go │ │ │ ├── client_handler.go │ │ │ ├── client_handler_test.go │ │ │ ├── codes.go │ │ │ ├── codes │ │ │ │ ├── code_string.go │ │ │ │ ├── codes.go │ │ │ │ └── codes_test.go │ │ │ ├── codes_test.go │ │ │ ├── conn_pool.go │ │ │ ├── conn_pool_test.go │ │ │ ├── doc.go │ │ │ ├── grpc │ │ │ │ ├── bdp_estimator.go │ │ │ │ ├── bdp_estimator_test.go │ │ │ │ ├── context.go │ │ │ │ ├── context_test.go │ │ │ │ ├── controlbuf.go │ │ │ │ ├── controlbuf_test.go │ │ │ │ ├── defaults.go │ │ │ │ ├── doc.go │ │ │ │ ├── flowcontrol.go │ │ │ │ ├── flowcontrol_test.go │ │ │ │ ├── framer.go │ │ │ │ ├── graceful_shutdown_test.go │ │ │ │ ├── grpcframe │ │ │ │ │ ├── errors.go │ │ │ │ │ ├── errors_test.go │ │ │ │ │ ├── frame_parser.go │ │ │ │ │ ├── frame_reader.go │ │ │ │ │ ├── frame_reader_test.go │ │ │ │ │ └── frame_writer.go │ │ │ │ ├── http2_client.go │ │ │ │ ├── http2_server.go │ │ │ │ ├── http_util.go │ │ │ │ ├── http_util_test.go │ │ │ │ ├── keepalive.go │ │ │ │ ├── keepalive_test.go │ │ │ │ ├── mocks_test.go │ │ │ │ ├── syscall │ │ │ │ │ ├── syscall_linux.go │ │ │ │ │ └── syscall_nonlinux.go │ │ │ │ ├── testutils │ │ │ │ │ ├── channel.go │ │ │ │ │ ├── leakcheck │ │ │ │ │ │ ├── leakcheck.go │ │ │ │ │ │ └── leakcheck_test.go │ │ │ │ │ ├── status_equal.go │ │ │ │ │ └── status_equal_test.go │ │ │ │ ├── transport.go │ │ │ │ └── transport_test.go │ │ │ ├── meta_api.go │ │ │ ├── meta_api_test.go │ │ │ ├── metadata │ │ │ │ ├── metadata.go │ │ │ │ └── metadata_test.go │ │ │ ├── mocks_test.go │ │ │ ├── peer │ │ │ │ ├── peer.go │ │ │ │ └── peer_test.go │ │ │ ├── server_conn.go │ │ │ ├── server_conn_test.go │ │ │ ├── server_handler.go │ │ │ ├── server_handler_test.go │ │ │ ├── status │ │ │ │ ├── mock_test.go │ │ │ │ ├── status.go │ │ │ │ └── status_test.go │ │ │ ├── stream.go │ │ │ └── stream_test.go │ │ └── ttstream │ │ │ ├── client_provider.go │ │ │ ├── client_provider_option.go │ │ │ ├── client_provider_test.go │ │ │ ├── client_trans_pool.go │ │ │ ├── client_trans_pool_longconn.go │ │ │ ├── client_trans_pool_muxconn.go │ │ │ ├── client_trans_pool_shortconn.go │ │ │ ├── container │ │ │ ├── linklist.go │ │ │ ├── object_pool.go │ │ │ ├── object_pool_test.go │ │ │ ├── pipe.go │ │ │ ├── pipe_test.go │ │ │ ├── queue.go │ │ │ ├── queue_test.go │ │ │ ├── stack.go │ │ │ └── stack_test.go │ │ │ ├── exception.go │ │ │ ├── exception_test.go │ │ │ ├── frame.go │ │ │ ├── frame_handler.go │ │ │ ├── frame_test.go │ │ │ ├── ktx │ │ │ ├── ktx.go │ │ │ └── ktx_test.go │ │ │ ├── metadata.go │ │ │ ├── mock_test.go │ │ │ ├── server_handler.go │ │ │ ├── server_handler_test.go │ │ │ ├── server_provider.go │ │ │ ├── server_provider_option.go │ │ │ ├── stream.go │ │ │ ├── stream_client.go │ │ │ ├── stream_reader.go │ │ │ ├── stream_reader_test.go │ │ │ ├── stream_server.go │ │ │ ├── stream_test.go │ │ │ ├── stream_writer.go │ │ │ ├── test_utils.go │ │ │ ├── transport.go │ │ │ ├── transport_buffer.go │ │ │ ├── transport_buffer_test.go │ │ │ └── transport_test.go │ ├── trans_errors.go │ ├── trans_errors_test.go │ ├── trans_handler.go │ ├── trans_meta.go │ ├── trans_pipeline.go │ ├── trans_server.go │ └── transmeta │ │ ├── http_metakey.go │ │ └── metakey.go ├── retry │ ├── backup.go │ ├── backup_retryer.go │ ├── backup_test.go │ ├── failure.go │ ├── failure_retryer.go │ ├── failure_test.go │ ├── item_retry.go │ ├── mixed.go │ ├── mixed_retryer.go │ ├── mixed_test.go │ ├── percentage_limit.go │ ├── policy.go │ ├── policy_test.go │ ├── retryer.go │ ├── retryer_test.go │ ├── util.go │ └── util_test.go ├── rpcinfo │ ├── basicinfo.go │ ├── convert.go │ ├── convert_test.go │ ├── copy.go │ ├── copy_test.go │ ├── ctx.go │ ├── ctx_test.go │ ├── endpointInfo.go │ ├── endpointInfo_test.go │ ├── interface.go │ ├── invocation.go │ ├── invocation_test.go │ ├── mocks_test.go │ ├── mutable.go │ ├── remoteinfo │ │ ├── remoteInfo.go │ │ └── remoteInfo_test.go │ ├── rpcconfig.go │ ├── rpcconfig_test.go │ ├── rpcinfo.go │ ├── rpcinfo_test.go │ ├── rpcstats.go │ ├── rpcstats_test.go │ ├── stats_util.go │ ├── stats_util_test.go │ ├── tracer.go │ └── tracer_test.go ├── rpctimeout │ ├── item_rpc_timeout.go │ └── rpctimeout.go ├── serviceinfo │ ├── serviceinfo.go │ └── serviceinfo_test.go ├── stats │ ├── event.go │ ├── event_test.go │ ├── status.go │ └── tracer.go ├── streaming │ ├── context.go │ ├── streaming.go │ ├── streamx.go │ ├── timeout.go │ ├── timeout_test.go │ ├── util.go │ └── util_test.go ├── transmeta │ ├── http2.go │ ├── http2_test.go │ ├── metainfo.go │ ├── metainfo_test.go │ ├── ttheader.go │ └── ttheader_test.go ├── utils │ ├── byte2str.go │ ├── byte2str_test.go │ ├── config.go │ ├── config_test.go │ ├── contextmap │ │ ├── contextmap.go │ │ └── contextmap_test.go │ ├── counter.go │ ├── counter_test.go │ ├── err_chain.go │ ├── err_chain_test.go │ ├── fastthrift │ │ ├── fastthrift.go │ │ └── fastthrift_test.go │ ├── func.go │ ├── func_test.go │ ├── int_len.go │ ├── int_len_test.go │ ├── interface.go │ ├── json.go │ ├── json_fuzz_test.go │ ├── json_sonic.go │ ├── json_test.go │ ├── kitexutil │ │ ├── kitexutil.go │ │ └── kitexutil_test.go │ ├── max_counter.go │ ├── max_counter_test.go │ ├── netaddr.go │ ├── netaddr_test.go │ ├── ring.go │ ├── ring_single.go │ ├── ring_test.go │ ├── rpcstats.go │ ├── rpcstats_test.go │ ├── runtimex.go │ ├── runtimex_test.go │ ├── sharedticker.go │ ├── sharedticker_test.go │ ├── slice.go │ ├── slice_test.go │ ├── strbuf.go │ ├── strbuf_test.go │ ├── strings.go │ ├── strings_test.go │ ├── thrift.go │ ├── thrift_test.go │ ├── yaml.go │ └── yaml_test.go ├── warmup │ ├── pool_helper.go │ ├── pool_helper_test.go │ └── warmup.go └── xds │ └── xds.go ├── server ├── deprecated.go ├── genericserver │ ├── server.go │ └── server_test.go ├── hooks.go ├── invoke.go ├── invoke │ ├── message.go │ └── message_test.go ├── invoke_test.go ├── middlewares.go ├── middlewares_test.go ├── mocks_test.go ├── option.go ├── option_advanced.go ├── option_advanced_test.go ├── option_stream.go ├── option_test.go ├── option_ttstream.go ├── option_unary.go ├── register_option.go ├── register_option_test.go ├── server.go ├── server_test.go ├── service.go ├── service_inline.go ├── service_inline_test.go ├── service_test.go ├── stream.go └── stream_test.go ├── tool ├── cmd │ └── kitex │ │ ├── args │ │ ├── args.go │ │ ├── args_test.go │ │ ├── deprecated.go │ │ └── deprecated_test.go │ │ ├── main.go │ │ ├── sdk │ │ └── kitex_sdk.go │ │ ├── utils │ │ └── utils.go │ │ └── versions │ │ ├── dependencies.go │ │ ├── dependencies_test.go │ │ ├── version.go │ │ └── version_test.go └── internal_pkg │ ├── doc.go │ ├── generator │ ├── completer.go │ ├── custom_template.go │ ├── feature.go │ ├── generator.go │ ├── generator_test.go │ ├── template.go │ ├── template_test.go │ ├── type.go │ └── type_test.go │ ├── log │ └── log.go │ ├── pluginmode │ ├── protoc │ │ ├── plugin.go │ │ ├── protoc.go │ │ └── util.go │ └── thriftgo │ │ ├── ast.go │ │ ├── convertor.go │ │ ├── file_tpl.go │ │ ├── hessian2.go │ │ ├── hessian2_test.go │ │ ├── patcher.go │ │ ├── plugin.go │ │ ├── register_tpl.go │ │ └── struct_tpl.go │ ├── prutal │ ├── prutal.go │ ├── prutal_test.go │ └── util.go │ ├── tpl │ ├── bootstrap.sh.go │ ├── build.sh.go │ ├── client.go │ ├── client_v2.go │ ├── handler.go │ ├── handler.method.go │ ├── invoker.go │ ├── main.go │ ├── pbtpl │ │ ├── pbtpl.go │ │ └── pbtpl_test.go │ ├── server.go │ ├── service.go │ ├── service_v2.go │ ├── templates_test.go │ └── tool_version.go │ └── util │ ├── dump.go │ ├── env │ ├── env.go │ └── env_test.go │ ├── util.go │ └── util_test.go ├── transport └── keys.go └── version.go /.codecov.yml: -------------------------------------------------------------------------------- 1 | github_checks: 2 | annotations: false 3 | 4 | ignore: 5 | - "tool/**/*.go" 6 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # For more information, please refer to https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners 2 | 3 | * @cloudwego/Kitex-reviewers @cloudwego/Kitex-approvers @cloudwego/Kitex-maintainers 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | 12 | A clear and concise description of what the bug is. 13 | 14 | **To Reproduce** 15 | 16 | Steps to reproduce the behavior: 17 | 1. Go to '...' 18 | 2. Click on '....' 19 | 3. Scroll down to '....' 20 | 4. See error 21 | 22 | **Expected behavior** 23 | 24 | A clear and concise description of what you expected to happen. 25 | 26 | **Screenshots** 27 | 28 | If applicable, add screenshots to help explain your problem. 29 | 30 | **Kitex version:** 31 | 32 | Please provide the version of Kitex you are using. 33 | 34 | **Environment:** 35 | 36 | The output of `go env`. 37 | 38 | **Additional context** 39 | 40 | Add any other context about the problem here. 41 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | 12 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 13 | 14 | **Describe the solution you'd like** 15 | 16 | A clear and concise description of what you want to happen. 17 | 18 | **Describe alternatives you've considered** 19 | 20 | A clear and concise description of any alternative solutions or features you've considered. 21 | 22 | **Additional context** 23 | 24 | Add any other context or screenshots about the feature request here. 25 | -------------------------------------------------------------------------------- /.github/workflows/pr-check.yml: -------------------------------------------------------------------------------- 1 | name: Pull Request Check 2 | 3 | on: [ pull_request ] 4 | 5 | jobs: 6 | compliant: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | 11 | - name: Check License Header 12 | uses: apache/skywalking-eyes/header@v0.4.0 13 | env: 14 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 15 | 16 | - name: Check Spell 17 | uses: crate-ci/typos@master 18 | 19 | golangci-lint: 20 | runs-on: [ Linux, X64 ] 21 | steps: 22 | - uses: actions/checkout@v4 23 | - name: Set up Go 24 | uses: actions/setup-go@v5 25 | with: 26 | go-version: stable 27 | # for self-hosted, the cache path is shared across projects 28 | # and it works well without the cache of github actions 29 | # Enable it if we're going to use Github only 30 | cache: false 31 | 32 | - name: Golangci Lint 33 | # https://golangci-lint.run/ 34 | uses: golangci/golangci-lint-action@v6 35 | with: 36 | version: latest 37 | only-new-issues: true 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Files generated by IDEs 2 | .idea/ 3 | *.iml 4 | 5 | # Vim swap files 6 | *.swp 7 | 8 | # Vscode files 9 | .vscode 10 | 11 | # Emacs save files 12 | *~ 13 | \#*\# 14 | .\#* 15 | 16 | # User cluster configs 17 | .kubeconfig 18 | # Downloaded Kubernetes binary release 19 | /kubernetes/ 20 | # Downloaded kubernetes binary release tar ball 21 | kubernetes.tar.gz 22 | 23 | # direnv .envrc files 24 | .envrc 25 | 26 | # make-related metadata 27 | /.make/ 28 | 29 | # the result of the go build 30 | output* 31 | output/* 32 | 33 | # log 34 | *.log 35 | *.log.* 36 | 37 | # Test binary, built with `go test -c` 38 | *.test 39 | 40 | # Output of the go coverage tool, specifically when used with LiteIDE 41 | *.out 42 | 43 | # Go test binaries 44 | *.test 45 | testdata/ 46 | 47 | # kitex binary 48 | tool/cmd/kitex/kitex 49 | 50 | # remote dump file 51 | *.json 52 | base1.go 53 | dump.txt 54 | base2.go 55 | 56 | # Go workspace file 57 | go.work 58 | go.work.sum 59 | -------------------------------------------------------------------------------- /.golangci.yaml: -------------------------------------------------------------------------------- 1 | # Options for analysis running. 2 | run: 3 | timeout: 3m 4 | 5 | linters: # https://golangci-lint.run/usage/linters/ 6 | disable-all: true 7 | enable: 8 | - gosimple 9 | - govet 10 | - ineffassign 11 | - staticcheck 12 | - unused 13 | - unconvert 14 | - goimports 15 | - gofumpt 16 | 17 | # Refer to https://golangci-lint.run/usage/linters 18 | linters-settings: 19 | gofumpt: 20 | # Choose whether to use the extra rules. 21 | # Default: false 22 | extra-rules: true 23 | goimports: 24 | # Put imports beginning with prefix after 3rd-party packages. 25 | # It's a comma-separated list of prefixes. 26 | local-prefixes: github.com/cloudwego/kitex 27 | 28 | issues: 29 | exclude-use-default: true 30 | exclude-dirs: 31 | - kitex_gen 32 | -------------------------------------------------------------------------------- /.licenserc.yaml: -------------------------------------------------------------------------------- 1 | header: 2 | license: 3 | spdx-id: Apache-2.0 4 | copyright-owner: CloudWeGo Authors 5 | 6 | paths: 7 | - "**/*.go" 8 | - "**/*.s" 9 | 10 | paths-ignore: 11 | - internal/mocks/thrift/test.go 12 | - pkg/remote/codec/thrift/thrift_frugal.go 13 | - pkg/remote/codec/thrift/thrift_frugal_test.go 14 | - pkg/remote/codec/thrift/thrift_others.go 15 | - pkg/remote/trans/nphttp2/codes/codes.go 16 | - pkg/remote/trans/nphttp2/grpc/grpcframe/ 17 | - pkg/remote/trans/nphttp2/grpc/bdp_estimator.go 18 | - pkg/remote/trans/nphttp2/grpc/controlbuf.go 19 | - pkg/remote/trans/nphttp2/grpc/defaults.go 20 | - pkg/remote/trans/nphttp2/grpc/framer.go 21 | - pkg/remote/trans/nphttp2/grpc/flowcontrol.go 22 | - pkg/remote/trans/nphttp2/grpc/http2_client.go 23 | - pkg/remote/trans/nphttp2/grpc/http2_server.go 24 | - pkg/remote/trans/nphttp2/grpc/http_util.go 25 | - pkg/remote/trans/nphttp2/grpc/keepalive.go 26 | - pkg/remote/trans/nphttp2/grpc/keepalive_test.go 27 | - pkg/remote/trans/nphttp2/grpc/transport.go 28 | - pkg/remote/trans/nphttp2/grpc/transport_test.go 29 | - pkg/remote/trans/nphttp2/metadata/metadata.go 30 | - pkg/remote/trans/nphttp2/status/status.go 31 | - pkg/remote/codec/protobuf/error.pb.go 32 | - pkg/remote/codec/protobuf/test.pb.go 33 | - pkg/generic/descriptor/tree.go 34 | - pkg/generic/descriptor/tree_test.go 35 | - pkg/generic/httppb_test/idl/echo.pb.go 36 | - pkg/utils/json.go 37 | - pkg/protocol/bthrift/test/kitex_gen/** 38 | 39 | comment: on-failure 40 | -------------------------------------------------------------------------------- /.typos.toml: -------------------------------------------------------------------------------- 1 | # Typo check: https://github.com/crate-ci/typos 2 | 3 | [files] 4 | extend-exclude = ["go.mod", "go.sum"] 5 | 6 | [default.extend-words] 7 | typ = "typ" # type 8 | Descritor = "Descritor" # reflect pkg typo, exported func, let it go 9 | 10 | [default.extend-identifiers] 11 | GoAways = "GoAways" # GoAway frame plural noun 12 | henc = "henc" # hpack encoder 13 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | CloudWeGo 2 | Copyright 2021 CloudWeGo Authors. 3 | 4 | Apache Thrift 5 | Copyright (C) 2006 - 2019, The Apache Software Foundation 6 | 7 | This product includes software developed at 8 | The Apache Software Foundation (http://www.apache.org/). -------------------------------------------------------------------------------- /ROADMAP.md: -------------------------------------------------------------------------------- 1 | # Kitex RoadMap 2 | 3 | This document shows key roadmap of Kitex development from the year of 2021 to 2022. It may help users know more about the future features. But the actual work is driven by real-world needs, we may adjust our goals sometimes. 4 | 5 | ## New Features: 6 | 7 | - IO Communication 8 | - Netpoll provides more features and do further performance optimization, references https://github.com/cloudwego/netpoll. 9 | - Support ShmIPC to optimize IPC. 10 | - Support data packet compression. 11 | - Service Governance 12 | - Support more extension for users. All developers are welcome to contribute your extension to https://github.com/kitex-contrib. 13 | - Support dynamic config, expose API in Kitex. 14 | - Support Proxyless mode to integrate with Istio. 15 | - Codec 16 | - Support no serialization which references [Cap'n](https://capnproto.org/index.html) but is compatible with Thrift. 17 | - Support runtime serialization&deserialization without generating code (JIT) for Thrift. 18 | - Tool 19 | - Support generating Protobuf code to optimize performance. 20 | 21 | ## Performance Optimization 22 | 23 | - Improve the throughput of client side. 24 | - Optimize Protobuf Streaming performance. 25 | - Kitex-Protobuf supports codec with NoCopy API to reduce memory copies. 26 | 27 | ## User Experience Optimization 28 | 29 | - Provide good development practices for users to develop with Kitex more easily. 30 | - Improve Kitex Tool to provide more conveniences. 31 | -------------------------------------------------------------------------------- /client/callopt/streamcall/call_options_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 streamcall 18 | 19 | import ( 20 | "strings" 21 | "testing" 22 | "time" 23 | 24 | "github.com/cloudwego/kitex/client/callopt" 25 | "github.com/cloudwego/kitex/internal/test" 26 | ) 27 | 28 | func TestWithRecvTimeout(t *testing.T) { 29 | var sb strings.Builder 30 | callOpts := callopt.CallOptions{} 31 | testTimeout := 1 * time.Second 32 | WithRecvTimeout(testTimeout).f(&callOpts, &sb) 33 | test.Assert(t, callOpts.StreamOptions.RecvTimeout == testTimeout) 34 | test.Assert(t, sb.String() == "WithRecvTimeout(1s)") 35 | } 36 | -------------------------------------------------------------------------------- /client/callopt/streamcall/grpc_options.go: -------------------------------------------------------------------------------- 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 streamcall 18 | 19 | import "github.com/cloudwego/kitex/client/callopt" 20 | 21 | // WithGRPCCompressor specifies the compressor for the GRPC frame payload. 22 | func WithGRPCCompressor(compressorName string) Option { 23 | return ConvertOptionFrom(callopt.WithGRPCCompressor(compressorName)) 24 | } 25 | -------------------------------------------------------------------------------- /client/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 client contains core part of Kitex client. 18 | // Users should not directly use any method here other than the options and call options. 19 | // Users should call NewClient in the generated code, which wraps the NewClient call in this package. 20 | package client 21 | -------------------------------------------------------------------------------- /client/genericclient/generic_stream_service.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 genericclient 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/generic" 21 | "github.com/cloudwego/kitex/pkg/serviceinfo" 22 | ) 23 | 24 | // Deprecated, use generic.ServiceInfoWithGeneric instead 25 | func StreamingServiceInfo(g generic.Generic) *serviceinfo.ServiceInfo { 26 | return generic.ServiceInfoWithGeneric(g) 27 | } 28 | -------------------------------------------------------------------------------- /client/mocks_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 client 18 | 19 | // MockTStruct was implemented the thrift.TStruct interface. 20 | // But actually Read/Write are not in use, so removed... only empty struct left for testing 21 | type MockTStruct struct{} 22 | -------------------------------------------------------------------------------- /images/lark_group.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudwego/kitex/8124ee3649c4117226aec0aa2026be18d5125996/images/lark_group.png -------------------------------------------------------------------------------- /images/lark_group_cn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudwego/kitex/8124ee3649c4117226aec0aa2026be18d5125996/images/lark_group_cn.png -------------------------------------------------------------------------------- /internal/client/option_ttstream.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 client 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/remote/trans/ttstream" 21 | "github.com/cloudwego/kitex/pkg/utils" 22 | ) 23 | 24 | type TTHeaderStreamingOption struct { 25 | F func(o *TTHeaderStreamingOptions, di *utils.Slice) 26 | } 27 | 28 | type TTHeaderStreamingOptions struct { 29 | TransportOptions []ttstream.ClientProviderOption 30 | } 31 | -------------------------------------------------------------------------------- /internal/client/remote_option.go: -------------------------------------------------------------------------------- 1 | //go:build !windows 2 | // +build !windows 3 | 4 | /* 5 | * Copyright 2022 CloudWeGo Authors 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | // Package client defines the Options about remote transport of client. 21 | package client 22 | 23 | import ( 24 | "github.com/cloudwego/kitex/pkg/remote" 25 | "github.com/cloudwego/kitex/pkg/remote/codec" 26 | "github.com/cloudwego/kitex/pkg/remote/trans/netpoll" 27 | ) 28 | 29 | func newClientRemoteOption() *remote.ClientOption { 30 | return &remote.ClientOption{ 31 | CliHandlerFactory: netpoll.NewCliTransHandlerFactory(), 32 | Dialer: netpoll.NewDialer(), 33 | Codec: codec.NewDefaultCodec(), 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /internal/client/remote_option_windows.go: -------------------------------------------------------------------------------- 1 | //go:build windows 2 | // +build windows 3 | 4 | /* 5 | * Copyright 2022 CloudWeGo Authors 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | // Package client defines the Options about remote transport of client. 21 | package client 22 | 23 | import ( 24 | "github.com/cloudwego/kitex/pkg/remote" 25 | "github.com/cloudwego/kitex/pkg/remote/codec" 26 | "github.com/cloudwego/kitex/pkg/remote/trans/gonet" 27 | ) 28 | 29 | func newClientRemoteOption() *remote.ClientOption { 30 | return &remote.ClientOption{ 31 | CliHandlerFactory: gonet.NewCliTransHandlerFactory(), 32 | Dialer: gonet.NewDialer(), 33 | Codec: codec.NewDefaultCodec(), 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /internal/configutil/once.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 configutil . 18 | package configutil 19 | 20 | import ( 21 | "fmt" 22 | "runtime" 23 | ) 24 | 25 | // OptionOnce is used to ensure an option is only set once. 26 | type OptionOnce struct { 27 | OptMap map[string]struct{} 28 | } 29 | 30 | // NewOptionOnce creates a new option once instance. 31 | func NewOptionOnce() *OptionOnce { 32 | return &OptionOnce{ 33 | OptMap: make(map[string]struct{}), 34 | } 35 | } 36 | 37 | // OnceOrPanic panics if the Option has already been set. 38 | func (o *OptionOnce) OnceOrPanic() { 39 | if o == nil { 40 | return 41 | } 42 | pc, _, _, _ := runtime.Caller(1) 43 | name := runtime.FuncForPC(pc).Name() 44 | if _, ok := o.OptMap[name]; ok { 45 | panic(fmt.Sprintf("Option %s has already been set", name)) 46 | } 47 | o.OptMap[name] = struct{}{} 48 | } 49 | -------------------------------------------------------------------------------- /internal/configutil/once_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 configutil 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestOnceOrPanic(t *testing.T) { 26 | once := NewOptionOnce() 27 | once.OnceOrPanic() 28 | test.Panic(t, func() { 29 | once.OnceOrPanic() 30 | once.OnceOrPanic() 31 | }) 32 | } 33 | -------------------------------------------------------------------------------- /internal/generic/proto/type.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 proto 18 | 19 | import ( 20 | "github.com/jhump/protoreflect/desc" 21 | "github.com/jhump/protoreflect/dynamic" 22 | ) 23 | 24 | type ( 25 | ServiceDescriptor = *desc.ServiceDescriptor 26 | MessageDescriptor = *desc.MessageDescriptor 27 | ) 28 | 29 | // TODO(marina.sakai): remove this 30 | type Message interface { 31 | Marshal() ([]byte, error) 32 | TryGetFieldByNumber(fieldNumber int) (interface{}, error) 33 | TrySetFieldByNumber(fieldNumber int, val interface{}) error 34 | } 35 | 36 | // TODO(marina.sakai): modify this 37 | func NewMessage(descriptor MessageDescriptor) Message { 38 | return dynamic.NewMessage(descriptor) 39 | } 40 | -------------------------------------------------------------------------------- /internal/mocks/README.md: -------------------------------------------------------------------------------- 1 | # Running Prerequisites 2 | 3 | - Run command `go install github.com/golang/mock/mockgen@latest` to install gomock 4 | - Run command `git clone github.com/cloudwego/netpoll` to clone netpoll in the parent directory of kitex. 5 | 6 | # User's Guidance 7 | 8 | - Add a line under the `files` parameter of update.sh. 9 | - Fill in the go file path where the interface you want to mock is located. 10 | - Fill in the path of the output mock file. 11 | - Fill in the package name of the output mock file. 12 | - Run `sh update.sh` to update mock files. 13 | - Now you can use the generated gomock class to mock interface. Refer to https://github.com/golang/mock. -------------------------------------------------------------------------------- /internal/mocks/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 mocks contains mocks of interfaces. 18 | package mocks 19 | -------------------------------------------------------------------------------- /internal/mocks/proto/kitex_gen/pbapi/mock/invoker.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 | // Code generated by Kitex v0.9.1. DO NOT EDIT. 18 | 19 | package mock 20 | 21 | import ( 22 | server "github.com/cloudwego/kitex/server" 23 | ) 24 | 25 | // NewInvoker creates a server.Invoker with the given handler and options. 26 | func NewInvoker(handler Mock, opts ...server.Option) server.Invoker { 27 | var options []server.Option 28 | 29 | options = append(options, opts...) 30 | 31 | s := server.NewInvoker(options...) 32 | if err := s.RegisterService(serviceInfo(), handler); err != nil { 33 | panic(err) 34 | } 35 | if err := s.Init(); err != nil { 36 | panic(err) 37 | } 38 | return s 39 | } 40 | -------------------------------------------------------------------------------- /internal/mocks/proto/kitex_gen/pbapi/mock/server.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 | // Code generated by Kitex v0.9.1. DO NOT EDIT. 18 | package mock 19 | 20 | import ( 21 | server "github.com/cloudwego/kitex/server" 22 | ) 23 | 24 | // NewServer creates a server.Server with the given handler and options. 25 | func NewServer(handler Mock, opts ...server.Option) server.Server { 26 | var options []server.Option 27 | 28 | options = append(options, opts...) 29 | 30 | svr := server.NewServer(options...) 31 | if err := svr.RegisterService(serviceInfo(), handler); err != nil { 32 | panic(err) 33 | } 34 | return svr 35 | } 36 | 37 | func RegisterService(svr server.Server, handler Mock, opts ...server.RegisterOption) error { 38 | return svr.RegisterService(serviceInfo(), handler, opts...) 39 | } 40 | -------------------------------------------------------------------------------- /internal/mocks/proto/pbapi.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package pbapi; 3 | 4 | option go_package = "pbapi"; 5 | 6 | message MockReq { 7 | string message = 1; 8 | } 9 | 10 | message MockResp { 11 | string message = 1; 12 | } 13 | 14 | service Mock { 15 | rpc UnaryTest (MockReq) returns (MockResp) {} 16 | rpc ClientStreamingTest (stream MockReq) returns (MockResp) {} 17 | rpc ServerStreamingTest (MockReq) returns (stream MockResp) {} 18 | rpc BidirectionalStreamingTest (stream MockReq) returns (stream MockResp) {} 19 | } -------------------------------------------------------------------------------- /internal/mocks/thrift/gen.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | kitex -thrift no_default_serdes -module github.com/cloudwego/kitex -gen-path .. ./test.thrift 4 | 5 | kitex -thrift no_default_serdes -module github.com/cloudwego/kitex -gen-path .. ./stream.thrift 6 | 7 | rm -rf ./mock # not in use, rm it 8 | 9 | rm -rf ./testservice # not in use, rm it -------------------------------------------------------------------------------- /internal/mocks/thrift/k-consts.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 thrift 18 | 19 | // KitexUnusedProtection is used to prevent 'imported and not used' error. 20 | var KitexUnusedProtection = struct{}{} 21 | -------------------------------------------------------------------------------- /internal/mocks/thrift/stream.thrift: -------------------------------------------------------------------------------- 1 | namespace go thrift 2 | 3 | struct Request { 4 | 1: required string message, 5 | } 6 | 7 | struct Response { 8 | 1: required string message, 9 | } 10 | 11 | service TestService { 12 | Response Echo (1: Request req) (streaming.mode="bidirectional"), 13 | Response EchoClient (1: Request req) (streaming.mode="client"), 14 | Response EchoServer (1: Request req) (streaming.mode="server"), 15 | Response EchoUnary (1: Request req) (streaming.mode="unary"), // not recommended 16 | Response EchoBizException (1: Request req) (streaming.mode="client"), 17 | 18 | Response EchoPingPong (1: Request req), // KitexThrift, non-streaming 19 | } -------------------------------------------------------------------------------- /internal/mocks/thrift/test.thrift: -------------------------------------------------------------------------------- 1 | namespace go thrift 2 | 3 | struct MockReq { 4 | 1: string Msg, 5 | 2: map strMap, 6 | 3: list strList, 7 | } 8 | 9 | exception Exception { 10 | 1: i32 code 11 | 255: string msg 12 | } 13 | 14 | service Mock { 15 | string Test(1:MockReq req) 16 | string ExceptionTest(1:MockReq req)throws(1: Exception err) 17 | } 18 | -------------------------------------------------------------------------------- /internal/reusable.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 internal 18 | 19 | // Reusable means a object is reusable. 20 | type Reusable interface { 21 | Recycle() 22 | } 23 | -------------------------------------------------------------------------------- /internal/server/register_option.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 server 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/endpoint" 21 | ) 22 | 23 | // RegisterOption is the only way to config service registration. 24 | type RegisterOption struct { 25 | F func(o *RegisterOptions) 26 | } 27 | 28 | // RegisterOptions is used to config service registration. 29 | type RegisterOptions struct { 30 | IsFallbackService bool 31 | Middlewares []endpoint.Middleware 32 | } 33 | 34 | // NewRegisterOptions creates a register options. 35 | func NewRegisterOptions(opts []RegisterOption) *RegisterOptions { 36 | o := &RegisterOptions{} 37 | ApplyRegisterOptions(opts, o) 38 | return o 39 | } 40 | 41 | // ApplyRegisterOptions applies the given register options. 42 | func ApplyRegisterOptions(opts []RegisterOption, o *RegisterOptions) { 43 | for _, op := range opts { 44 | op.F(o) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /internal/utils/safemcache/safemcache_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 safemcache 18 | 19 | import ( 20 | "testing" 21 | "unsafe" 22 | 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | func TestMallocFree(t *testing.T) { 27 | // case: normal 28 | b := Malloc(1) 29 | test.Assert(t, len(b) == 1) 30 | 31 | h := (*sliceHeader)(unsafe.Pointer(&b)) 32 | test.Assert(t, h.Footer() == footerMagic) 33 | 34 | Free(b) 35 | test.Assert(t, h.Footer() == 0) 36 | 37 | // case: magic not match 38 | b = Malloc(1) 39 | test.Assert(t, len(b) == 1) 40 | h = (*sliceHeader)(unsafe.Pointer(&b)) 41 | test.Assert(t, h.Footer() == footerMagic) 42 | 43 | h.SetFooter(2) 44 | Free(b) // it will not work 45 | test.Assert(t, h.Footer() == 2) 46 | } 47 | -------------------------------------------------------------------------------- /licenses/LICENSE-gjson: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Josh Baker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /licenses/LICENSE-httprouter: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2013, Julien Schmidt 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /licenses/LICENSE-json-iterator.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 json-iterator 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /licenses/LICENSE-protobuf.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 The Go Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google Inc. nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /licenses/LICENSE-xxhash.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Caleb Spare 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /pkg/circuitbreak/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 circuitbreak implements the circuit breaker logic. 18 | package circuitbreak 19 | -------------------------------------------------------------------------------- /pkg/consts/ctx.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 consts 18 | 19 | // Method key used in context. 20 | const ( 21 | CtxKeyMethod = "K_METHOD" 22 | CtxKeyLogID = "K_LOGID" 23 | ) 24 | 25 | const ( 26 | SERVICE_INLINE_RPCINFO_KEY = "SERVICE_INLINE_RPCINFO_KEY" 27 | SERVICE_INLINE_SERVICE_NAME = "SERVICE_INLINE_SERVICE_NAME" 28 | SERVICE_INLINE_DATA_KEY = "SERVICE_INLINE_DATA_KEY" 29 | ) 30 | -------------------------------------------------------------------------------- /pkg/discovery/constants.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 discovery 18 | 19 | // Predefined discovery event names. 20 | const ( 21 | ChangeEventName = "discovery_change" 22 | DeleteEventName = "discovery_delete" 23 | ) 24 | -------------------------------------------------------------------------------- /pkg/endpoint/unary_endpoint.go: -------------------------------------------------------------------------------- 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 endpoint 18 | 19 | import "context" 20 | 21 | type UnaryEndpoint Endpoint 22 | 23 | type UnaryMiddleware func(next UnaryEndpoint) UnaryEndpoint 24 | 25 | type UnaryMiddlewareBuilder func(ctx context.Context) UnaryMiddleware 26 | 27 | func UnaryChain(mws ...UnaryMiddleware) UnaryMiddleware { 28 | return func(next UnaryEndpoint) UnaryEndpoint { 29 | for i := len(mws) - 1; i >= 0; i-- { 30 | next = mws[i](next) 31 | } 32 | return next 33 | } 34 | } 35 | 36 | func (mw UnaryMiddleware) ToMiddleware() Middleware { 37 | return func(next Endpoint) Endpoint { 38 | return Endpoint(mw(UnaryEndpoint(next))) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /pkg/event/event.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 event 18 | 19 | import "time" 20 | 21 | // Event represents an event. 22 | type Event struct { 23 | Name string 24 | Time time.Time 25 | Detail string 26 | Extra interface{} 27 | } 28 | -------------------------------------------------------------------------------- /pkg/exception/deprecated.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 exception is deprecated 18 | package exception 19 | 20 | import ( 21 | "github.com/cloudwego/kitex/pkg/utils" 22 | ) 23 | 24 | // MarshalError convert go error to thrift exception, and encode exception over buffered binary transport. 25 | // Deprecated: Use MarshalError in pkg/utils/thrift.go instead. 26 | func MarshalError(method string, err error) []byte { 27 | return utils.MarshalError(method, err) 28 | } 29 | 30 | // UnmarshalError decode binary and return error message 31 | // Deprecated: Use UnmarshalError in pkg/utils/thrift.go instead. 32 | func UnmarshalError(b []byte) error { 33 | return utils.UnmarshalError(b) 34 | } 35 | -------------------------------------------------------------------------------- /pkg/exception/deprecated_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 exception 18 | 19 | import ( 20 | "errors" 21 | "testing" 22 | 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | func TestException(t *testing.T) { 27 | errMsg := "my error" 28 | b := MarshalError("some method", errors.New(errMsg)) 29 | test.Assert(t, UnmarshalError(b).Error() == errMsg) 30 | } 31 | -------------------------------------------------------------------------------- /pkg/generic/binarypb_codec.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 generic 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/generic/proto" 21 | ) 22 | 23 | type binaryPbCodec struct { 24 | svcName string 25 | extra map[string]string 26 | readerWriter *proto.RawReaderWriter 27 | } 28 | 29 | func newBinaryPbCodec(svcName, packageName string) *binaryPbCodec { 30 | bpc := &binaryPbCodec{ 31 | svcName: svcName, 32 | extra: make(map[string]string), 33 | readerWriter: proto.NewRawReaderWriter(), 34 | } 35 | bpc.setPackageName(packageName) 36 | return bpc 37 | } 38 | 39 | func (c *binaryPbCodec) getMessageReaderWriter() interface{} { 40 | return c.readerWriter 41 | } 42 | 43 | func (c *binaryPbCodec) Name() string { 44 | return "BinaryPb" 45 | } 46 | 47 | func (c *binaryPbCodec) setPackageName(pkg string) { 48 | c.extra[packageNameKey] = pkg 49 | } 50 | -------------------------------------------------------------------------------- /pkg/generic/closer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 generic 18 | 19 | // Closer is usually used to recycle resource. 20 | type Closer interface { 21 | // Close the unused resource. 22 | Close() error 23 | } 24 | -------------------------------------------------------------------------------- /pkg/generic/descriptor/field_mapping_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 descriptor 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func Test_goTag_Handle(t *testing.T) { 26 | f := &FieldDescriptor{} 27 | NewGoTag(`db:"b" json:"a"`).Handle(f) 28 | test.Assert(t, f.Alias == "a", f.Alias) 29 | NewGoTag(`db:"b" json:"a,omitempty"`).Handle(f) 30 | test.Assert(t, f.Alias == "a", f.Alias) 31 | NewGoTag(`db:"b" json:"a,string"`).Handle(f) 32 | test.Assert(t, f.Alias == "a", f.Alias) 33 | NewGoTag(`json:\"a\"`).Handle(f) 34 | test.Assert(t, f.Alias == "a", f.Alias) 35 | } 36 | -------------------------------------------------------------------------------- /pkg/generic/descriptor/type_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 descriptor 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestFromThriftTType(t *testing.T) { 26 | test.Assert(t, FromThriftTType(byte(1)) == Type(1)) 27 | test.Assert(t, FromThriftTType(int8(2)) == Type(2)) 28 | } 29 | -------------------------------------------------------------------------------- /pkg/generic/descriptor_provider.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 generic 18 | 19 | import "github.com/cloudwego/kitex/pkg/generic/descriptor" 20 | 21 | // DescriptorProvider provide service descriptor 22 | type DescriptorProvider interface { 23 | Closer 24 | // Provide return a channel for provide service descriptors 25 | Provide() <-chan *descriptor.ServiceDescriptor 26 | } 27 | 28 | // GetProviderOption provide options for descriptor provider 29 | type GetProviderOption interface { 30 | Option() ProviderOption 31 | } 32 | 33 | type ProviderOption struct { 34 | // DynamicGoEnabled is if dynamicgo is enabled or not 35 | DynamicGoEnabled bool 36 | } 37 | -------------------------------------------------------------------------------- /pkg/generic/grpcjson_test/idl/api.thrift: -------------------------------------------------------------------------------- 1 | namespace go thrift 2 | 3 | struct Request { 4 | 1: required string message, 5 | } 6 | 7 | struct Response { 8 | 1: required string message, 9 | } 10 | 11 | service TestService { 12 | Response Echo (1: Request req) (streaming.mode="bidirectional"), 13 | Response EchoClient (1: Request req) (streaming.mode="client"), 14 | Response EchoServer (1: Request req) (streaming.mode="server"), 15 | Response EchoUnary (1: Request req) (streaming.mode="unary"), // not recommended 16 | Response EchoBizException (1: Request req) (streaming.mode="client"), 17 | 18 | Response EchoPingPong (1: Request req), // KitexThrift, non-streaming 19 | } -------------------------------------------------------------------------------- /pkg/generic/grpcjsonpb_test/idl/pbapi.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package pbapi; 3 | 4 | option go_package = "pbapi"; 5 | 6 | message MockReq { 7 | string message = 1; 8 | } 9 | 10 | message MockResp { 11 | string message = 1; 12 | } 13 | 14 | service Mock { 15 | rpc UnaryTest (MockReq) returns (MockResp) {} 16 | rpc ClientStreamingTest (stream MockReq) returns (MockResp) {} 17 | rpc ServerStreamingTest (MockReq) returns (stream MockResp) {} 18 | rpc BidirectionalStreamingTest (stream MockReq) returns (stream MockResp) {} 19 | } -------------------------------------------------------------------------------- /pkg/generic/grpcjsonpb_test/idl/pbapi_multi_service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package pbapi_multi_service; 3 | 4 | option go_package = "pbapi_multi_service"; 5 | 6 | message MockReq { 7 | string message = 1; 8 | } 9 | 10 | message MockResp { 11 | string message = 1; 12 | } 13 | 14 | service Mock { 15 | rpc UnaryTest (MockReq) returns (MockResp) {} 16 | rpc ClientStreamingTest (stream MockReq) returns (MockResp) {} 17 | rpc ServerStreamingTest (MockReq) returns (stream MockResp) {} 18 | rpc BidirectionalStreamingTest (stream MockReq) returns (stream MockResp) {} 19 | } 20 | 21 | service Mock2 { 22 | rpc ClientStreamingTest2 (stream MockReq) returns (MockResp) {} 23 | } -------------------------------------------------------------------------------- /pkg/generic/http_test/conf/kitex.yml: -------------------------------------------------------------------------------- 1 | Address: ":8109" 2 | EnableDebugServer: true 3 | DebugServerPort: "19109" 4 | Log: 5 | Dir: log 6 | Loggers: 7 | - Name: default 8 | Level: info # Notice: change it to debug if needed in local development 9 | Outputs: 10 | - Console # Notice: change it to debug if needed in local development, don't use this in production! 11 | - Name: rpcAccess 12 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 13 | Outputs: 14 | - Agent 15 | - Name: rpcCall 16 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 17 | Outputs: 18 | - Console 19 | -------------------------------------------------------------------------------- /pkg/generic/http_test/idl/baseline.thrift: -------------------------------------------------------------------------------- 1 | namespace go baseline 2 | 3 | struct Simple { 4 | 1: byte ByteField 5 | 2: i64 I64Field (api.js_conv = "") 6 | 3: double DoubleField 7 | 4: i32 I32Field 8 | 5: string StringField, 9 | 6: binary BinaryField 10 | } 11 | 12 | struct Nesting { 13 | 1: string String (api.header = "String") 14 | 2: list ListSimple 15 | 3: double Double (api.path = "double") 16 | 4: i32 I32 (api.body = "I32") 17 | 5: list ListI32 (api.query = "ListI32") 18 | 6: i64 I64 19 | 7: map MapStringString 20 | 8: Simple SimpleStruct 21 | 9: map MapI32I64 22 | 10: list ListString 23 | 11: binary Binary 24 | 12: map MapI64String 25 | 13: list ListI64 (api.cookie = "list_i64") 26 | 14: byte Byte 27 | 15: map MapStringSimple 28 | } 29 | 30 | service BaselineService { 31 | Simple SimpleMethod(1: Simple req) (api.post = "/simple", api.baseurl = 'example.com') 32 | Nesting NestingMethod(1: Nesting req) (api.post = "/nesting/:double", api.baseurl = 'example.com') 33 | } 34 | -------------------------------------------------------------------------------- /pkg/generic/http_test/idl/binary_echo.thrift: -------------------------------------------------------------------------------- 1 | namespace go kitex.test.server 2 | 3 | struct BinaryWrapper { 4 | 1: binary msg (api.body = "msg") 5 | 2: bool got_base64 (api.body = "got_base64") 6 | 3: required i64 num (api.body = "num", api.js_conv="") 7 | 4: optional string str = "echo" (api.query = "str", go.tag = "json:\"STR\"") 8 | } 9 | 10 | service ExampleService { 11 | BinaryWrapper BinaryEcho(1: BinaryWrapper req) (api.get = '/BinaryEcho', api.baseurl = 'example.com') 12 | } -------------------------------------------------------------------------------- /pkg/generic/http_test/idl/dynamicgo_go_tag_error.thrift: -------------------------------------------------------------------------------- 1 | namespace go kitex.test.server 2 | 3 | struct BinaryWrapper { 4 | 1: optional string str (api.query = "str", go.tag='json:\"STR,omitempty\"') 5 | } 6 | 7 | service ExampleService { 8 | BinaryWrapper BinaryEcho(1: BinaryWrapper req) (api.get = '/BinaryEcho', api.baseurl = 'example.com') 9 | } -------------------------------------------------------------------------------- /pkg/generic/http_test/idl/mock.thrift: -------------------------------------------------------------------------------- 1 | namespace go thrift 2 | 3 | struct MockReq { 4 | 1: string Msg, 5 | 2: map strMap, 6 | 3: list strList, 7 | } 8 | 9 | exception Exception { 10 | 1: i32 code 11 | 255: string msg 12 | } 13 | 14 | service Mock { 15 | string Test(1:MockReq req) (api.get = '/Test', api.baseurl = 'example.com') 16 | string ExceptionTest(1:MockReq req)throws(1: Exception err) (api.get = '/ExceptionTest', api.baseurl = 'example.com') 17 | } 18 | -------------------------------------------------------------------------------- /pkg/generic/httppb_test/conf/kitex.yml: -------------------------------------------------------------------------------- 1 | Address: ":8109" 2 | EnableDebugServer: true 3 | DebugServerPort: "19109" 4 | Log: 5 | Dir: log 6 | Loggers: 7 | - Name: default 8 | Level: info # Notice: change it to debug if needed in local development 9 | Outputs: 10 | - Console # Notice: change it to debug if needed in local development, don't use this in production! 11 | - Name: rpcAccess 12 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 13 | Outputs: 14 | - Agent 15 | - Name: rpcCall 16 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 17 | Outputs: 18 | - Console 19 | -------------------------------------------------------------------------------- /pkg/generic/httppb_test/idl/echo.proto: -------------------------------------------------------------------------------- 1 | syntax="proto3"; 2 | package idl; 3 | 4 | option go_package = "github.com/cloudwego/kitex/pkg/generic/httppb_test/idl"; 5 | 6 | enum TestEnum { 7 | Zero = 0; 8 | First = 1; 9 | } 10 | 11 | message Elem { 12 | bool ok = 1; 13 | } 14 | 15 | message Message { 16 | int32 tiny = 1; 17 | int64 large = 2; 18 | TestEnum tenum = 3; 19 | string str = 4; 20 | map elems = 5; 21 | repeated Elem els = 6; 22 | // map els = 7; 23 | } 24 | 25 | service ExampleService { 26 | rpc Echo(Message) returns(Message); 27 | } 28 | 29 | // protoc --go_out=$GOPATH/src kitex/pkg/generic/httppb_test/idl/echo.proto 30 | -------------------------------------------------------------------------------- /pkg/generic/httppb_test/idl/echo.thrift: -------------------------------------------------------------------------------- 1 | namespace go kitex.test.server 2 | 3 | enum TestEnum { 4 | Zero = 0 5 | First = 1 6 | } 7 | 8 | struct Elem { 9 | 1: bool ok 10 | } 11 | 12 | struct Message { 13 | 1: i8 tiny 14 | 2: i64 large 15 | 3: TestEnum tenum 16 | 4: string str 17 | 5: map elems 18 | 6: list els 19 | // 7: map els = {12: {"ok": true}} 20 | } 21 | 22 | service ExampleService { 23 | Message Echo(1: Message req) (api.get = '/Echo', api.baseurl = 'example.com') 24 | } 25 | -------------------------------------------------------------------------------- /pkg/generic/json_test/conf/kitex.yml: -------------------------------------------------------------------------------- 1 | Address: ":8109" 2 | EnableDebugServer: true 3 | DebugServerPort: "19109" 4 | Log: 5 | Dir: log 6 | Loggers: 7 | - Name: default 8 | Level: info # Notice: change it to debug if needed in local development 9 | Outputs: 10 | - Console # Notice: change it to debug if needed in local development, don't use this in production! 11 | - Name: rpcAccess 12 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 13 | Outputs: 14 | - Agent 15 | - Name: rpcCall 16 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 17 | Outputs: 18 | - Console 19 | -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/base.thrift: -------------------------------------------------------------------------------- 1 | namespace py base 2 | namespace go base 3 | namespace java com.bytedance.thrift.base 4 | 5 | struct TrafficEnv { 6 | 1: bool Open = false, 7 | 2: string Env = "", 8 | } 9 | 10 | struct Base { 11 | 1: string LogID = "", 12 | 2: string Caller = "", 13 | 3: string Addr = "", 14 | 4: string Client = "", 15 | 5: optional TrafficEnv TrafficEnv, 16 | 6: optional map Extra, 17 | } 18 | 19 | struct BaseResp { 20 | 1: string StatusMessage = "", 21 | 2: i32 StatusCode = 0, 22 | 3: optional map Extra, 23 | } 24 | -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/baseline.thrift: -------------------------------------------------------------------------------- 1 | namespace go baseline 2 | 3 | struct Simple { 4 | 1: byte ByteField 5 | 2: i64 I64Field 6 | 3: double DoubleField 7 | 4: i32 I32Field 8 | 5: string StringField, 9 | 6: binary BinaryField 10 | } 11 | 12 | struct Nesting { 13 | 1: string String 14 | 2: list ListSimple 15 | 3: double Double 16 | 4: i32 I32 17 | 5: list ListI32 18 | 6: i64 I64 19 | 7: map MapStringString 20 | 8: Simple SimpleStruct 21 | 9: map MapI32I64 22 | 10: list ListString 23 | 11: binary Binary 24 | 12: map MapI64String 25 | 13: list ListI64 26 | 14: byte Byte 27 | 15: map MapStringSimple 28 | } 29 | 30 | service BaselineService { 31 | Simple SimpleMethod(1: Simple req) 32 | Nesting NestingMethod(1: Nesting req) 33 | } 34 | -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/binary_echo.thrift: -------------------------------------------------------------------------------- 1 | namespace go kitex.test.server 2 | 3 | struct BinaryWrapper { 4 | 1: binary msg 5 | 2: bool got_base64 6 | 3: optional string str 7 | } 8 | 9 | service ExampleService { 10 | BinaryWrapper BinaryEcho(1: BinaryWrapper req) 11 | } -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/example.thrift: -------------------------------------------------------------------------------- 1 | include "base.thrift" 2 | include "self_ref.thrift" 3 | include "extend.thrift" 4 | namespace go kitex.test.server 5 | 6 | enum FOO { 7 | A = 1; 8 | } 9 | 10 | struct InnerBase { 11 | 255: base.Base Base, 12 | } 13 | 14 | struct ExampleReq { 15 | 1: required string Msg, 16 | 2: FOO Foo, 17 | 3: InnerBase InnerBase, 18 | 4: optional i8 I8, 19 | 5: optional i16 I16, 20 | 6: optional i32 I32, 21 | 7: optional i64 I64, 22 | 8: optional double Double, 23 | 9: optional Test Test, 24 | 255: base.Base Base, 25 | } 26 | struct ExampleResp { 27 | 1: required string Msg, 28 | 2: string required_field, 29 | 3: optional i64 num (api.js_conv="true"), 30 | 4: optional i8 I8 = 8, 31 | 5: optional i16 I16, 32 | 6: optional i32 I32, 33 | 7: optional i64 I64, 34 | 8: optional double Double, 35 | 9: Test Test, 36 | 255: base.BaseResp BaseResp, 37 | } 38 | exception Exception { 39 | 1: i32 code 40 | 2: string msg 41 | } 42 | 43 | struct Test { 44 | 1: optional string aaa = "aaaaaaa" 45 | 2: string bbb 46 | } 47 | 48 | struct A { 49 | 1: A self 50 | 2: self_ref.A a 51 | } 52 | 53 | service ExampleService extends extend.ExtendService { 54 | ExampleResp ExampleMethod(1: ExampleReq req)throws(1: Exception err), 55 | A Foo(1: A req) 56 | string Ping(1: string msg) 57 | oneway void Oneway(1: string msg) 58 | void Void(1: string msg) 59 | void VoidWithString(1: string msg) 60 | } -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/example_multi_service.thrift: -------------------------------------------------------------------------------- 1 | include "base.thrift" 2 | include "self_ref.thrift" 3 | include "extend.thrift" 4 | namespace go kitex.test.server 5 | 6 | enum FOO { 7 | A = 1; 8 | } 9 | 10 | struct InnerBase { 11 | 255: base.Base Base, 12 | } 13 | 14 | struct ExampleReq { 15 | 1: required string Msg, 16 | 2: FOO Foo, 17 | 3: InnerBase InnerBase, 18 | 4: optional i8 I8, 19 | 5: optional i16 I16, 20 | 6: optional i32 I32, 21 | 7: optional i64 I64, 22 | 8: optional double Double, 23 | 255: base.Base Base, 24 | } 25 | struct ExampleResp { 26 | 1: required string Msg, 27 | 2: string required_field, 28 | 3: optional i64 num (api.js_conv="true"), 29 | 4: optional i8 I8, 30 | 5: optional i16 I16, 31 | 6: optional i32 I32, 32 | 7: optional i64 I64, 33 | 8: optional double Double, 34 | 255: base.BaseResp BaseResp, 35 | } 36 | exception Exception { 37 | 1: i32 code 38 | 2: string msg 39 | } 40 | 41 | struct A { 42 | 1: A self 43 | 2: self_ref.A a 44 | } 45 | 46 | service ExampleService extends extend.ExtendService { 47 | ExampleResp ExampleMethod(1: ExampleReq req)throws(1: Exception err), 48 | A Foo(1: A req) 49 | string Ping(1: string msg) 50 | oneway void Oneway(1: string msg) 51 | void Void(1: string msg) 52 | } 53 | 54 | service Example2Service { 55 | ExampleResp Example2Method(1: ExampleReq req) 56 | } -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/extend.thrift: -------------------------------------------------------------------------------- 1 | namespace go extend 2 | 3 | struct ExampleReq { 4 | 1: i64 Msg 5 | } 6 | struct ExampleResp { 7 | 1: i64 Msg 8 | } 9 | 10 | service ExtendService { 11 | ExampleResp ExtendMethod(1: ExampleReq req) 12 | } -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/mock.thrift: -------------------------------------------------------------------------------- 1 | namespace go thrift 2 | 3 | struct MockReq { 4 | 1: string Msg, 5 | 2: map strMap, 6 | 3: list strList, 7 | } 8 | 9 | exception Exception { 10 | 1: i32 code 11 | 255: string msg 12 | } 13 | 14 | service Mock { 15 | string Test(1:MockReq req) 16 | string ExceptionTest(1:MockReq req)throws(1: Exception err) 17 | } 18 | -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/mock_unknown_method.thrift: -------------------------------------------------------------------------------- 1 | include "base.thrift" 2 | include "self_ref.thrift" 3 | include "extend.thrift" 4 | namespace go kitex.test.server 5 | 6 | enum FOO { 7 | A = 1; 8 | } 9 | 10 | struct InnerBase { 11 | 255: base.Base Base, 12 | } 13 | 14 | struct ExampleReq { 15 | 1: required string Msg, 16 | 2: FOO Foo, 17 | 3: InnerBase InnerBase, 18 | 4: optional i8 I8, 19 | 5: optional i16 I16, 20 | 6: optional i32 I32, 21 | 7: optional i64 I64, 22 | 8: optional double Double, 23 | 255: base.Base Base, 24 | } 25 | struct ExampleResp { 26 | 1: required string Msg, 27 | 2: string required_field, 28 | 3: optional i64 num (api.js_conv="true"), 29 | 4: optional i8 I8, 30 | 5: optional i16 I16, 31 | 6: optional i32 I32, 32 | 7: optional i64 I64, 33 | 8: optional double Double, 34 | 255: base.BaseResp BaseResp, 35 | } 36 | exception Exception { 37 | 1: i32 code 38 | 2: string msg 39 | } 40 | 41 | struct A { 42 | 1: A self 43 | 2: self_ref.A a 44 | } 45 | 46 | service ExampleService extends extend.ExtendService { 47 | ExampleResp ExampleMethod(1: ExampleReq req)throws(1: Exception err) 48 | ExampleResp UnknownMethod(1: ExampleReq req) 49 | } -------------------------------------------------------------------------------- /pkg/generic/json_test/idl/self_ref.thrift: -------------------------------------------------------------------------------- 1 | namespace go self_ref 2 | 3 | struct A { 4 | 1: A self 5 | 2: string extra 6 | } 7 | 8 | service Mock { 9 | string Test(1:A req) 10 | } -------------------------------------------------------------------------------- /pkg/generic/jsonpb_test/data/example2_pb.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudwego/kitex/8124ee3649c4117226aec0aa2026be18d5125996/pkg/generic/jsonpb_test/data/example2_pb.bin -------------------------------------------------------------------------------- /pkg/generic/jsonpb_test/idl/echo.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package test; 3 | // The greeting service definition. 4 | option go_package = "./"; 5 | 6 | message Request { 7 | string message = 1; 8 | } 9 | 10 | message Response { 11 | string message = 1; 12 | } 13 | 14 | service Echo { 15 | rpc Echo (Request) returns (Response) {} 16 | } -------------------------------------------------------------------------------- /pkg/generic/jsonpb_test/idl/echo_import.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package test; 3 | 4 | import "echo.proto"; 5 | // The greeting service definition. 6 | option go_package = "./"; 7 | 8 | 9 | service EchoService { 10 | rpc Echo (test.Request) returns (test.Response) {} 11 | } -------------------------------------------------------------------------------- /pkg/generic/jsonpb_test/idl/example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package test; 3 | // The greeting service definition with modifications 4 | option go_package = "./"; 5 | 6 | message ExampleReq { 7 | repeated string reqs = 1; 8 | } 9 | 10 | message ExampleResp { 11 | repeated string resps = 1; 12 | } 13 | 14 | message Void {} 15 | 16 | service ExampleService { 17 | rpc ExampleMethod(ExampleReq) returns (ExampleResp); 18 | rpc VoidMethod(Void) returns (Void); 19 | } -------------------------------------------------------------------------------- /pkg/generic/map_test/conf/kitex.yml: -------------------------------------------------------------------------------- 1 | Address: ":9009" 2 | EnableDebugServer: true 3 | DebugServerPort: "19009" 4 | Log: 5 | Dir: log 6 | Loggers: 7 | - Name: default 8 | Level: info # Notice: change it to debug if needed in local development 9 | Outputs: 10 | - Console # Notice: change it to debug if needed in local development, don't use this in production! 11 | - Name: rpcAccess 12 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 13 | Outputs: 14 | - Agent 15 | - Name: rpcCall 16 | Level: trace # Notice: Not recommended for modification, otherwise may affect construction of call chain (tracing) 17 | Outputs: 18 | - Console 19 | -------------------------------------------------------------------------------- /pkg/generic/map_test/idl/base.thrift: -------------------------------------------------------------------------------- 1 | namespace py base 2 | namespace go base 3 | namespace java com.bytedance.thrift.base 4 | 5 | struct TrafficEnv { 6 | 1: bool Open = false, 7 | 2: string Env = "", 8 | } 9 | 10 | struct Base { 11 | 1: string LogID = "", 12 | 2: string Caller = "", 13 | 3: string Addr = "", 14 | 4: string Client = "", 15 | 5: optional TrafficEnv TrafficEnv, 16 | 6: optional map Extra, 17 | } 18 | 19 | struct BaseResp { 20 | 1: string StatusMessage = "", 21 | 2: i32 StatusCode = 0, 22 | 3: optional map Extra, 23 | } 24 | -------------------------------------------------------------------------------- /pkg/generic/map_test/idl/example.thrift: -------------------------------------------------------------------------------- 1 | include "base.thrift" 2 | include "self_ref.thrift" 3 | namespace go kitex.test.server 4 | 5 | enum FOO { 6 | A = 1; 7 | } 8 | 9 | struct InnerBase { 10 | 255: base.Base Base, 11 | } 12 | 13 | struct MockElem { 14 | 1: string Bar 15 | } 16 | 17 | struct ExampleReq { 18 | 1: required string Msg = "Hello", 19 | 2: FOO Foo, 20 | 3: list TestList, 21 | 4: optional map TestMap, 22 | 5: list StrList, 23 | 6: list I64List = [1, 2, 3], 24 | 7: bool B, 25 | 8: optional binary BinaryMsg, 26 | 255: base.Base Base, 27 | } 28 | struct ExampleResp { 29 | 1: required string Msg, 30 | 2: string required_field 31 | 255: base.BaseResp BaseResp, 32 | } 33 | exception Exception { 34 | 1: i32 code 35 | 2: string msg 36 | } 37 | 38 | struct A { 39 | 1: A self 40 | 2: self_ref.A a 41 | } 42 | 43 | service ExampleService { 44 | ExampleReq ExampleMethod(1: ExampleReq req)throws(1: Exception err), 45 | A Foo(1: A req) 46 | string Ping(1: string msg) 47 | oneway void Oneway(1: string msg) 48 | void Void(1: string msg) 49 | } -------------------------------------------------------------------------------- /pkg/generic/map_test/idl/mock.thrift: -------------------------------------------------------------------------------- 1 | namespace go thrift 2 | 3 | struct MockReq { 4 | 1: string Msg, 5 | 2: map strMap, 6 | 3: list strList, 7 | } 8 | 9 | service Mock { 10 | string Test(1:MockReq req) 11 | } 12 | -------------------------------------------------------------------------------- /pkg/generic/map_test/idl/self_ref.thrift: -------------------------------------------------------------------------------- 1 | namespace go kitex.test.server 2 | 3 | struct A { 4 | 1: A self 5 | 2: string extra 6 | } 7 | 8 | service Mock { 9 | string Test(1:A req) 10 | } -------------------------------------------------------------------------------- /pkg/generic/option_test.go: -------------------------------------------------------------------------------- 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 generic 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/dynamicgo/conv" 23 | 24 | "github.com/cloudwego/kitex/internal/test" 25 | ) 26 | 27 | func TestWithCustomDynamicGoConvOpts(t *testing.T) { 28 | convOpts := conv.Options{WriteRequireField: true, WriteDefaultField: true} 29 | var opt []Option 30 | opt = append(opt, WithCustomDynamicGoConvOpts(&convOpts)) 31 | var opts Options 32 | opts.apply(opt) 33 | test.DeepEqual(t, opts.dynamicgoConvOpts, convOpts) 34 | } 35 | 36 | func TestUseRawBodyForHTTPResp(t *testing.T) { 37 | var opt []Option 38 | opt = append(opt, UseRawBodyForHTTPResp(true)) 39 | var opts Options 40 | test.Assert(t, !opts.useRawBodyForHTTPResp) 41 | opts.apply(opt) 42 | test.Assert(t, opts.useRawBodyForHTTPResp) 43 | opt = opt[0:0] 44 | opt = append(opt, UseRawBodyForHTTPResp(false)) 45 | opts.apply(opt) 46 | test.Assert(t, !opts.useRawBodyForHTTPResp) 47 | } 48 | -------------------------------------------------------------------------------- /pkg/generic/pb_descriptor_provider.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 generic 18 | 19 | import ( 20 | dproto "github.com/cloudwego/dynamicgo/proto" 21 | 22 | "github.com/cloudwego/kitex/pkg/generic/proto" 23 | ) 24 | 25 | // PbDescriptorProvider provide service descriptor 26 | type PbDescriptorProvider interface { 27 | Closer 28 | // Provide return a channel for provide service descriptors 29 | Provide() <-chan proto.ServiceDescriptor 30 | } 31 | 32 | // PbDescriptorProvider provide service descriptor 33 | type PbDescriptorProviderDynamicGo interface { 34 | Closer 35 | // Provide return a channel for provide service descriptors 36 | Provide() <-chan *dproto.ServiceDescriptor 37 | } 38 | -------------------------------------------------------------------------------- /pkg/generic/proto/protobuf.go: -------------------------------------------------------------------------------- 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 proto 18 | 19 | import ( 20 | "context" 21 | ) 22 | 23 | // MessageReader read from ActualMsgBuf with method and returns a string 24 | type MessageReader interface { 25 | Read(ctx context.Context, method string, isClient bool, actualMsgBuf []byte) (interface{}, error) 26 | } 27 | 28 | // MessageWriter writes to a converts json to protobufs wireformat and returns an output bytebuffer 29 | type MessageWriter interface { 30 | Write(ctx context.Context, msg interface{}, method string, isClient bool) (interface{}, error) 31 | } 32 | -------------------------------------------------------------------------------- /pkg/generic/proto/raw_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 proto 18 | 19 | import ( 20 | "context" 21 | "reflect" 22 | "testing" 23 | 24 | "github.com/cloudwego/kitex/internal/test" 25 | ) 26 | 27 | func TestWriteRaw_Write(t *testing.T) { 28 | ctx := context.Background() 29 | wr := NewRawReaderWriter() 30 | 31 | data := []byte("test data") 32 | result, err := wr.Write(ctx, data, "method", true) 33 | test.Assert(t, err == nil) 34 | resultBytes, ok := result.([]byte) 35 | test.Assert(t, ok) 36 | test.Assert(t, reflect.DeepEqual(data, resultBytes)) 37 | } 38 | 39 | func TestReadRaw_Read(t *testing.T) { 40 | ctx := context.Background() 41 | reader := NewRawReaderWriter() 42 | 43 | data := []byte("test data for reading") 44 | result, err := reader.Read(ctx, "method", true, data) 45 | test.Assert(t, err == nil) 46 | 47 | resultBytes, ok := result.([]byte) 48 | test.Assert(t, ok) 49 | test.Assert(t, reflect.DeepEqual(data, resultBytes)) 50 | } 51 | -------------------------------------------------------------------------------- /pkg/generic/proto/type.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 proto 18 | 19 | import ( 20 | "github.com/jhump/protoreflect/desc" 21 | "github.com/jhump/protoreflect/dynamic" 22 | ) 23 | 24 | type ( 25 | ServiceDescriptor = *desc.ServiceDescriptor 26 | MessageDescriptor = *desc.MessageDescriptor 27 | ) 28 | 29 | // Deprecated: please use github.com/jhump/protoreflect/dynamic Message instead 30 | type Message interface { 31 | Marshal() ([]byte, error) 32 | TryGetFieldByNumber(fieldNumber int) (interface{}, error) 33 | TrySetFieldByNumber(fieldNumber int, val interface{}) error 34 | } 35 | 36 | // Deprecated: please use github.com/jhump/protoreflect/dynamic NewMessage instead 37 | func NewMessage(descriptor MessageDescriptor) Message { 38 | return dynamic.NewMessage(descriptor) 39 | } 40 | -------------------------------------------------------------------------------- /pkg/generic/reflect_test/idl/base.thrift: -------------------------------------------------------------------------------- 1 | namespace py base 2 | namespace go base 3 | namespace java com.bytedance.thrift.base 4 | 5 | struct TrafficEnv { 6 | 1: bool Open = false, 7 | 2: string Env = "", 8 | } 9 | 10 | struct Base { 11 | 1: string LogID = "", 12 | 2: string Caller = "", 13 | 3: string Addr = "", 14 | 4: string Client = "", 15 | 5: optional TrafficEnv TrafficEnv, 16 | 6: optional map Extra, 17 | } 18 | 19 | struct BaseResp { 20 | 1: string StatusMessage = "", 21 | 2: i32 StatusCode = 0, 22 | 3: optional map Extra, 23 | } 24 | -------------------------------------------------------------------------------- /pkg/generic/reflect_test/idl/example.thrift: -------------------------------------------------------------------------------- 1 | include "base.thrift" 2 | include "self_ref.thrift" 3 | namespace go kitex.test.server 4 | 5 | enum FOO { 6 | A = 1; 7 | } 8 | 9 | struct InnerBase { 10 | 255: base.Base Base, 11 | } 12 | 13 | struct MockElem { 14 | 1: string Bar 15 | } 16 | 17 | struct ExampleReq { 18 | 1: required string Msg = "Hello", 19 | 2: FOO Foo, 20 | 3: list TestList, 21 | 4: optional map TestMap, 22 | 5: list StrList, 23 | 6: list I64List = [1, 2, 3], 24 | 7: bool B, 25 | 255: base.Base Base, 26 | } 27 | struct ExampleResp { 28 | 1: required string Msg, 29 | 2: string required_field 30 | 255: base.BaseResp BaseResp, 31 | } 32 | exception Exception { 33 | 1: i32 code 34 | 2: string msg 35 | } 36 | 37 | struct A { 38 | 1: A self 39 | 2: self_ref.A a 40 | } 41 | 42 | service ExampleService { 43 | ExampleReq ExampleMethod(1: ExampleReq req)throws(1: Exception err), 44 | ExampleResp ExampleMethod2(1: ExampleReq req)throws(1: Exception err), 45 | A Foo(1: A req) 46 | string Ping(1: string msg) 47 | oneway void Oneway(1: string msg) 48 | void Void(1: string msg) 49 | } -------------------------------------------------------------------------------- /pkg/generic/reflect_test/idl/self_ref.thrift: -------------------------------------------------------------------------------- 1 | namespace go kitex.test.server 2 | 3 | struct A { 4 | 1: A self 5 | 2: string extra 6 | } -------------------------------------------------------------------------------- /pkg/generic/thrift/binary.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 thrift 18 | 19 | import ( 20 | "context" 21 | 22 | "github.com/cloudwego/gopkg/bufiox" 23 | "github.com/cloudwego/gopkg/protocol/thrift" 24 | "github.com/cloudwego/gopkg/protocol/thrift/base" 25 | ) 26 | 27 | // WriteBinary implement of MessageWriter 28 | type WriteBinary struct{} 29 | 30 | func NewWriteBinary() *WriteBinary { 31 | return &WriteBinary{} 32 | } 33 | 34 | func (w *WriteBinary) Write(ctx context.Context, out bufiox.Writer, msg interface{}, method string, isClient bool, requestBase *base.Base) error { 35 | bw := thrift.NewBufferWriter(out) 36 | defer bw.Recycle() 37 | if err := bw.WriteFieldStop(); err != nil { 38 | return err 39 | } 40 | return nil 41 | } 42 | -------------------------------------------------------------------------------- /pkg/generic/thrift/parse_option.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 thrift 18 | 19 | import "github.com/cloudwego/kitex/pkg/generic/descriptor" 20 | 21 | type parseOptions struct { 22 | goTag *descriptor.GoTagOption 23 | serviceName string 24 | } 25 | 26 | type ParseOption struct { 27 | F func(opt *parseOptions) 28 | } 29 | 30 | func (o *parseOptions) apply(opts []ParseOption) { 31 | for _, op := range opts { 32 | op.F(o) 33 | } 34 | } 35 | 36 | func WithGoTagDisabled(disable bool) ParseOption { 37 | return ParseOption{F: func(opt *parseOptions) { 38 | opt.goTag = &descriptor.GoTagOption{ 39 | IsGoAliasDisabled: disable, 40 | } 41 | }} 42 | } 43 | 44 | // WithIDLServiceName specifies the target IDL service to be parsed. 45 | // NOTE: with this option, the specified service is prioritized and parse mode will be ignored. 46 | func WithIDLServiceName(serviceName string) ParseOption { 47 | return ParseOption{F: func(opt *parseOptions) { 48 | opt.serviceName = serviceName 49 | }} 50 | } 51 | -------------------------------------------------------------------------------- /pkg/generic/thrift/thrift.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 thrift provides thrift idl parser and codec for generic call 18 | package thrift 19 | 20 | import ( 21 | "context" 22 | 23 | "github.com/cloudwego/gopkg/bufiox" 24 | "github.com/cloudwego/gopkg/protocol/thrift/base" 25 | ) 26 | 27 | const ( 28 | structWrapLen = 4 29 | ) 30 | 31 | // MessageReader read from thrift.TProtocol with method 32 | type MessageReader interface { 33 | Read(ctx context.Context, method string, isClient bool, dataLen int, in bufiox.Reader) (interface{}, error) 34 | } 35 | 36 | // MessageWriter write to thrift.TProtocol 37 | type MessageWriter interface { 38 | Write(ctx context.Context, out bufiox.Writer, msg interface{}, method string, isClient bool, requestBase *base.Base) error 39 | } 40 | -------------------------------------------------------------------------------- /pkg/gofunc/go_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 gofunc 18 | 19 | import ( 20 | "context" 21 | "sync" 22 | "testing" 23 | ) 24 | 25 | func TestRecoverGoFuncWithInfo(t *testing.T) { 26 | wg := sync.WaitGroup{} 27 | wg.Add(1) 28 | task := func() { 29 | panic("test") 30 | } 31 | SetPanicHandler(func(info *Info, panicErr interface{}, panicStack string) { 32 | wg.Done() 33 | }) 34 | ctx := context.Background() 35 | remoteService := "aaa.aaa.aaa" 36 | remoteAddr := "127.0.0.1:888" 37 | RecoverGoFuncWithInfo(ctx, task, NewBasicInfo(remoteService, remoteAddr)) 38 | wg.Wait() 39 | } 40 | -------------------------------------------------------------------------------- /pkg/kerrors/streaming_errors.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 kerrors 18 | 19 | // ErrStreamingProtocol is the parent type of all streaming protocol(e.g. gRPC, TTHeader Streaming) 20 | // related but not user-aware errors. 21 | var ErrStreamingProtocol = &basicError{"streaming protocol error"} 22 | -------------------------------------------------------------------------------- /pkg/limit/limit.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 limit 18 | 19 | // Updater is used to update the limit dynamically. 20 | type Updater interface { 21 | UpdateLimit(opt *Option) (updated bool) 22 | } 23 | 24 | // Option is used to config the limiter. 25 | type Option struct { 26 | MaxConnections int 27 | MaxQPS int 28 | 29 | // UpdateControl receives a Updater which gives the limitation provider 30 | // the ability to update limit dynamically. 31 | UpdateControl func(u Updater) 32 | } 33 | 34 | // Valid checks if the option is valid. 35 | func (lo *Option) Valid() bool { 36 | return lo.MaxConnections > 0 || lo.MaxQPS > 0 37 | } 38 | -------------------------------------------------------------------------------- /pkg/limit/limit_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 limit 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestValid(t *testing.T) { 26 | test.Assert(t, (&Option{MaxConnections: 0, MaxQPS: 0}).Valid() == false) 27 | test.Assert(t, (&Option{MaxConnections: 1, MaxQPS: 0}).Valid() == true) 28 | test.Assert(t, (&Option{MaxConnections: 0, MaxQPS: 1}).Valid() == true) 29 | } 30 | -------------------------------------------------------------------------------- /pkg/limiter/dummy.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 limiter 18 | 19 | import ( 20 | "context" 21 | "time" 22 | ) 23 | 24 | // DummyConcurrencyLimiter implements ConcurrencyLimiter but without actual limitation. 25 | type DummyConcurrencyLimiter struct{} 26 | 27 | // Acquire . 28 | func (dcl *DummyConcurrencyLimiter) Acquire(ctx context.Context) bool { 29 | return true 30 | } 31 | 32 | // Release . 33 | func (dcl *DummyConcurrencyLimiter) Release(ctx context.Context) {} 34 | 35 | // Status . 36 | func (dcl *DummyConcurrencyLimiter) Status(ctx context.Context) (limit, occupied int) { 37 | return 38 | } 39 | 40 | // DummyRateLimiter implements RateLimiter but without actual limitation. 41 | type DummyRateLimiter struct{} 42 | 43 | // Acquire . 44 | func (drl *DummyRateLimiter) Acquire(ctx context.Context) bool { return true } 45 | 46 | // Status . 47 | func (drl *DummyRateLimiter) Status(ctx context.Context) (max, current int, interval time.Duration) { 48 | return 49 | } 50 | -------------------------------------------------------------------------------- /pkg/limiter/limiter_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 limiter 18 | 19 | import ( 20 | "testing" 21 | "time" 22 | 23 | "github.com/cloudwego/kitex/internal/test" 24 | "github.com/cloudwego/kitex/pkg/limit" 25 | ) 26 | 27 | func TestLimiterWrapper(t *testing.T) { 28 | cl := NewConnectionLimiter(1000) 29 | rl := NewQPSLimiter(time.Second, 10000) 30 | lw := NewLimiterWrapper(cl, rl) 31 | test.Assert(t, lw.UpdateLimit(nil) == false) 32 | opt := &limit.Option{ 33 | MaxConnections: 2000, 34 | MaxQPS: 20000, 35 | } 36 | test.Assert(t, lw.UpdateLimit(opt) == true) 37 | test.Assert(t, cl.(*connectionLimiter).lim == 2000) 38 | test.Assert(t, rl.(*qpsLimiter).limit == 20000) 39 | } 40 | -------------------------------------------------------------------------------- /pkg/loadbalance/dummy_picker.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 loadbalance 18 | 19 | import ( 20 | "context" 21 | 22 | "github.com/cloudwego/kitex/pkg/discovery" 23 | ) 24 | 25 | var _ Picker = &DummyPicker{} 26 | 27 | // DummyPicker is a picker that always returns nil on Next. 28 | type DummyPicker struct{} 29 | 30 | // Next implements the Picker interface. 31 | func (np *DummyPicker) Next(ctx context.Context, request interface{}) (ins discovery.Instance) { 32 | return nil 33 | } 34 | -------------------------------------------------------------------------------- /pkg/loadbalance/iterator.go: -------------------------------------------------------------------------------- 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 loadbalance 18 | 19 | import ( 20 | "sync/atomic" 21 | 22 | "github.com/bytedance/gopkg/lang/fastrand" 23 | ) 24 | 25 | // round implement a strict Round Robin algorithm 26 | type round struct { 27 | state uint64 // 8 bytes 28 | _ [7]uint64 // + 7 * 8 bytes 29 | // = 64 bytes 30 | } 31 | 32 | func (r *round) Next() uint64 { 33 | return atomic.AddUint64(&r.state, 1) 34 | } 35 | 36 | func newRound() *round { 37 | r := &round{} 38 | return r 39 | } 40 | 41 | func newRandomRound() *round { 42 | r := &round{ 43 | state: fastrand.Uint64(), 44 | } 45 | return r 46 | } 47 | -------------------------------------------------------------------------------- /pkg/loadbalance/lbcache/shared_ticker.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 lbcache 18 | 19 | import ( 20 | "sync" 21 | "time" 22 | 23 | "golang.org/x/sync/singleflight" 24 | 25 | "github.com/cloudwego/kitex/pkg/utils" 26 | ) 27 | 28 | var ( 29 | // insert, not delete 30 | sharedTickers sync.Map 31 | sharedTickersSfg singleflight.Group 32 | ) 33 | 34 | func getSharedTicker(b *Balancer, refreshInterval time.Duration) *utils.SharedTicker { 35 | sti, ok := sharedTickers.Load(refreshInterval) 36 | if ok { 37 | st := sti.(*utils.SharedTicker) 38 | st.Add(b) 39 | return st 40 | } 41 | v, _, _ := sharedTickersSfg.Do(refreshInterval.String(), func() (interface{}, error) { 42 | st := utils.NewSharedTicker(refreshInterval) 43 | sharedTickers.Store(refreshInterval, st) 44 | return st, nil 45 | }) 46 | st := v.(*utils.SharedTicker) 47 | // Add without singleflight, 48 | // because we need all refreshers those call this function to add themselves to SharedTicker 49 | st.Add(b) 50 | return st 51 | } 52 | -------------------------------------------------------------------------------- /pkg/loadbalance/loadbalancer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 loadbalance 18 | 19 | import ( 20 | "context" 21 | 22 | "github.com/cloudwego/kitex/pkg/discovery" 23 | ) 24 | 25 | // Picker picks an instance for next RPC call. 26 | type Picker interface { 27 | Next(ctx context.Context, request interface{}) discovery.Instance 28 | } 29 | 30 | // Loadbalancer generates pickers for the given service discovery result. 31 | type Loadbalancer interface { 32 | GetPicker(discovery.Result) Picker 33 | Name() string // unique key 34 | } 35 | 36 | // Rebalancer is a kind of Loadbalancer that performs rebalancing when the result of service discovery changes. 37 | type Rebalancer interface { 38 | Rebalance(discovery.Change) 39 | Delete(discovery.Change) 40 | } 41 | -------------------------------------------------------------------------------- /pkg/protocol/bthrift/README.md: -------------------------------------------------------------------------------- 1 | # bthrift 2 | 3 | `bthrift` is no longer used, but the legacy generated code may still rely on it. For newly added code, should use `github.com/cloudwego/gopkg/protocol/thrift` instead. 4 | 5 | We're planning to get rid of `github.com/apache/thrift`, here are steps we have done: 6 | 1. Removed unnecessary dependencies of apache from kitex 7 | 2. Moved all apache dependencies to `bthrift/apache`, mainly types, interfaces and consts 8 | - We may use type alias at the beginning for better compatibility 9 | - `bthrift/apache`calls `apache.RegisterXXX` in `gopkg` for step 4 10 | 3. For internal dependencies of apache, use `gopkg` 11 | 4. For existing framework code working with apache thrift: 12 | - Use `gopkg/protocol/thrift/apache` 13 | 5. For Kitex tool: 14 | - Use `gopkg/protocol/thrift` for fastcodec 15 | - replace `github.com/apache/thrift` with `bthrift/apache` 16 | - by using `thrift_import_path` parameter of thriftgo 17 | 18 | The final step we planned to do in kitex version v0.12.0: 19 | * Add go.mod for `bthrift` 20 | * Remove the last `github.com/apache/thrift` dependencies (Remove the import 'github.com/cloudwego/kitex/pkg/protocol/bthrift' and 'github.com/cloudwego/kitex/pkg/protocol/bthrift/apache') 21 | * `ThriftMessageCodec` of `pkg/utils` 22 | * `MessageReader` and `MessageWriter` interfaces in `pkg/remote/codec/thrift` 23 | * `BinaryProtocol` type in `pkg/remote/codec/thrift` 24 | * basic codec tests in `pkg/remote/codec/thrift` 25 | * kitex DOESN'T DEPEND bthrift, bthrift will only be dependent in the generated code(if has apache thrift code) 26 | -------------------------------------------------------------------------------- /pkg/protocol/bthrift/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/cloudwego/kitex/pkg/protocol/bthrift 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/apache/thrift v0.13.0 7 | github.com/cloudwego/gopkg v0.1.3-0.20241115063537-a218fe69d609 8 | github.com/cloudwego/thriftgo v0.3.18 9 | ) 10 | 11 | require github.com/bytedance/gopkg v0.1.1 // indirect 12 | -------------------------------------------------------------------------------- /pkg/protocol/bthrift/internal/test/README.md: -------------------------------------------------------------------------------- 1 | # test 2 | 3 | `test` is copied from `kitex/internal/test` for preparing adding `go.mod` under `bthrift` in the future. 4 | `bthrift` will not import `kitex` other packages, vice versa. 5 | 6 | for more details, see README.md of `bthrift` 7 | -------------------------------------------------------------------------------- /pkg/remote/codec/protobuf/error.proto: -------------------------------------------------------------------------------- 1 | syntax="proto3"; 2 | package protobuf; 3 | 4 | option go_package = "github.com/cloudwego/kitex/pkg/remote/codec/protobuf"; 5 | 6 | // Messages used for transporting error between server and client. 7 | message ErrorProto { 8 | int32 TypeID = 1; 9 | string Message = 2; 10 | } 11 | 12 | // protoc --go_out=$GOPATH/src kitex/pkg/remote/codec/protobuf/error.proto -------------------------------------------------------------------------------- /pkg/remote/codec/protobuf/test.proto: -------------------------------------------------------------------------------- 1 | syntax="proto3"; 2 | package protobuf; 3 | 4 | option go_package = "github.com/cloudwego/kitex/pkg/remote/codec/protobuf"; 5 | 6 | message MockReq { 7 | string msg = 1; 8 | map strMap = 2; 9 | repeated string strList = 3; 10 | } 11 | 12 | // protoc --proto_path=. --go_out=$GOPATH/src test.proto -------------------------------------------------------------------------------- /pkg/remote/codec/thrift/codec.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 thrift 18 | 19 | import ( 20 | "reflect" 21 | "sync" 22 | ) 23 | 24 | type typeCodec struct { 25 | FastCodec bool 26 | Frugal bool 27 | Apache bool 28 | } 29 | 30 | var type2codec sync.Map 31 | 32 | func getTypeCodec(data interface{}) typeCodec { 33 | rt := reflect.TypeOf(data) 34 | v, ok := type2codec.Load(rt) 35 | if ok { 36 | return v.(typeCodec) 37 | } 38 | // slow path 39 | // not that slow then no lock needed. 40 | c := typeCodec{ 41 | FastCodec: fastCodecAvailable(data), 42 | Frugal: frugalAvailable(data), 43 | Apache: apacheCodecAvailable(data), 44 | } 45 | type2codec.Store(rt, c) 46 | return c 47 | } 48 | -------------------------------------------------------------------------------- /pkg/remote/connpool/utils.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 connpool 18 | 19 | import ( 20 | "net" 21 | "sync/atomic" 22 | ) 23 | 24 | // ConnWithPkgSize wraps a connection and records the bytes read or written through it. 25 | type ConnWithPkgSize struct { 26 | net.Conn 27 | Written int32 28 | Readn int32 29 | } 30 | 31 | // Read implements the net.Conn interface. 32 | func (c *ConnWithPkgSize) Read(b []byte) (n int, err error) { 33 | n, err = c.Conn.Read(b) 34 | atomic.AddInt32(&(c.Readn), int32(n)) 35 | return n, err 36 | } 37 | 38 | // Write implements the net.Conn interface. 39 | func (c *ConnWithPkgSize) Write(b []byte) (n int, err error) { 40 | n, err = c.Conn.Write(b) 41 | atomic.AddInt32(&(c.Written), int32(n)) 42 | return n, err 43 | } 44 | 45 | // Close implements the net.Conn interface. 46 | func (c *ConnWithPkgSize) Close() error { 47 | err := c.Conn.Close() 48 | c.Conn = nil 49 | return err 50 | } 51 | -------------------------------------------------------------------------------- /pkg/remote/dialer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 remote 18 | 19 | import ( 20 | "net" 21 | "time" 22 | ) 23 | 24 | // Dialer is used to dial and get a connection. 25 | type Dialer interface { 26 | DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) 27 | } 28 | 29 | // NewDefaultDialer is used to create a default dialer. 30 | func NewDefaultDialer() Dialer { 31 | return &SynthesizedDialer{ 32 | DialFunc: net.DialTimeout, 33 | } 34 | } 35 | 36 | // SynthesizedDialer is used to synthesize a DialFunc to implement a Dialer. 37 | type SynthesizedDialer struct { 38 | DialFunc func(network, address string, timeout time.Duration) (net.Conn, error) 39 | } 40 | 41 | // DialTimeout implements the Dialer interface. 42 | func (sd SynthesizedDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) { 43 | return sd.DialFunc(network, address, timeout) 44 | } 45 | -------------------------------------------------------------------------------- /pkg/remote/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 remote defines all interfaces that are used to do transport with peer side 18 | // and contains default extension implementations. 19 | package remote 20 | -------------------------------------------------------------------------------- /pkg/remote/role.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 remote 18 | 19 | // RPCRole is to distinguished client or server 20 | type RPCRole int 21 | 22 | // RPC role 23 | const ( 24 | Client RPCRole = iota 25 | Server 26 | ) 27 | -------------------------------------------------------------------------------- /pkg/remote/trans/gonet/client_handler.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 gonet 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/remote" 21 | "github.com/cloudwego/kitex/pkg/remote/trans" 22 | ) 23 | 24 | type cliTransHandlerFactory struct{} 25 | 26 | // NewCliTransHandlerFactory returns a new remote.ClientTransHandlerFactory for go net. 27 | func NewCliTransHandlerFactory() remote.ClientTransHandlerFactory { 28 | return &cliTransHandlerFactory{} 29 | } 30 | 31 | // NewTransHandler implements the remote.ClientTransHandlerFactory interface. 32 | func (f *cliTransHandlerFactory) NewTransHandler(opt *remote.ClientOption) (remote.ClientTransHandler, error) { 33 | return newCliTransHandler(opt) 34 | } 35 | 36 | func newCliTransHandler(opt *remote.ClientOption) (remote.ClientTransHandler, error) { 37 | return trans.NewDefaultCliTransHandler(opt, NewGonetExtension()) 38 | } 39 | -------------------------------------------------------------------------------- /pkg/remote/trans/gonet/dialer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 gonet 18 | 19 | import ( 20 | "context" 21 | "net" 22 | "time" 23 | 24 | "github.com/cloudwego/kitex/pkg/remote" 25 | ) 26 | 27 | // NewDialer returns the default go net dialer. 28 | func NewDialer() remote.Dialer { 29 | return &dialer{} 30 | } 31 | 32 | type dialer struct { 33 | net.Dialer 34 | } 35 | 36 | func (d *dialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) { 37 | ctx, cancel := context.WithTimeout(context.Background(), timeout) 38 | defer cancel() 39 | return d.DialContext(ctx, network, address) 40 | } 41 | -------------------------------------------------------------------------------- /pkg/remote/trans/gonet/server_handler.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 gonet 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/remote" 21 | "github.com/cloudwego/kitex/pkg/remote/trans" 22 | ) 23 | 24 | type svrTransHandlerFactory struct{} 25 | 26 | // NewSvrTransHandlerFactory creates a default go net server transport handler factory. 27 | func NewSvrTransHandlerFactory() remote.ServerTransHandlerFactory { 28 | return &svrTransHandlerFactory{} 29 | } 30 | 31 | // NewTransHandler implements the remote.ServerTransHandlerFactory interface. 32 | func (f *svrTransHandlerFactory) NewTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) { 33 | return newSvrTransHandler(opt) 34 | } 35 | 36 | func newSvrTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) { 37 | return trans.NewDefaultSvrTransHandler(opt, NewGonetExtension()) 38 | } 39 | -------------------------------------------------------------------------------- /pkg/remote/trans/invoke/invoke.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 invoke . 18 | package invoke 19 | 20 | import ( 21 | "github.com/cloudwego/kitex/pkg/remote" 22 | "github.com/cloudwego/kitex/pkg/remote/trans" 23 | ) 24 | 25 | type ivkTransHandlerFactory struct{} 26 | 27 | // NewIvkTransHandlerFactory creates a ServerTransHandlerFactory for invoker. 28 | func NewIvkTransHandlerFactory() remote.ServerTransHandlerFactory { 29 | return &ivkTransHandlerFactory{} 30 | } 31 | 32 | // NewTransHandler implements the remote.ServerTransHandlerFactory interface. 33 | func (f *ivkTransHandlerFactory) NewTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) { 34 | return newIvkTransHandler(opt) 35 | } 36 | 37 | func newIvkTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) { 38 | return trans.NewDefaultSvrTransHandler(opt, newIvkConnExtension()) 39 | } 40 | -------------------------------------------------------------------------------- /pkg/remote/trans/invoke/invoke_handler_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 invoke 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/mocks" 23 | "github.com/cloudwego/kitex/pkg/remote" 24 | ) 25 | 26 | func Test_ivkHandler_Call(t *testing.T) { 27 | hdlFct := mocks.NewMockSvrTransHandlerFactory(&mocks.MockSvrTransHandler{}) 28 | hdl, _ := hdlFct.NewTransHandler(&remote.ServerOption{}) 29 | handler, _ := NewIvkHandler(&remote.ServerOption{}, hdl) 30 | err := handler.Call(NewMessage(nil, nil)) 31 | if err != nil { 32 | t.Fatal(err) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /pkg/remote/trans/netpoll/client_handler.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 netpoll 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/remote" 21 | "github.com/cloudwego/kitex/pkg/remote/trans" 22 | ) 23 | 24 | type cliTransHandlerFactory struct{} 25 | 26 | // NewCliTransHandlerFactory returns a new remote.ClientTransHandlerFactory for netpoll. 27 | func NewCliTransHandlerFactory() remote.ClientTransHandlerFactory { 28 | return &cliTransHandlerFactory{} 29 | } 30 | 31 | // NewTransHandler implements the remote.ClientTransHandlerFactory interface. 32 | func (f *cliTransHandlerFactory) NewTransHandler(opt *remote.ClientOption) (remote.ClientTransHandler, error) { 33 | return newCliTransHandler(opt) 34 | } 35 | 36 | func newCliTransHandler(opt *remote.ClientOption) (remote.ClientTransHandler, error) { 37 | return trans.NewDefaultCliTransHandler(opt, NewNetpollConnExtension()) 38 | } 39 | -------------------------------------------------------------------------------- /pkg/remote/trans/netpoll/dialer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 netpoll 18 | 19 | import ( 20 | "github.com/cloudwego/netpoll" 21 | 22 | "github.com/cloudwego/kitex/pkg/remote" 23 | ) 24 | 25 | // NewDialer returns the default netpoll dialer. 26 | func NewDialer() remote.Dialer { 27 | return netpoll.NewDialer() 28 | } 29 | -------------------------------------------------------------------------------- /pkg/remote/trans/netpoll/http_client.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 netpoll 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/remote" 21 | ) 22 | 23 | type httpCliTransHandlerFactory struct{} 24 | 25 | // NewHTTPCliTransHandlerFactory creates a netpoll http client transport handler factory. 26 | func NewHTTPCliTransHandlerFactory() remote.ClientTransHandlerFactory { 27 | return &httpCliTransHandlerFactory{} 28 | } 29 | 30 | // NewTransHandler implements the remote.ClientTransHandlerFactory interface. 31 | func (f *httpCliTransHandlerFactory) NewTransHandler(opt *remote.ClientOption) (remote.ClientTransHandler, error) { 32 | return newHTTPCliTransHandler(opt, NewNetpollConnExtension()) 33 | } 34 | -------------------------------------------------------------------------------- /pkg/remote/trans/netpoll/server_handler.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 netpoll 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/remote" 21 | "github.com/cloudwego/kitex/pkg/remote/trans" 22 | ) 23 | 24 | type svrTransHandlerFactory struct{} 25 | 26 | // NewSvrTransHandlerFactory creates a default netpoll server transport handler factory. 27 | func NewSvrTransHandlerFactory() remote.ServerTransHandlerFactory { 28 | return &svrTransHandlerFactory{} 29 | } 30 | 31 | // NewTransHandler implements the remote.ServerTransHandlerFactory interface. 32 | func (f *svrTransHandlerFactory) NewTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) { 33 | return newSvrTransHandler(opt) 34 | } 35 | 36 | func newSvrTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) { 37 | return trans.NewDefaultSvrTransHandler(opt, NewNetpollConnExtension()) 38 | } 39 | -------------------------------------------------------------------------------- /pkg/remote/trans/netpollmux/mux_transport.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 netpollmux 18 | 19 | import ( 20 | "encoding/binary" 21 | "fmt" 22 | 23 | "github.com/cloudwego/netpoll" 24 | 25 | "github.com/cloudwego/kitex/pkg/remote/codec" 26 | ) 27 | 28 | // 0-4Byte length, 4-8Byte version check, 8-12Byte seqID 29 | func parseHeader(reader netpoll.Reader) (length int, seqID int32, err error) { 30 | buf, err := reader.Peek(3 * codec.Size32) 31 | if err != nil { 32 | return 0, 0, err 33 | } 34 | // add length self 4 bytes 35 | length = int(binary.BigEndian.Uint32(buf[:codec.Size32])) + 4 36 | if length <= 4 { 37 | return 0, 0, fmt.Errorf("mux read head length[%d] invalid", length-4) 38 | } 39 | 40 | // check version 41 | if !codec.IsTTHeader(buf[:2*codec.Size32]) { 42 | return 0, 0, fmt.Errorf("mux read check protocol failed, just support TTHeader") 43 | } 44 | seqID = int32(binary.BigEndian.Uint32(buf[2*codec.Size32 : 3*codec.Size32])) 45 | return length, seqID, nil 46 | } 47 | -------------------------------------------------------------------------------- /pkg/remote/trans/nphttp2/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 nphttp2 transport powered by netpoll 18 | package nphttp2 19 | -------------------------------------------------------------------------------- /pkg/remote/trans/nphttp2/grpc/context_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 grpc 18 | 19 | import ( 20 | "context" 21 | "errors" 22 | "testing" 23 | 24 | "github.com/cloudwego/kitex/internal/test" 25 | ) 26 | 27 | func TestContextWithCancelReason(t *testing.T) { 28 | ctx0, cancel0 := context.WithCancel(context.Background()) 29 | ctx, cancel := newContextWithCancelReason(ctx0, cancel0) 30 | 31 | // cancel contextWithCancelReason 32 | expectErr := errors.New("testing") 33 | cancel(expectErr) 34 | test.Assert(t, ctx0.Err() == context.Canceled) 35 | test.Assert(t, ctx.Err() == expectErr) 36 | 37 | // cancel underlying context 38 | ctx0, cancel0 = context.WithCancel(context.Background()) 39 | ctx, _ = newContextWithCancelReason(ctx0, cancel0) 40 | cancel0() 41 | test.Assert(t, ctx0.Err() == context.Canceled) 42 | test.Assert(t, ctx.Err() == context.Canceled) 43 | } 44 | -------------------------------------------------------------------------------- /pkg/remote/trans/nphttp2/grpc/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 | // The files in grpc package are forked from gRPC[github.com/grpc/grpc-go], 18 | // and we keep the original Copyright[Copyright 2017 gRPC authors] and License of gRPC for those files. 19 | // We also need to modify as we need, the modifications are Copyright of 2021 CloudWeGo Authors. 20 | // Thanks for gRPC authors! Below is the source code information: 21 | // 22 | // Repo: github.com/grpc/grpc-go 23 | // Forked Version: v1.26.0 24 | package grpc 25 | -------------------------------------------------------------------------------- /pkg/remote/trans/nphttp2/grpc/grpcframe/errors_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 The Go Authors. All rights reserved. 3 | * Use of this source code is governed by a BSD-style 4 | * license that can be found in the LICENSE file. 5 | * 6 | * Code forked from golang v1.17.4 7 | */ 8 | 9 | package grpcframe 10 | 11 | import ( 12 | "testing" 13 | 14 | "golang.org/x/net/http2" 15 | ) 16 | 17 | func TestErrCodeString(t *testing.T) { 18 | tests := []struct { 19 | err http2.ErrCode 20 | want string 21 | }{ 22 | {http2.ErrCodeProtocol, "PROTOCOL_ERROR"}, 23 | {0xd, "HTTP_1_1_REQUIRED"}, 24 | {0xf, "unknown error code 0xf"}, 25 | } 26 | for i, tt := range tests { 27 | got := tt.err.String() 28 | if got != tt.want { 29 | t.Errorf("%d. Error = %q; want %q", i, got, tt.want) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /pkg/remote/trans/nphttp2/grpc/testutils/status_equal.go: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright 2019 gRPC authors. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * This file may have been modified by CloudWeGo authors. All CloudWeGo 18 | * Modifications are Copyright 2021 CloudWeGo Authors. 19 | */ 20 | 21 | package testutils 22 | 23 | import ( 24 | "google.golang.org/protobuf/proto" 25 | 26 | "github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/status" 27 | ) 28 | 29 | // StatusErrEqual returns true iff both err1 and err2 wrap status.Status errors 30 | // and their underlying status protos are equal. 31 | func StatusErrEqual(err1, err2 error) bool { 32 | status1, ok := status.FromError(err1) 33 | if !ok { 34 | return false 35 | } 36 | status2, ok := status.FromError(err2) 37 | if !ok { 38 | return false 39 | } 40 | return proto.Equal(status1.Proto(), status2.Proto()) 41 | } 42 | -------------------------------------------------------------------------------- /pkg/remote/trans/nphttp2/peer/peer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 peer 18 | 19 | import ( 20 | "context" 21 | "net" 22 | ) 23 | 24 | type peerKey struct{} 25 | 26 | // Peer contains the information of the peer for an RPC, such as the address 27 | // and authentication information. 28 | type Peer struct { 29 | // Addr is the peer address. 30 | Addr net.Addr 31 | } 32 | 33 | // GRPCPeer is used for client to get remote service address 34 | func GRPCPeer(ctx context.Context, p *Peer) context.Context { 35 | return context.WithValue(ctx, peerKey{}, p) 36 | } 37 | 38 | func GetPeerFromContext(ctx context.Context) (peer *Peer, ok bool) { 39 | p := ctx.Value(peerKey{}) 40 | if p != nil { 41 | return p.(*Peer), true 42 | } 43 | return nil, false 44 | } 45 | -------------------------------------------------------------------------------- /pkg/remote/trans/nphttp2/peer/peer_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 peer 18 | 19 | import ( 20 | "testing" 21 | 22 | "golang.org/x/net/context" 23 | 24 | "github.com/cloudwego/kitex/internal/test" 25 | ) 26 | 27 | func TestPeer(t *testing.T) { 28 | p := Peer{} 29 | ctx := GRPCPeer(context.Background(), &p) 30 | peer, ok := GetPeerFromContext(ctx) 31 | test.Assert(t, ok) 32 | test.Assert(t, peer == &p) 33 | } 34 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/client_provider_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 ttstream 18 | 19 | import ( 20 | "context" 21 | "sync/atomic" 22 | "testing" 23 | 24 | "github.com/cloudwego/kitex/internal/test" 25 | "github.com/cloudwego/kitex/pkg/remote/trans/ttstream/ktx" 26 | ) 27 | 28 | func Test_registerStreamCancelCallback(t *testing.T) { 29 | ctx, cancel := ktx.WithCancel(context.Background()) 30 | s := &stream{peerEOF: 1} 31 | registerStreamCancelCallback(ctx, s) 32 | cancel() 33 | test.Assert(t, atomic.LoadInt32(&s.selfEOF) == 1) 34 | } 35 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/client_trans_pool.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 ttstream 18 | 19 | import ( 20 | "github.com/cloudwego/netpoll" 21 | ) 22 | 23 | var dialer = netpoll.NewDialer() 24 | 25 | type transPool interface { 26 | Get(network, addr string) (trans *transport, err error) 27 | Put(trans *transport) 28 | } 29 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/client_trans_pool_shortconn.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 ttstream 18 | 19 | import ( 20 | "errors" 21 | "time" 22 | ) 23 | 24 | func newShortConnTransPool() transPool { 25 | return &shortConnTransPool{} 26 | } 27 | 28 | type shortConnTransPool struct{} 29 | 30 | func (p *shortConnTransPool) Get(network, addr string) (*transport, error) { 31 | // create new connection 32 | conn, err := dialer.DialConnection(network, addr, time.Second) 33 | if err != nil { 34 | return nil, err 35 | } 36 | // create new transport 37 | trans := newTransport(clientTransport, conn, p) 38 | return trans, nil 39 | } 40 | 41 | func (p *shortConnTransPool) Put(trans *transport) { 42 | _ = trans.Close(errTransport.WithCause(errors.New("short connection closed"))) 43 | } 44 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/container/linklist.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 container 18 | 19 | type linkNode[ValueType any] struct { 20 | val ValueType 21 | next *linkNode[ValueType] 22 | } 23 | 24 | func (n *linkNode[ValueType]) reset() { 25 | var nilVal ValueType 26 | n.val = nilVal 27 | n.next = nil 28 | } 29 | 30 | type doubleLinkNode[ValueType any] struct { 31 | val ValueType 32 | next *doubleLinkNode[ValueType] 33 | last *doubleLinkNode[ValueType] 34 | } 35 | 36 | func (n *doubleLinkNode[ValueType]) reset() { 37 | var nilVal ValueType 38 | n.val = nilVal 39 | n.next = nil 40 | n.last = nil 41 | } 42 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/container/queue_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 container 18 | 19 | import ( 20 | "sync" 21 | "testing" 22 | 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | func TestQueue(t *testing.T) { 27 | q := NewQueue[int]() 28 | round := 100000 29 | var wg sync.WaitGroup 30 | wg.Add(1) 31 | go func() { 32 | for i := 0; i < round; i++ { 33 | q.Add(1) 34 | } 35 | }() 36 | sum := 0 37 | for sum < round { 38 | v, ok := q.Get() 39 | if ok { 40 | sum += v 41 | } 42 | } 43 | test.DeepEqual(t, sum, round) 44 | test.DeepEqual(t, q.Size(), 0) 45 | } 46 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/ktx/ktx_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 ktx 18 | 19 | import ( 20 | "context" 21 | "testing" 22 | ) 23 | 24 | func TestKtx(t *testing.T) { 25 | ctx := context.Background() 26 | 27 | // server start 28 | ctx, cancelFunc := WithCancel(ctx) 29 | 30 | // client call 31 | var clientCanceled int32 32 | RegisterCancelCallback(ctx, func() { 33 | clientCanceled++ 34 | }) 35 | 36 | // server recv exception 37 | cancelFunc() 38 | if clientCanceled != 1 { 39 | t.Fatal() 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/server_provider_option.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 ttstream 18 | 19 | // ServerProviderOption define server provider options 20 | type ServerProviderOption func(pc *serverProvider) 21 | 22 | // WithServerMetaFrameHandler register TTHeader Streaming meta frame handler 23 | func WithServerMetaFrameHandler(handler MetaFrameHandler) ServerProviderOption { 24 | return func(sp *serverProvider) { 25 | sp.metaHandler = handler 26 | } 27 | } 28 | 29 | // WithServerHeaderFrameHandler register TTHeader Streaming header frame handler 30 | func WithServerHeaderFrameHandler(handler HeaderFrameReadHandler) ServerProviderOption { 31 | return func(sp *serverProvider) { 32 | sp.headerHandler = handler 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /pkg/remote/trans/ttstream/stream_writer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 ttstream 18 | 19 | var _ streamWriter = (*transport)(nil) 20 | 21 | type streamWriter interface { 22 | WriteFrame(f *Frame) error 23 | CloseStream(sid int32) error 24 | } 25 | -------------------------------------------------------------------------------- /pkg/remote/trans_errors_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 remote 18 | 19 | import ( 20 | "errors" 21 | "io" 22 | "testing" 23 | 24 | "github.com/cloudwego/kitex/internal/test" 25 | ) 26 | 27 | func TestTransError(t *testing.T) { 28 | errMsg := "mock err" 29 | transErr := NewTransError(InternalError, io.ErrShortWrite) 30 | test.Assert(t, errors.Is(transErr, io.ErrShortWrite)) 31 | 32 | transErr = NewTransError(InternalError, NewTransErrorWithMsg(100, errMsg)) 33 | test.Assert(t, transErr.TypeID() == 100) 34 | test.Assert(t, transErr.Error() == errMsg, transErr.Error()) 35 | } 36 | -------------------------------------------------------------------------------- /pkg/remote/trans_meta.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 remote 18 | 19 | import ( 20 | "context" 21 | ) 22 | 23 | // MetaHandler reads or writes metadata through certain protocol. 24 | type MetaHandler interface { 25 | WriteMeta(ctx context.Context, msg Message) (context.Context, error) 26 | ReadMeta(ctx context.Context, msg Message) (context.Context, error) 27 | } 28 | 29 | // StreamingMetaHandler reads or writes metadata through streaming header(http2 header) 30 | type StreamingMetaHandler interface { 31 | // writes metadata before create a stream 32 | OnConnectStream(ctx context.Context) (context.Context, error) 33 | // reads metadata before read first message from stream 34 | OnReadStream(ctx context.Context) (context.Context, error) 35 | } 36 | -------------------------------------------------------------------------------- /pkg/remote/trans_server.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 remote 18 | 19 | import ( 20 | "net" 21 | 22 | "github.com/cloudwego/kitex/pkg/utils" 23 | ) 24 | 25 | // TransServerFactory is used to create TransServer instances. 26 | type TransServerFactory interface { 27 | NewTransServer(opt *ServerOption, transHdlr ServerTransHandler) TransServer 28 | } 29 | 30 | // TransServer is the abstraction for remote server. 31 | type TransServer interface { 32 | CreateListener(net.Addr) (net.Listener, error) 33 | BootstrapServer(net.Listener) (err error) 34 | Shutdown() error 35 | ConnCount() utils.AtomicInt 36 | } 37 | -------------------------------------------------------------------------------- /pkg/remote/transmeta/http_metakey.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 transmeta 18 | 19 | // key of http transit info 20 | const ( 21 | HTTPDestService = "destination-service" 22 | HTTPDestAddr = "destination-addr" 23 | HTTPSourceService = "source-service" 24 | 25 | // supply metadata 26 | HTTPDestMethod = "destination-method" 27 | HTTPSourceMethod = "source-method" 28 | 29 | HTTPStreamLogID = "stream-log-id" 30 | ) 31 | -------------------------------------------------------------------------------- /pkg/retry/percentage_limit.go: -------------------------------------------------------------------------------- 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 retry 18 | 19 | import ( 20 | "github.com/bytedance/gopkg/cloud/circuitbreaker" 21 | ) 22 | 23 | // treat retry as 'error' for limiting the percentage of retry requests. 24 | // callTimes == 1 means it's the first request, not a retry. 25 | func recordRetryStat(cbKey string, panel circuitbreaker.Panel, callTimes int32) { 26 | if callTimes > 1 { 27 | panel.Fail(cbKey) 28 | } else { 29 | panel.Succeed(cbKey) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /pkg/rpcinfo/basicinfo.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 rpcinfo 18 | 19 | // EndpointBasicInfo should be immutable after created. 20 | type EndpointBasicInfo struct { 21 | ServiceName string 22 | Method string 23 | Tags map[string]string 24 | } 25 | 26 | // Tag names in EndpointInfo. Notice: These keys just be used for framework. 27 | const ( 28 | ConnResetTag = "crrst" 29 | RetryTag = "retry" 30 | RetryLastCostTag = "last_cost" 31 | RetryPrevInstTag = "prev_inst" 32 | ShmIPCTag = "shmipc" 33 | RemoteClosedTag = "remote_closed" 34 | ) 35 | 36 | // client HTTP 37 | const ( 38 | // connection full url 39 | HTTPURL = "http_url" 40 | // specify host header 41 | HTTPHost = "http_host" 42 | // http header for remote message tag 43 | HTTPHeader = "http_header" 44 | ) 45 | -------------------------------------------------------------------------------- /pkg/rpcinfo/rpcinfo_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 rpcinfo 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestEnablePool(t *testing.T) { 26 | t.Run("disable", func(t *testing.T) { 27 | EnablePool(false) 28 | test.Assert(t, !PoolEnabled()) 29 | }) 30 | 31 | t.Run("disable-enable", func(t *testing.T) { 32 | EnablePool(false) 33 | EnablePool(true) 34 | test.Assert(t, PoolEnabled()) 35 | }) 36 | } 37 | -------------------------------------------------------------------------------- /pkg/stats/event_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 stats 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestDefineNewEvent(t *testing.T) { 26 | num0 := MaxEventNum() 27 | 28 | event1, err1 := DefineNewEvent("myevent", LevelDetailed) 29 | num1 := MaxEventNum() 30 | 31 | test.Assert(t, err1 == nil) 32 | test.Assert(t, event1 != nil) 33 | test.Assert(t, num1 == num0+1) 34 | test.Assert(t, event1.Level() == LevelDetailed) 35 | 36 | event2, err2 := DefineNewEvent("myevent", LevelBase) 37 | num2 := MaxEventNum() 38 | test.Assert(t, err2 == ErrDuplicated) 39 | test.Assert(t, event2 == event1) 40 | test.Assert(t, num2 == num1) 41 | test.Assert(t, event1.Level() == LevelDetailed) 42 | 43 | FinishInitialization() 44 | 45 | event3, err3 := DefineNewEvent("another", LevelDetailed) 46 | num3 := MaxEventNum() 47 | test.Assert(t, err3 == ErrNotAllowed) 48 | test.Assert(t, event3 == nil) 49 | test.Assert(t, num3 == num1) 50 | } 51 | -------------------------------------------------------------------------------- /pkg/stats/status.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 stats 18 | 19 | // Status is used to indicate the status of an event. 20 | type Status int8 21 | 22 | // Predefined status. 23 | const ( 24 | StatusInfo Status = 1 25 | StatusWarn Status = 2 26 | StatusError Status = 3 27 | ) 28 | -------------------------------------------------------------------------------- /pkg/stats/tracer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 stats 18 | 19 | import ( 20 | "context" 21 | ) 22 | 23 | // Tracer is executed at the start and finish of an RPC. 24 | type Tracer interface { 25 | Start(ctx context.Context) context.Context 26 | Finish(ctx context.Context) 27 | } 28 | -------------------------------------------------------------------------------- /pkg/streaming/context.go: -------------------------------------------------------------------------------- 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 streaming 18 | 19 | import ( 20 | "context" 21 | ) 22 | 23 | type ( 24 | streamKey struct{} 25 | ) 26 | 27 | // Deprecated. 28 | func NewCtxWithStream(ctx context.Context, stream Stream) context.Context { 29 | return context.WithValue(ctx, streamKey{}, stream) 30 | } 31 | 32 | // Deprecated. 33 | func GetStream(ctx context.Context) Stream { 34 | if s, ok := ctx.Value(streamKey{}).(Stream); ok { 35 | return s 36 | } 37 | return nil 38 | } 39 | -------------------------------------------------------------------------------- /pkg/utils/byte2str.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "reflect" 21 | "unsafe" 22 | ) 23 | 24 | // SliceByteToString converts []byte to string without copy. 25 | // DO NOT USE unless you know what you're doing. 26 | func SliceByteToString(b []byte) string { 27 | return *(*string)(unsafe.Pointer(&b)) 28 | } 29 | 30 | // StringToSliceByte converts string to []byte without copy. 31 | // DO NOT USE unless you know what you're doing. 32 | func StringToSliceByte(s string) []byte { 33 | p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data) 34 | var b []byte 35 | hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b)) 36 | hdr.Data = uintptr(p) 37 | hdr.Cap = len(s) 38 | hdr.Len = len(s) 39 | return b 40 | } 41 | -------------------------------------------------------------------------------- /pkg/utils/byte2str_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestSliceByteToString(t *testing.T) { 26 | s1 := "012345" 27 | bs := []byte(s1) 28 | s2 := SliceByteToString(bs) 29 | test.Assert(t, s2 == s1) 30 | } 31 | 32 | func TestStringToSliceByte(t *testing.T) { 33 | s1 := "012345" 34 | b1 := []byte(s1) 35 | b2 := StringToSliceByte(s1) 36 | 37 | test.Assert(t, len(b1) == len(b2)) 38 | test.Assert(t, len(s1) >= cap(b2)) 39 | test.Assert(t, string(b2) == s1) 40 | } 41 | -------------------------------------------------------------------------------- /pkg/utils/contextmap/contextmap.go: -------------------------------------------------------------------------------- 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 contextmap 18 | 19 | import ( 20 | "context" 21 | "sync" 22 | ) 23 | 24 | type contextMapKey struct{} 25 | 26 | // WithContextMap returns a new context that carries a sync.Map 27 | // It's useful if you want to share a sync.Map between middlewares, especially for 28 | // StreamMiddleware and RecvMiddleware/SendMiddleware, since in recv/send middlewares, 29 | // we can only get the stream.Context() which is a fixed node in the context tree. 30 | // 31 | // Note: it's not added to context by default, and you should add it yourself if needed. 32 | func WithContextMap(ctx context.Context) context.Context { 33 | return context.WithValue(ctx, contextMapKey{}, &sync.Map{}) 34 | } 35 | 36 | // GetContextMap returns the sync.Map in the given context 37 | func GetContextMap(ctx context.Context) (m *sync.Map, ok bool) { 38 | if ctx != nil { 39 | m, ok = ctx.Value(contextMapKey{}).(*sync.Map) 40 | } 41 | return 42 | } 43 | -------------------------------------------------------------------------------- /pkg/utils/contextmap/contextmap_test.go: -------------------------------------------------------------------------------- 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 contextmap 18 | 19 | import ( 20 | "context" 21 | "testing" 22 | ) 23 | 24 | func TestContextMap(t *testing.T) { 25 | t.Run("nil context", func(t *testing.T) { 26 | //lint:ignore SA1012 This is the test to make the function more robust 27 | _, ok := GetContextMap(nil) 28 | if ok { 29 | t.Fatal("GetContextMap failed") 30 | } 31 | }) 32 | 33 | t.Run("normal context", func(t *testing.T) { 34 | ctx := WithContextMap(context.Background()) 35 | m, ok := GetContextMap(ctx) 36 | if !ok { 37 | t.Fatal("GetContextMap failed") 38 | } 39 | m.Store("key", "value") 40 | 41 | m1, ok := GetContextMap(ctx) 42 | if !ok { 43 | t.Fatal("GetContextMap failed") 44 | } 45 | v, ok := m1.Load("key") 46 | if !ok || v.(string) != "value" { 47 | t.Fatal("Load failed") 48 | } 49 | }) 50 | } 51 | -------------------------------------------------------------------------------- /pkg/utils/counter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import "sync/atomic" 20 | 21 | // AtomicInt is a shortcut for atomic.int32. 22 | type AtomicInt int32 23 | 24 | // Inc adds 1 to the int. 25 | func (i *AtomicInt) Inc() { 26 | atomic.AddInt32((*int32)(i), 1) 27 | } 28 | 29 | // Dec subs 1 to the int. 30 | func (i *AtomicInt) Dec() { 31 | atomic.AddInt32((*int32)(i), -1) 32 | } 33 | 34 | // Value returns the int. 35 | func (i *AtomicInt) Value() int { 36 | return int(atomic.LoadInt32((*int32)(i))) 37 | } 38 | -------------------------------------------------------------------------------- /pkg/utils/err_chain_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "errors" 21 | "testing" 22 | 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | func TestErrChain(t *testing.T) { 27 | var ec ErrChain 28 | test.Assert(t, !ec.HasError()) 29 | test.Assert(t, ec.Error() == "") 30 | 31 | ec.Append(errors.New("err1")) 32 | test.Assert(t, ec.HasError()) 33 | test.Assert(t, ec.Error() == "err1") 34 | 35 | ec.Append(errors.New("err2")) 36 | test.Assert(t, ec.HasError()) 37 | test.Assert(t, ec.Error() == "err1 | err2") 38 | 39 | ec.UseSeparator("+") 40 | test.Assert(t, ec.HasError()) 41 | test.Assert(t, ec.Error() == "err1+err2") 42 | 43 | var ec2 ErrChain 44 | test.Assert(t, !ec2.HasError()) 45 | test.Assert(t, ec2.Error() == "") 46 | ec2.Append(errors.New("err1")) 47 | ec2.Append(errors.New("err2")) 48 | test.Assert(t, ec2.Error() == "err1 | err2") 49 | } 50 | -------------------------------------------------------------------------------- /pkg/utils/fastthrift/fastthrift.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 fastthrift 18 | 19 | import ( 20 | "github.com/cloudwego/gopkg/protocol/thrift" 21 | ) 22 | 23 | // FastMarshal marshals the msg to buf. The msg should be generated by Kitex tool and implement ThriftFastCodec. 24 | // Deprecated: use github.com/cloudwego/gopkg/protocol/thrift.FastMarshal 25 | func FastMarshal(msg thrift.FastCodec) []byte { 26 | return thrift.FastMarshal(msg) 27 | } 28 | 29 | // FastUnmarshal unmarshal the buf into msg. The msg should be generated by Kitex tool and implement ThriftFastCodec. 30 | // Deprecated: use github.com/cloudwego/gopkg/protocol/thrift.FastUnmarshal 31 | func FastUnmarshal(buf []byte, msg thrift.FastCodec) error { 32 | return thrift.FastUnmarshal(buf, msg) 33 | } 34 | -------------------------------------------------------------------------------- /pkg/utils/fastthrift/fastthrift_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 fastthrift 18 | 19 | import ( 20 | "testing" 21 | 22 | mocks "github.com/cloudwego/kitex/internal/mocks/thrift" 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | var ( 27 | msgSize = 1024 28 | keyNum = 100 29 | ) 30 | 31 | func newRequest() *mocks.MockReq { 32 | var strList []string 33 | for i := 0; i < keyNum; i++ { 34 | msg := make([]byte, msgSize) 35 | strList = append(strList, string(msg)) 36 | } 37 | 38 | return &mocks.MockReq{ 39 | StrList: strList, 40 | } 41 | } 42 | 43 | func TestFastThrift(t *testing.T) { 44 | req1, req2 := newRequest(), &mocks.MockReq{} 45 | buf := FastMarshal(req1) 46 | err := FastUnmarshal(buf, req2) 47 | test.Assert(t, err == nil) 48 | test.Assert(t, req1.Msg == req2.Msg) 49 | test.Assert(t, len(req1.StrList) == len(req2.StrList)) 50 | } 51 | -------------------------------------------------------------------------------- /pkg/utils/func.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "reflect" 21 | "runtime" 22 | ) 23 | 24 | // GetFuncName returns the function name of i. 25 | func GetFuncName(i interface{}) string { 26 | return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() 27 | } 28 | -------------------------------------------------------------------------------- /pkg/utils/func_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "reflect" 21 | "testing" 22 | 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | func dump() { /* just an empty function */ } 27 | 28 | func TestGetFuncName(t *testing.T) { 29 | mod := "github.com/cloudwego/kitex/pkg/utils" 30 | nested := func() { /* just an empty function */ } 31 | test.Assert(t, GetFuncName(TestGetFuncName) == mod+".TestGetFuncName") 32 | test.Assert(t, GetFuncName(nested) == mod+".TestGetFuncName.func1") 33 | test.Assert(t, GetFuncName(dump) == mod+".dump") 34 | test.PanicAt(t, func() { 35 | GetFuncName(0) 36 | }, func(err interface{}) (ok bool) { 37 | _, ok = err.(*reflect.ValueError) 38 | return 39 | }) 40 | } 41 | -------------------------------------------------------------------------------- /pkg/utils/int_len.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | // GetUIntLen returns the length of the string representation of the given value n. 20 | func GetUIntLen(n uint64) int { 21 | cnt := 1 22 | for n >= 10 { 23 | cnt++ 24 | n /= 10 25 | } 26 | return cnt 27 | } 28 | -------------------------------------------------------------------------------- /pkg/utils/int_len_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import "testing" 20 | 21 | func TestGetUIntLen(t *testing.T) { 22 | type args struct { 23 | n uint64 24 | } 25 | tests := []struct { 26 | name string 27 | args args 28 | want int 29 | }{ 30 | { 31 | name: "0", 32 | args: args{0}, 33 | want: 1, 34 | }, 35 | { 36 | name: "5", 37 | args: args{5}, 38 | want: 1, 39 | }, 40 | { 41 | name: "10", 42 | args: args{10}, 43 | want: 2, 44 | }, 45 | { 46 | name: "56", 47 | args: args{56}, 48 | want: 2, 49 | }, 50 | { 51 | name: "102", 52 | args: args{102}, 53 | want: 3, 54 | }, 55 | { 56 | name: "99999", 57 | args: args{99999}, 58 | want: 5, 59 | }, 60 | } 61 | for _, tt := range tests { 62 | t.Run(tt.name, func(t *testing.T) { 63 | if got := GetUIntLen(tt.args.n); got != tt.want { 64 | t.Errorf("GetIntLen() = %v, want %v", got, tt.want) 65 | } 66 | }) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /pkg/utils/interface.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | // KitexArgs is used for assert when get real request from XXXArgs. 20 | // Thrift and KitexProtobuf will generate GetFirstArgument() interface{} for XXXArgs 21 | type KitexArgs interface { 22 | GetFirstArgument() interface{} 23 | } 24 | 25 | // KitexResult is used for assert when get real response from XXXResult. 26 | // Thrift and KitexProtobuf will generate the two functions for XXXResult. 27 | type KitexResult interface { 28 | GetResult() interface{} 29 | SetSuccess(interface{}) 30 | } 31 | -------------------------------------------------------------------------------- /pkg/utils/max_counter.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import "sync/atomic" 20 | 21 | // MaxCounter is an integer counter with upper limit. 22 | type MaxCounter struct { 23 | now int64 24 | max int 25 | } 26 | 27 | // NewMaxCounter returns a new MaxCounter. 28 | func NewMaxCounter(max int) *MaxCounter { 29 | return &MaxCounter{ 30 | max: max, 31 | } 32 | } 33 | 34 | // Inc increases the counter by one and returns if the operation succeeds. 35 | func (cl *MaxCounter) Inc() bool { 36 | if atomic.AddInt64(&cl.now, 1) > int64(cl.max) { 37 | atomic.AddInt64(&cl.now, -1) 38 | return false 39 | } 40 | return true 41 | } 42 | 43 | // Dec decreases the counter by one. 44 | func (cl *MaxCounter) Dec() { 45 | atomic.AddInt64(&cl.now, -1) 46 | } 47 | 48 | // DecN decreases the counter by n. 49 | func (cl *MaxCounter) DecN(n int64) { 50 | atomic.AddInt64(&cl.now, -n) 51 | } 52 | 53 | func (cl *MaxCounter) Now() int64 { 54 | return atomic.LoadInt64(&cl.now) 55 | } 56 | -------------------------------------------------------------------------------- /pkg/utils/netaddr.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import "net" 20 | 21 | var _ net.Addr = &NetAddr{} 22 | 23 | // NetAddr implements the net.Addr interface. 24 | type NetAddr struct { 25 | network string 26 | address string 27 | } 28 | 29 | // NewNetAddr creates a new NetAddr object with the network and address provided. 30 | func NewNetAddr(network, address string) net.Addr { 31 | return &NetAddr{network, address} 32 | } 33 | 34 | // Network implements the net.Addr interface. 35 | func (na *NetAddr) Network() string { 36 | return na.network 37 | } 38 | 39 | // String implements the net.Addr interface. 40 | func (na *NetAddr) String() string { 41 | return na.address 42 | } 43 | -------------------------------------------------------------------------------- /pkg/utils/netaddr_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "net" 21 | "testing" 22 | 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | func TestNetAddr(t *testing.T) { 27 | var n net.Addr = NewNetAddr("tcp", "12345") 28 | test.Assert(t, n.Network() == "tcp") 29 | test.Assert(t, n.String() == "12345") 30 | } 31 | -------------------------------------------------------------------------------- /pkg/utils/rpcstats.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "time" 21 | 22 | "github.com/cloudwego/kitex/pkg/rpcinfo" 23 | "github.com/cloudwego/kitex/pkg/stats" 24 | ) 25 | 26 | // CalculateEventCost get events from rpcstats, and calculates the time duration of (end - start). 27 | // It returns 0 when get nil rpcinfo event of either stats event. 28 | func CalculateEventCost(rpcstats rpcinfo.RPCStats, start, end stats.Event) time.Duration { 29 | endEvent := rpcstats.GetEvent(end) 30 | startEvent := rpcstats.GetEvent(start) 31 | if startEvent == nil || endEvent == nil { 32 | return 0 33 | } 34 | return endEvent.Time().Sub(startEvent.Time()) 35 | } 36 | -------------------------------------------------------------------------------- /pkg/utils/runtimex.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 utils 18 | 19 | import ( 20 | "github.com/bytedance/gopkg/lang/fastrand" 21 | "github.com/cloudwego/runtimex" 22 | ) 23 | 24 | // getGoroutineID return current G's ID, it will fall back to get a random int, 25 | // the caller should make sure it's ok if it cannot get correct goroutine id. 26 | func getGoroutineID() int { 27 | gid, err := runtimex.GID() 28 | if err == nil { 29 | return gid 30 | } 31 | // We use a Unit Test to ensure kitex should compatible with new Go versions, so it will have a rare choice to meet the rand fallback 32 | gid = fastrand.Int() 33 | if gid < 0 { 34 | gid = -gid 35 | } 36 | return gid 37 | } 38 | -------------------------------------------------------------------------------- /pkg/utils/runtimex_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 utils 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/runtimex" 23 | ) 24 | 25 | func TestRuntimeXCompatibility(t *testing.T) { 26 | _, err := runtimex.GID() 27 | if err != nil { 28 | t.Fatal("Your runtimex package is not compatible with current Go runtime version !!!!!!!!!") 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /pkg/utils/slice.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | // Slice is an abstraction of []interface{}. 20 | type Slice []interface{} 21 | 22 | // Push pushes an interface to the slice. 23 | func (s *Slice) Push(any interface{}) { 24 | *s = append(*s, any) 25 | } 26 | 27 | // Pop pops the last element from the slice. 28 | func (s *Slice) Pop() (res interface{}) { 29 | if size := len(*s); size > 0 { 30 | res = (*s)[size-1] 31 | *s = (*s)[:size-1] 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/utils/slice_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestSlice_Push(t *testing.T) { 26 | s := Slice{} 27 | s.Push("test") 28 | test.Assert(t, len(s) == 1) 29 | test.Assert(t, s[0].(string) == "test") 30 | 31 | s.Push(123) 32 | test.Assert(t, len(s) == 2) 33 | test.Assert(t, s[1].(int) == 123) 34 | 35 | s.Push(nil) 36 | test.Assert(t, len(s) == 3) 37 | test.Assert(t, s[2] == nil) 38 | } 39 | 40 | func TestSlice_Pop(t *testing.T) { 41 | s := Slice{} 42 | s.Push("test") 43 | s.Push(123) 44 | s.Push(nil) 45 | 46 | test.Assert(t, s.Pop() == nil) 47 | test.Assert(t, s.Pop().(int) == 123) 48 | test.Assert(t, s.Pop().(string) == "test") 49 | } 50 | -------------------------------------------------------------------------------- /pkg/utils/strbuf.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 | //lint:file-ignore SA6002 allocations cannot be avoided 18 | 19 | package utils 20 | 21 | import ( 22 | "strconv" 23 | "strings" 24 | "sync" 25 | ) 26 | 27 | var intBytesPool = sync.Pool{ 28 | New: func() interface{} { 29 | return make([]byte, 0, 20) 30 | }, 31 | } 32 | 33 | // WriteInt64ToStringBuilder writes a int64 to a strings.Builder. 34 | func WriteInt64ToStringBuilder(sb *strings.Builder, value int64) { 35 | bs := intBytesPool.Get().([]byte) 36 | bs = strconv.AppendInt(bs, value, 10) 37 | sb.Write(bs) 38 | intBytesPool.Put(bs[:0]) 39 | } 40 | -------------------------------------------------------------------------------- /pkg/utils/strbuf_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 utils 18 | 19 | import ( 20 | "fmt" 21 | "math/rand" 22 | "strings" 23 | "testing" 24 | 25 | "github.com/cloudwego/kitex/internal/test" 26 | ) 27 | 28 | func TestWriteInt64ToStringBuilder(t *testing.T) { 29 | var b1, b2 strings.Builder 30 | r := rand.Int63() 31 | WriteInt64ToStringBuilder(&b1, r) 32 | b2.WriteString(fmt.Sprint(r)) 33 | test.Assert(t, b1.String() == b2.String()) 34 | } 35 | -------------------------------------------------------------------------------- /pkg/xds/xds.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 xds 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/discovery" 21 | "github.com/cloudwego/kitex/pkg/endpoint" 22 | ) 23 | 24 | // ClientSuite includes all extensions used by the xDS-enabled client. 25 | type ClientSuite struct { 26 | RouterMiddleware endpoint.Middleware 27 | Resolver discovery.Resolver 28 | } 29 | 30 | // CheckClientSuite checks if the client suite is valid. 31 | // RouteMiddleware and Resolver should not be nil. 32 | func CheckClientSuite(cs ClientSuite) bool { 33 | return cs.RouterMiddleware != nil && cs.Resolver != nil 34 | } 35 | -------------------------------------------------------------------------------- /server/deprecated.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 server 18 | 19 | import ( 20 | internal_server "github.com/cloudwego/kitex/internal/server" 21 | "github.com/cloudwego/kitex/pkg/utils" 22 | ) 23 | 24 | // Deprecated: Useless option now, server support multi protocols by default. 25 | // IMPORTANT: this option will be deleted in the future!!! 26 | func WithMultiProtocol() Option { 27 | return Option{F: func(o *internal_server.Options, di *utils.Slice) { 28 | di.Push("WithMultiProtocol()") 29 | // no nothing 30 | }} 31 | } 32 | -------------------------------------------------------------------------------- /server/hooks.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 server 18 | 19 | import "sync" 20 | 21 | // RegisterStartHook add hook which is executed after the server starts. 22 | func RegisterStartHook(h func()) { 23 | muStartHooks.Lock() 24 | defer muStartHooks.Unlock() 25 | onServerStart.add(h) 26 | } 27 | 28 | // RegisterShutdownHook add hook which is executed after the server shutdown. 29 | func RegisterShutdownHook(h func()) { 30 | muShutdownHooks.Lock() 31 | defer muShutdownHooks.Unlock() 32 | onShutdown.add(h) 33 | } 34 | 35 | // Hooks is a collection of func. 36 | type Hooks []func() 37 | 38 | // Add adds a hook. 39 | func (h *Hooks) add(g func()) { 40 | *h = append(*h, g) 41 | } 42 | 43 | // Server hooks 44 | var ( 45 | onServerStart Hooks // Hooks executes after the server starts. 46 | muStartHooks sync.Mutex // onServerStart 's mutex 47 | onShutdown Hooks // Hooks executes when the server is shutdown gracefully. 48 | muShutdownHooks sync.Mutex // onShutdown 's mutex 49 | ) 50 | -------------------------------------------------------------------------------- /server/invoke/message.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 invoke 18 | 19 | import ( 20 | "net" 21 | 22 | "github.com/cloudwego/kitex/pkg/remote/trans/invoke" 23 | ) 24 | 25 | // Message indicates invoke message. 26 | type Message = invoke.Message 27 | 28 | // NewMessage creates new invoke message. 29 | func NewMessage(local, remote net.Addr) Message { 30 | return invoke.NewMessage(local, remote) 31 | } 32 | -------------------------------------------------------------------------------- /server/invoke/message_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 invoke 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | "github.com/cloudwego/kitex/pkg/utils" 24 | ) 25 | 26 | // TestNewMessage tests the NewMessage function. 27 | func TestNewMessage(t *testing.T) { 28 | lAddr := utils.NewNetAddr("tcp", "lAddr") 29 | rAddr := utils.NewNetAddr("tcp", "rAddr") 30 | msg := NewMessage(lAddr, rAddr) 31 | test.Assert(t, msg.LocalAddr().String() == lAddr.String()) 32 | test.Assert(t, msg.RemoteAddr().String() == rAddr.String()) 33 | } 34 | -------------------------------------------------------------------------------- /server/middlewares.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 server 18 | 19 | import ( 20 | "context" 21 | 22 | "github.com/cloudwego/kitex/pkg/endpoint" 23 | "github.com/cloudwego/kitex/pkg/rpcinfo" 24 | ) 25 | 26 | func serverTimeoutMW(next endpoint.Endpoint) endpoint.Endpoint { 27 | return func(ctx context.Context, request, response interface{}) (err error) { 28 | // Regardless of the underlying protocol, only by checking the RPCTimeout 29 | // For TTHeader, it will be set by transmeta.ServerTTHeaderHandler (not added by default though) 30 | // For GRPC/HTTP2, the timeout deadline is already set in the context, so no need to check it 31 | ri := rpcinfo.GetRPCInfo(ctx) 32 | timeout := ri.Config().RPCTimeout() 33 | if timeout <= 0 { 34 | return next(ctx, request, response) 35 | } 36 | 37 | ctx, cancel := context.WithTimeout(ctx, timeout) 38 | defer func() { 39 | if err != nil { 40 | cancel() 41 | } 42 | }() 43 | return next(ctx, request, response) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /server/mocks_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 server 18 | 19 | import ( 20 | "github.com/cloudwego/kitex/pkg/registry" 21 | ) 22 | 23 | var _ registry.Registry = &MockRegistry{} 24 | 25 | // MockRegistry is the mock implementation of registry.Registry interface. 26 | type MockRegistry struct { 27 | RegisterFunc func(info *registry.Info) error 28 | DeregisterFunc func(info *registry.Info) error 29 | } 30 | 31 | // Register is the mock implementation of registry.Registry interface. 32 | func (m MockRegistry) Register(info *registry.Info) error { 33 | if m.RegisterFunc != nil { 34 | return m.RegisterFunc(info) 35 | } 36 | return nil 37 | } 38 | 39 | // Deregister is the mock implementation of registry.Registry interface. 40 | func (m MockRegistry) Deregister(info *registry.Info) error { 41 | if m.DeregisterFunc != nil { 42 | return m.DeregisterFunc(info) 43 | } 44 | return nil 45 | } 46 | -------------------------------------------------------------------------------- /server/register_option_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 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 server 18 | 19 | import ( 20 | "testing" 21 | 22 | internal_server "github.com/cloudwego/kitex/internal/server" 23 | "github.com/cloudwego/kitex/internal/test" 24 | ) 25 | 26 | func TestWithFallbackService(t *testing.T) { 27 | opts := []RegisterOption{WithFallbackService()} 28 | registerOpts := internal_server.NewRegisterOptions(opts) 29 | test.Assert(t, registerOpts.IsFallbackService) 30 | } 31 | -------------------------------------------------------------------------------- /server/stream_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 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 server 18 | 19 | import ( 20 | "context" 21 | "testing" 22 | 23 | internal_server "github.com/cloudwego/kitex/internal/server" 24 | "github.com/cloudwego/kitex/internal/test" 25 | "github.com/cloudwego/kitex/pkg/rpcinfo" 26 | "github.com/cloudwego/kitex/pkg/stats" 27 | ) 28 | 29 | type mockTracer struct { 30 | stats.Tracer 31 | rpcinfo.StreamEventReporter 32 | } 33 | 34 | func Test_server_initStreamMiddlewares(t *testing.T) { 35 | ctl := &rpcinfo.TraceController{} 36 | ctl.Append(mockTracer{}) 37 | s := &server{ 38 | opt: &internal_server.Options{ 39 | TracerCtl: ctl, 40 | }, 41 | svcs: &services{}, 42 | } 43 | s.buildMiddlewares(context.Background()) 44 | 45 | test.Assert(t, len(s.opt.Streaming.RecvMiddlewares) == 0, "init middlewares failed") 46 | test.Assert(t, len(s.opt.Streaming.SendMiddlewares) == 0, "init middlewares failed") 47 | } 48 | -------------------------------------------------------------------------------- /tool/cmd/kitex/args/deprecated.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 args 18 | 19 | import "errors" 20 | 21 | type deprecatedFlag struct{} 22 | 23 | var errFlagDeprecated = errors.New(`flag is deprecated`) 24 | 25 | func (deprecatedFlag) Set(_ string) error { 26 | return errFlagDeprecated 27 | } 28 | 29 | func (deprecatedFlag) String() string { return "" } 30 | -------------------------------------------------------------------------------- /tool/cmd/kitex/args/deprecated_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 args 18 | 19 | import ( 20 | "flag" 21 | "io" 22 | "strings" 23 | "testing" 24 | 25 | "github.com/cloudwego/kitex/internal/test" 26 | ) 27 | 28 | func TestDeprecatedFlag(t *testing.T) { 29 | f := flag.NewFlagSet("TestDeprecatedFlag", flag.ContinueOnError) 30 | 31 | // ignore print 32 | f.Usage = func() {} 33 | f.SetOutput(io.Discard) 34 | 35 | p := &deprecatedFlag{} 36 | test.Assert(t, p.String() == "") 37 | 38 | f.Var(p, "test", "") 39 | err := f.Parse([]string{"-test", "hello"}) 40 | test.Assert(t, err != nil) 41 | test.Assert(t, strings.Contains(err.Error(), errFlagDeprecated.Error()), err) 42 | } 43 | -------------------------------------------------------------------------------- /tool/internal_pkg/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package internalpkg contains packages for building the kitex command line tool. 16 | // APIs exported by packages under this directory do not promise any backward 17 | // compatibility, so please do not rely on them. 18 | package internalpkg 19 | -------------------------------------------------------------------------------- /tool/internal_pkg/generator/feature.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package generator 16 | 17 | type feature int 18 | 19 | const ( 20 | placeHolder feature = iota 21 | ) 22 | 23 | var ( 24 | featureMap = make(map[string]feature) 25 | maxFeature = placeHolder 26 | ) 27 | 28 | // HasFeature check whether a feature in the list 29 | func HasFeature(list []feature, key string) bool { 30 | target, ok := getFeature(key) 31 | if !ok { 32 | return false 33 | } 34 | for _, f := range list { 35 | if f == target { 36 | return true 37 | } 38 | } 39 | return false 40 | } 41 | 42 | // RegisterFeature register a feature 43 | func RegisterFeature(key string) { 44 | if _, ok := featureMap[key]; !ok { 45 | maxFeature++ 46 | featureMap[key] = maxFeature 47 | } 48 | } 49 | 50 | func getFeature(key string) (feature, bool) { 51 | f, ok := featureMap[key] 52 | return f, ok 53 | } 54 | -------------------------------------------------------------------------------- /tool/internal_pkg/prutal/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 prutal 18 | -------------------------------------------------------------------------------- /tool/internal_pkg/tpl/bootstrap.sh.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tpl 16 | 17 | // BootstrapTpl is the template for generating bootstrap.sh. 18 | var BootstrapTpl string = `#! /usr/bin/env bash 19 | CURDIR=$(cd $(dirname $0); pwd) 20 | 21 | if [ "X$1" != "X" ]; then 22 | RUNTIME_ROOT=$1 23 | else 24 | RUNTIME_ROOT=${CURDIR} 25 | fi 26 | 27 | export KITEX_RUNTIME_ROOT=$RUNTIME_ROOT 28 | export KITEX_LOG_DIR="$RUNTIME_ROOT/log" 29 | 30 | if [ ! -d "$KITEX_LOG_DIR/app" ]; then 31 | mkdir -p "$KITEX_LOG_DIR/app" 32 | fi 33 | 34 | if [ ! -d "$KITEX_LOG_DIR/rpc" ]; then 35 | mkdir -p "$KITEX_LOG_DIR/rpc" 36 | fi 37 | 38 | exec "$CURDIR/bin/{{.RealServiceName}}" 39 | ` 40 | -------------------------------------------------------------------------------- /tool/internal_pkg/tpl/build.sh.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tpl 16 | 17 | // BuildTpl is the template for generating build.sh. 18 | var BuildTpl string = `#!/usr/bin/env bash 19 | RUN_NAME="{{.RealServiceName}}" 20 | 21 | mkdir -p output/bin 22 | cp script/* output/ 23 | chmod +x output/bootstrap.sh 24 | 25 | if [ "$IS_SYSTEM_TEST_ENV" != "1" ]; then 26 | go build -o output/bin/${RUN_NAME} 27 | else 28 | go test -c -covermode=set -o output/bin/${RUN_NAME} -coverpkg=./... 29 | fi 30 | ` 31 | -------------------------------------------------------------------------------- /tool/internal_pkg/tpl/handler.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tpl 16 | 17 | // HandlerTpl is the template for generating handler.go. 18 | var HandlerTpl string = `package main 19 | 20 | import ( 21 | {{- range $path, $aliases := .Imports}} 22 | {{- if not $aliases}} 23 | "{{$path}}" 24 | {{- else}} 25 | {{- range $alias, $is := $aliases}} 26 | {{$alias}} "{{$path}}" 27 | {{- end}} 28 | {{- end}} 29 | {{- end}} 30 | ) 31 | 32 | // {{.ServiceName}}Impl implements the last service interface defined in the IDL. 33 | type {{.ServiceName}}Impl struct{} 34 | {{template "HandlerMethod" .}} 35 | ` 36 | -------------------------------------------------------------------------------- /tool/internal_pkg/tpl/templates_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tpl 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/cloudwego/kitex/internal/test" 21 | ) 22 | 23 | func TestEmbedOK(t *testing.T) { 24 | test.Assert(t, len(BootstrapTpl) > 0) 25 | test.Assert(t, len(BuildTpl) > 0) 26 | test.Assert(t, len(ClientTpl) > 0) 27 | test.Assert(t, len(HandlerTpl) > 0) 28 | test.Assert(t, len(HandlerMethodsTpl) > 0) 29 | test.Assert(t, len(InvokerTpl) > 0) 30 | test.Assert(t, len(MainTpl) > 0) 31 | test.Assert(t, len(ServerTpl) > 0) 32 | test.Assert(t, len(ServiceTpl) > 0) 33 | test.Assert(t, len(ToolVersionTpl) > 0) 34 | } 35 | -------------------------------------------------------------------------------- /tool/internal_pkg/tpl/tool_version.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package tpl 16 | 17 | // ToolVersionTpl is the template for generating the .kitex source. 18 | var ToolVersionTpl string = `kitexinfo: 19 | ServiceName: '{{.RealServiceName}}' 20 | ToolVersion: '{{.Version}}' 21 | ` 22 | -------------------------------------------------------------------------------- /tool/internal_pkg/util/dump.go: -------------------------------------------------------------------------------- 1 | // Copyright 2023 CloudWeGo Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package util 16 | 17 | import ( 18 | "io" 19 | "os" 20 | ) 21 | 22 | // ReadInput defaults to read from stdin. 23 | // If `KITEX_TOOL_STDIN_LOAD_FILE` is a valid path, it reads from the specified file; 24 | // If `KITEX_TOOL_STDIN_DUMP_FILE` is a valid path, it dumps what it read to the file; 25 | // This feature makes it easier to debug kitex tool. 26 | func ReadInput() ([]byte, error) { 27 | if dumpFileName := os.Getenv("KITEX_TOOL_STDIN_LOAD_FILE"); dumpFileName != "" { 28 | return os.ReadFile(dumpFileName) 29 | } 30 | data, err := io.ReadAll(os.Stdin) 31 | if err == nil { 32 | if dumpFileName := os.Getenv("KITEX_TOOL_STDIN_DUMP_FILE"); dumpFileName != "" { 33 | os.WriteFile(dumpFileName, data, 0o644) 34 | } 35 | } 36 | return data, err 37 | } 38 | -------------------------------------------------------------------------------- /tool/internal_pkg/util/env/env.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 env 18 | 19 | import ( 20 | "os" 21 | "strconv" 22 | ) 23 | 24 | // UseProtoc returns true if we'd like to use protoc instead of prutal. 25 | // 26 | // protoc will be deprecated in the future, and this func will be removed. 27 | func UseProtoc() bool { 28 | v := os.Getenv("KITEX_TOOL_USE_PROTOC") 29 | if v == "" { 30 | return false // disable protoc by default 31 | } 32 | ok, _ := strconv.ParseBool(v) 33 | return ok 34 | } 35 | -------------------------------------------------------------------------------- /tool/internal_pkg/util/env/env_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2025 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 env 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/cloudwego/kitex/internal/test" 23 | ) 24 | 25 | func TestUseProtoc(t *testing.T) { 26 | // default: false 27 | use := UseProtoc() 28 | test.Assert(t, use == false, use) 29 | 30 | // set to 1 31 | t.Setenv("KITEX_TOOL_USE_PROTOC", "1") 32 | use = UseProtoc() 33 | test.Assert(t, use, use) 34 | 35 | // set to 0 36 | t.Setenv("KITEX_TOOL_USE_PROTOC", "0") 37 | use = UseProtoc() 38 | test.Assert(t, use == false, use) 39 | } 40 | -------------------------------------------------------------------------------- /version.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 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 kitex 18 | 19 | // Name and Version info of this framework, used for statistics and debug 20 | const ( 21 | Name = "Kitex" 22 | Version = "v0.13.1" 23 | ) 24 | --------------------------------------------------------------------------------