kratos: github.com/go-kratos/kratos/pkg/net/rpc/warden Index | Examples | Files | Directories

package warden

import "github.com/go-kratos/kratos/pkg/net/rpc/warden"

Index

Examples

Package Files

client.go logging.go metrics.go recovery.go server.go stats.go validate.go

Constants

const (
    // disable all log.
    LogFlagDisable = 1 << iota
    // disable print args on log.
    LogFlagDisableArgs
    // disable info level log.
    LogFlagDisableInfo
)

Warden Log Flag

func NewConn Uses

func NewConn(target string, opt ...grpc.DialOption) (*grpc.ClientConn, error)

NewConn will create a grpc conn by default config.

func WithDialLogFlag Uses

func WithDialLogFlag(flag int8) grpc.DialOption

WithDialLogFlag set client level log behaviour.

func WithLogFlag Uses

func WithLogFlag(flag int8) grpc.CallOption

WithLogFlag disable client access log.

type Client Uses

type Client struct {
    // contains filtered or unexported fields
}

Client is the framework's client side instance, it contains the ctx, opt and interceptors. Create an instance of Client, by using NewClient().

Code:

client := warden.NewClient(&warden.ClientConfig{
    Dial:    xtime.Duration(time.Second * 10),
    Timeout: xtime.Duration(time.Second * 10),
    Breaker: &breaker.Config{
        Window:  xtime.Duration(3 * time.Second),
        Bucket:  10,
        K:       1.5,
        Request: 20,
    },
})
// apply client interceptor middleware
client.Use(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (ret error) {
    newctx, cancel := context.WithTimeout(ctx, time.Second*5)
    defer cancel()
    ret = invoker(newctx, method, req, reply, cc, opts...)
    return
})
conn, err := client.Dial(context.Background(), "127.0.0.1:8080")
if err != nil {
    log.Error("did not connect: %v", err)
    return
}
defer conn.Close()

c := pb.NewGreeterClient(conn)
name := "2233"
rp, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name, Age: 18})
if err != nil {
    log.Error("could not greet: %v", err)
    return
}
fmt.Println("rp", *rp)

func DefaultClient Uses

func DefaultClient() *Client

DefaultClient returns a new default Client instance with a default client interceptor and default dialoption. opt can be used to add grpc dial options.

func NewClient Uses

func NewClient(conf *ClientConfig, opt ...grpc.DialOption) *Client

NewClient returns a new blank Client instance with a default client interceptor. opt can be used to add grpc dial options.

func (*Client) Dial Uses

func (c *Client) Dial(ctx context.Context, target string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error)

Dial creates a client connection to the given target. Target format is scheme://authority/endpoint?query_arg=value example: discovery://default/account.account.service?cluster=shfy01&cluster=shfy02

func (*Client) DialTLS Uses

func (c *Client) DialTLS(ctx context.Context, target string, file string, name string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error)

DialTLS creates a client connection over tls transport to the given target.

func (*Client) SetConfig Uses

func (c *Client) SetConfig(conf *ClientConfig) (err error)

SetConfig hot reloads client config

func (*Client) Use Uses

func (c *Client) Use(handlers ...grpc.UnaryClientInterceptor) *Client

Use attachs a global inteceptor to the Client. For example, this is the right place for a circuit breaker or error management inteceptor.

func (*Client) UseOpt Uses

func (c *Client) UseOpt(opts ...grpc.DialOption) *Client

UseOpt attachs a global grpc DialOption to the Client.

type ClientConfig Uses

type ClientConfig struct {
    Dial                   xtime.Duration
    Timeout                xtime.Duration
    Breaker                *breaker.Config
    Method                 map[string]*ClientConfig
    Clusters               []string
    Zone                   string
    Subset                 int
    NonBlock               bool
    KeepAliveInterval      xtime.Duration
    KeepAliveTimeout       xtime.Duration
    KeepAliveWithoutStream bool
}

ClientConfig is rpc client conf.

type Server Uses

type Server struct {
    // contains filtered or unexported fields
}

Server is the framework's server side instance, it contains the GrpcServer, interceptor and interceptors. Create an instance of Server, by using NewServer().

Code:

s := warden.NewServer(&warden.ServerConfig{Timeout: xtime.Duration(time.Second), Addr: ":8080"})
// apply server interceptor middleware
s.Use(func(ctx context.Context, req interface{}, args *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    newctx, cancel := context.WithTimeout(ctx, time.Second*10)
    defer cancel()
    resp, err := handler(newctx, req)
    return resp, err
})
pb.RegisterGreeterServer(s.Server(), &helloServer{})
s.Start()

func NewServer Uses

func NewServer(conf *ServerConfig, opt ...grpc.ServerOption) (s *Server)

NewServer returns a new blank Server instance with a default server interceptor.

func (*Server) GetValidate Uses

func (s *Server) GetValidate() *validator.Validate

GetValidate return the default validate

func (*Server) RegisterValidation Uses

func (s *Server) RegisterValidation(key string, fn validator.Func) error

RegisterValidation adds a validation Func to a Validate's map of validators denoted by the key NOTE: if the key already exists, the previous validation function will be replaced. NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation

func (*Server) Run Uses

func (s *Server) Run(addr string) error

Run create a tcp listener and start goroutine for serving each incoming request. Run will return a non-nil error unless Stop or GracefulStop is called.

func (*Server) RunUnix Uses

func (s *Server) RunUnix(file string) error

RunUnix create a unix listener and start goroutine for serving each incoming request. RunUnix will return a non-nil error unless Stop or GracefulStop is called.

func (*Server) Serve Uses

func (s *Server) Serve(lis net.Listener) error

Serve accepts incoming connections on the listener lis, creating a new ServerTransport and service goroutine for each. Serve will return a non-nil error unless Stop or GracefulStop is called.

func (*Server) Server Uses

func (s *Server) Server() *grpc.Server

Server return the grpc server for registering service.

func (*Server) SetConfig Uses

func (s *Server) SetConfig(conf *ServerConfig) (err error)

SetConfig hot reloads server config

func (*Server) Shutdown Uses

func (s *Server) Shutdown(ctx context.Context) (err error)

Shutdown stops the server gracefully. It stops the server from accepting new connections and RPCs and blocks until all the pending RPCs are finished or the context deadline is reached.

func (*Server) Start Uses

func (s *Server) Start() (*Server, error)

Start create a new goroutine run server with configured listen addr will panic if any error happend return server itself

func (*Server) StartWithAddr Uses

func (s *Server) StartWithAddr() (*Server, net.Addr, error)

StartWithAddr create a new goroutine run server with configured listen addr will panic if any error happend return server itself and the actually listened address (if configured listen port is zero, the os will allocate an unused port)

func (*Server) Use Uses

func (s *Server) Use(handlers ...grpc.UnaryServerInterceptor) *Server

Use attachs a global inteceptor to the server. For example, this is the right place for a rate limiter or error management inteceptor.

type ServerConfig Uses

type ServerConfig struct {
    // Network is grpc listen network,default value is tcp
    Network string `dsn:"network"`
    // Addr is grpc listen addr,default value is 0.0.0.0:9000
    Addr string `dsn:"address"`
    // Timeout is context timeout for per rpc call.
    Timeout xtime.Duration `dsn:"query.timeout"`
    // IdleTimeout is a duration for the amount of time after which an idle connection would be closed by sending a GoAway.
    // Idleness duration is defined since the most recent time the number of outstanding RPCs became zero or the connection establishment.
    IdleTimeout xtime.Duration `dsn:"query.idleTimeout"`
    // MaxLifeTime is a duration for the maximum amount of time a connection may exist before it will be closed by sending a GoAway.
    // A random jitter of +/-10% will be added to MaxConnectionAge to spread out connection storms.
    MaxLifeTime xtime.Duration `dsn:"query.maxLife"`
    // ForceCloseWait is an additive period after MaxLifeTime after which the connection will be forcibly closed.
    ForceCloseWait xtime.Duration `dsn:"query.closeWait"`
    // KeepAliveInterval is after a duration of this time if the server doesn't see any activity it pings the client to see if the transport is still alive.
    KeepAliveInterval xtime.Duration `dsn:"query.keepaliveInterval"`
    // KeepAliveTimeout  is After having pinged for keepalive check, the server waits for a duration of Timeout and if no activity is seen even after that
    // the connection is closed.
    KeepAliveTimeout xtime.Duration `dsn:"query.keepaliveTimeout"`
    // LogFlag to control log behaviour. e.g. LogFlag: warden.LogFlagDisableLog.
    // Disable: 1 DisableArgs: 2 DisableInfo: 4
    LogFlag int8 `dsn:"query.logFlag"`
}

ServerConfig is rpc server conf.

type TimeoutCallOption Uses

type TimeoutCallOption struct {
    *grpc.EmptyCallOption
    Timeout time.Duration
}

TimeoutCallOption timeout option.

func WithTimeoutCallOption Uses

func WithTimeoutCallOption(timeout time.Duration) *TimeoutCallOption

WithTimeoutCallOption can override the timeout in ctx and the timeout in the configuration file

Directories

PathSynopsis
balancer/p2c
internal/encoding/json
internal/metadata
internal/proto/testprotoPackage testproto is a generated protocol buffer package.
internal/status
ratelimiter
resolver
resolver/direct

Package warden imports 42 packages (graph). Updated 2020-09-14. Refresh now. Tools for package owners.