Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]feat: option loaders to generate kitex options #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/callopt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

type CalloptConfig struct {
HostPort string `yaml:"host_port"`
URL string `yaml:"url"`
HTTPHost string `yaml:"http_host"`
RPCTimeout *TimeInterval `yaml:"rpc_timeout"`
ConnectTimeout *TimeInterval `yaml:"connect_timeout"`
Tag *Tag `yaml:"tag"`
GRPCCompressor string `yaml:"grpc_compressor"`
}
38 changes: 38 additions & 0 deletions config/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package config

type ClientConfig struct {
// basic
TransportProtocol int `yaml:"transport_protocol"`
DestService string `yaml:"dest_service"`
HostPorts []string `yaml:"host_ports"`
LongConnection *IdleConfig `yaml:"long_connection"`
MuxConnection int `yaml:"mux_connection"`
RPCTimeout *TimeInterval `yaml:"rpc_timeout"`
ConnectTimeout *TimeInterval `yaml:"connect_timeout"`
Tag *Tag `yaml:"tag"`
StatsLevel int `yaml:"stats_level"`
ConnReporterEnabled bool `yaml:"conn_reporter_enabled"`
GRPCConnPoolSize uint32 `yaml:"grpc_conn_pool_size"`
GRPCWriteBufferSize uint32 `yaml:"grpc_write_buffer_size"`
GRPCReadBufferSize uint32 `yaml:"grpc_read_buffer_size"`
GRPCInitialWindowSize uint32 `yaml:"grpc_initial_window_size"`
GRPCInitialConnWindowSize uint32 `yaml:"grpc_initial_conn_window_size"`
GRPCMaxHeaderListSize uint32 `yaml:"grpc_max_header_list_size"`
GRPCKeepaliveParams *GRPCClientKeepaliveParams `yaml:"grpc_keepalive_params"`
// advanced
HTTPConnection bool `yaml:"http_connection"`
ClientBasicInfo *EndpointBasicInfo `yaml:"client_basic_info"`
}

type IdleConfig struct {
MinIdlePerAddress int `yaml:"min_idle_per_address"`
MaxIdlePerAddress int `yaml:"max_idle_per_address"`
MaxIdleGlobal int `yaml:"max_idle_global"`
MaxIdleTimeout TimeInterval `yaml:"max_idle_timeout"`
}

type GRPCClientKeepaliveParams struct {
Time TimeInterval `yaml:"time"`
Timeout TimeInterval `yaml:"timeout"`
PermitWithoutStream bool `yaml:"permit_without_stream"`
}
41 changes: 41 additions & 0 deletions config/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

import "time"

type EndpointBasicInfo struct {
ServiceName string `yaml:"service_name"`
Method string `yaml:"method"`
Tags map[string]string `yaml:"tags"`
}

// TimeInterval common time interval struct.
// Unit ns, us, ms, s, m, h.
// Value is the timeout value.
type TimeInterval struct {
Unit string `yaml:"unit"`
Value int `yaml:"value"`
}

func (t *TimeInterval) Transform() time.Duration {
var unit time.Duration
switch t.Unit {
case "ns":
unit = time.Nanosecond
case "us":
unit = time.Microsecond
case "ms":
unit = time.Millisecond
case "s":
unit = time.Second
case "m":
unit = time.Minute
case "h":
unit = time.Hour
}
return time.Duration(t.Value) * unit
}

type Tag struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}
53 changes: 53 additions & 0 deletions config/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package config

type ServerConfig struct {
// basic
MuxTransport bool `yaml:"mux_transport"` // For WithMuxTransport
ReadWriteTimeout *TimeInterval `yaml:"read_write_timeout"` // For WithReadWriteTimeout
ExitWaitTime *TimeInterval `yaml:"exit_wait_time"` // For WithExitWaitTime
MaxConnIdleTime *TimeInterval `yaml:"max_conn_idle_time"` // For WithMaxConnIdleTime
StatsLevel int `yaml:"stats_level"` // For WithStatsLevel
ServiceAddr *NetAddr `yaml:"service_addr"` // For WithServiceAddr
GRPCWriteBufferSize uint32 `yaml:"grpc_write_buffer_size"` // For WithGRPCWriteBufferSize
GRPCReadBufferSize uint32 `yaml:"grpc_read_buffer_size"` // For WithGRPCReadBufferSize
GRPCInitialWindowSize uint32 `yaml:"grpc_initial_window_size"` // For WithGRPCInitialWindowSize
GRPCInitialConnWindowSize uint32 `yaml:"grpc_initial_conn_window_size"` // For WithGRPCInitialConnWindowSize
GRPCKeepaliveParams *GRPCServerKeepaliveParams `yaml:"grpc_keepalive_params"` // For WithGRPCKeepaliveParams
GRPCKeepaliveEnforcementPolicy *GRPCServerKeepaliveEnforcementPolicy `yaml:"grpc_keepalive_enforcement_policy"` // For WithGRPCKeepaliveEnforcementPolicy
GRPCMaxConcurrentStreams uint32 `yaml:"grpc_max_concurrent_streams"` // For WithGRPCMaxConcurrentStreams
GRPCMaxHeaderListSize uint32 `yaml:"grpc_max_header_list_size"` // For WithGRPCMaxHeaderListSize
ContextBackup *ContextBackup `yaml:"context_backup"` // For WithContextBackup
RefuseTrafficWithoutServiceName bool `yaml:"refuse_traffic_without_service_name"` // For WithRefuseTrafficWithoutServiceName
EnableContextTimeout bool `yaml:"enable_context_timeout"` // For WithEnableContextTimeout
// advanced
ServerBasicInfo *EndpointBasicInfo `yaml:"server_basic_info"` // For WithServerBasicInfo
ReusePort bool `yaml:"reuse_port"` // For WithReusePort
// stream
CompatibleMiddlewareForUnary bool `yaml:"compatible_middleware_for_unary"` // For WithCompatibleMiddlewareForUnary
}

// NetAddr defines the network and address of the service.
// Network name of the network (for example, "tcp", "udp").
// Address string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80").
type NetAddr struct {
Network string `yaml:"network"`
Address string `yaml:"address"`
}

type GRPCServerKeepaliveParams struct {
MaxConnectionIdle TimeInterval `yaml:"max_connection_idle"`
MaxConnectionAge TimeInterval `yaml:"max_connection_age"`
MaxConnectionAgeGrace TimeInterval `yaml:"max_connection_age_grace"`
Time TimeInterval `yaml:"time"`
Timeout TimeInterval `yaml:"timeout"`
}

type GRPCServerKeepaliveEnforcementPolicy struct {
MinTime TimeInterval `yaml:"min_time"`
PermitWithoutStream bool `yaml:"permit_without_stream"`
}

type ContextBackup struct {
Enable bool `yaml:"enable"`
Async bool `yaml:"async"`
}
10 changes: 10 additions & 0 deletions config/streamcall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

type StreamCallConfig struct {
// from callopt
HostPort string `yaml:"host_port"`
URL string `yaml:"url"`
ConnectTimeout *TimeInterval `yaml:"connect_timeout"`
Tag *Tag `yaml:"tag"`
GRPCCompressor string `yaml:"grpc_compressor"`
}
20 changes: 20 additions & 0 deletions config/streamclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package config

type StreamClientConfig struct {
// from client
DestService string `yaml:"dest_service"`
HostPorts []string `yaml:"host_ports"`
ConnectTimeout *TimeInterval `yaml:"connect_timeout"`
Tag *Tag `yaml:"tag"`
StatsLevel int `yaml:"stats_level"`
ConnReporterEnabled bool `yaml:"conn_reporter_enabled"`
GRPCConnPoolSize uint32 `yaml:"grpc_conn_pool_size"`
GRPCWriteBufferSize uint32 `yaml:"grpc_write_buffer_size"`
GRPCReadBufferSize uint32 `yaml:"grpc_read_buffer_size"`
GRPCInitialWindowSize uint32 `yaml:"grpc_initial_window_size"`
GRPCInitialConnWindowSize uint32 `yaml:"grpc_initial_conn_window_size"`
GRPCMaxHeaderListSize uint32 `yaml:"grpc_max_header_list_size"`
GRPCKeepaliveParams *GRPCClientKeepaliveParams `yaml:"grpc_keepalive_params"`
// from client advanced
ClientBasicInfo *EndpointBasicInfo `yaml:"client_basic_info"`
}
28 changes: 28 additions & 0 deletions configloader/yaml/callopt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package yaml

import (
"fmt"
"github.com/kitex-contrib/optionloader/config"
"gopkg.in/yaml.v3"
"os"
)

func LoadCalloptConfig(filePath string) (*config.CalloptConfig, error) {

if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %s", filePath)
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

var cfg config.CalloptConfig

err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}
28 changes: 28 additions & 0 deletions configloader/yaml/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package yaml

import (
"fmt"
"github.com/kitex-contrib/optionloader/config"
"gopkg.in/yaml.v3"
"os"
)

func LoadClientConfig(filePath string) (*config.ClientConfig, error) {

if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %s", filePath)
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

var cfg config.ClientConfig

err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}
27 changes: 27 additions & 0 deletions configloader/yaml/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package yaml

import (
"fmt"
"github.com/kitex-contrib/optionloader/config"
"gopkg.in/yaml.v3"
"os"
)

func LoadServerConfig(filePath string) (*config.ServerConfig, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %s", filePath)
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

var cfg config.ServerConfig

err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}
28 changes: 28 additions & 0 deletions configloader/yaml/streamcall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package yaml

import (
"fmt"
"github.com/kitex-contrib/optionloader/config"
"gopkg.in/yaml.v3"
"os"
)

func LoadStreamCallConfig(filePath string) (*config.StreamCallConfig, error) {

if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %s", filePath)
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

var cfg config.StreamCallConfig

err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}
28 changes: 28 additions & 0 deletions configloader/yaml/streamclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package yaml

import (
"fmt"
"github.com/kitex-contrib/optionloader/config"
"gopkg.in/yaml.v3"
"os"
)

func LoadStreamClientConfig(filePath string) (*config.StreamClientConfig, error) {

if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %s", filePath)
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}

var cfg config.StreamClientConfig

err = yaml.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}

return &cfg, nil
}
13 changes: 13 additions & 0 deletions examples/yaml/client/callopt/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
host_port: "localhost:8080"
url: "/your-service-name/your-method"
http_host: "your-destination-service-name"
rpc_timeout:
unit: "s"
value: 5
connect_timeout:
unit: "s"
value: 3
tag:
key: "your-tag-key"
value: "your-tag-value"
grpc_compressor: "gzip"
21 changes: 21 additions & 0 deletions examples/yaml/client/callopt/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"github.com/kitex-contrib/optionloader/configloader/yaml"
"github.com/kitex-contrib/optionloader/optionloader/client/callopt"
)

func main() {
filePath := "./examples/yaml/client/callopt/config.yaml"
cfg, err := yaml.LoadCalloptConfig(filePath)
if err != nil {
panic(err)
}
loader := callopt.NewOptionLoader()
options, err := loader.Load(cfg)
if err != nil {
panic(err)
}
fmt.Println(len(options))
}
9 changes: 9 additions & 0 deletions examples/yaml/client/callopt/streamcall/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
host_port: "localhost:8080"
url: "/your-service-name/your-method"
connect_timeout:
unit: "s"
value: 3
tag:
key: "your-tag-key"
value: "your-tag-value"
grpc_compressor: "gzip"
21 changes: 21 additions & 0 deletions examples/yaml/client/callopt/streamcall/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"github.com/kitex-contrib/optionloader/configloader/yaml"
"github.com/kitex-contrib/optionloader/optionloader/client/callopt/streamcall"
)

func main() {
filePath := "./examples/yaml/client/callopt/streamcall/config.yaml"
cfg, err := yaml.LoadStreamCallConfig(filePath)
if err != nil {
panic(err)
}
loader := streamcall.NewOptionLoader()
options, err := loader.Load(cfg)
if err != nil {
panic(err)
}
fmt.Println(len(options))
}
Loading
Loading