micro

package module
v6.0.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

TP-Micro GitHub release report card github issues github closed issues GoDoc view examples view teleport view Go网络编程群

TP-Micro master(v6) is a simple, powerful micro service framework based on Teleport v6.

简体中文

tp-micro flow chart

Install

go version ≥ 1.12
go get -u -f -d github.com/xiaoenai/tp-micro/...
cd $GOPATH/src/github.com/xiaoenai/tp-micro/cmd/micro
go install

Feature

  • Support auto service-discovery
  • Supports custom service linker
  • Support load balancing
  • Support Asynchronous multiplexing IO
  • Support custom protocol
  • Support custom body codec
  • Support plug-in expansion
  • Support heartbeat mechanism
  • Detailed log information, support print input and output details
  • Support for setting slow operation alarm thresholds
  • Support for custom log
  • Support smooth shutdown and update
  • Support push handler
  • Support network list: tcp, tcp4, tcp6, unix, unixpacket and so on
  • Client support automatically redials after disconnection
  • Circuit breaker for overload protection

Example

  • server
package main

import (
    micro "github.com/xiaoenai/tp-micro"
    tp "github.com/henrylee2cn/teleport"
)

// Arg arg
type Arg struct {
    A int
    B int `param:"<range:1:>"`
}

// P handler
type P struct {
    tp.CallCtx
}

// Divide divide API
func (p *P) Divide(arg *Arg) (int, *tp.Status) {
    return arg.A / arg.B, nil
}

func main() {
    srv := micro.NewServer(micro.SrvConfig{
        ListenAddress: ":9090",
    })
    srv.RouteCall(new(P))
    srv.ListenAndServe()
}
  • client
package main

import (
    micro "github.com/xiaoenai/tp-micro"
    tp "github.com/henrylee2cn/teleport"
)

func main() {
    cli := micro.NewClient(
        micro.CliConfig{},
        micro.NewStaticLinker(":9090"),
    )
    defer cli.Close()

    type Arg struct {
        A int
        B int
    }

    var result int
    stat := cli.Call("/p/divide", &Arg{
        A: 10,
        B: 2,
    }, &result).Status()
    if stat != nil {
        tp.Fatalf("%v", stat)
    }
    tp.Infof("10/2=%d", result)
    stat = cli.Call("/p/divide", &Arg{
        A: 10,
        B: 0,
    }, &result).Status()
    if stat == nil {
        tp.Fatalf("%v", stat)
    }
    tp.Infof("test binding error: ok: %v", stat)
}

More Examples

Learn micro Command

Command micro is deployment tools of tp-micro frameware.

  • Quickly create a tp-micro project
  • Run tp-micro project with hot compilation
Generate project

micro gen command help:

NAME:
     micro gen - Generate a tp-micro project

USAGE:
     micro gen [command options] [arguments...]

OPTIONS:
     --template value, -t value    The template for code generation(relative/absolute)
     --app_path value, -p value  The path(relative/absolute) of the project

example: micro gen -p ./myapp or default micro gen myapp

  • The initial template file __tp-micro__tpl__.go:
// package __TPL__ is the project template
package __TPL__

// __API_CALL__ register CALL router:
//  /home
//  /math/divide
type __API_CALL__ interface {
    Home(*struct{}) *HomeResult
    Math
}

// __API_PUSH__ register PUSH router:
//  /stat
type __API_PUSH__ interface {
    Stat(*StatArg)
}

// __MYSQL_MODEL__ create mysql model
type __MYSQL_MODEL__ struct {
    User
    Log
    Device
}

// __MONGO_MODEL__ create mongodb model
type __MONGO_MODEL__ struct {
    Meta
}

// Math controller
type Math interface {
    // Divide handler
    Divide(*DivideArg) *DivideResult
}

// HomeResult home result
type HomeResult struct {
    Content string // text
}

type (
    // DivideArg divide api arg
    DivideArg struct {
        // dividend
        A float64
        // divisor
        B float64 `param:"<range: 0.01:100000>"`
    }
    // DivideResult divide api result
    DivideResult struct {
        // quotient
        C float64
    }
)

// StatArg stat handler arg
type StatArg struct {
    Ts int64 // timestamps
}

// User user info
type User struct {
    Id   int64  `key:"pri"`
    Name string `key:"uni"`
    Age  int32
}

type Log struct {
    Text string
}

type Device struct {
    UUID string `key:"pri"`
}

type Meta struct {
    Hobby []string
    Tags  []string
}
  • The template generated by micro gen command.
├── README.md
├── __tp-micro__gen__.lock
├── __tp-micro__tpl__.go
├── api
│   ├── handler.go
│   ├── pull_handler.gen.go
│   ├── push_handler.gen.go
│   ├── router.gen.go
│   └── router.go
├── args
│   ├── const.gen.go
│   ├── const.go
│   ├── type.gen.go
│   ├── type.go
│   └── var.go
├── config
│   └── config.yaml
├── config.go
├── doc
│   ├── APIDoc.md
│   ├── README.md
│   └── databases.md
├── log
│   └── PID
├── logic
│   ├── model
│   │   ├── init.go
│   │   ├── mongo_meta.gen.go
│   │   ├── mysql_device.gen.go
│   │   ├── mysql_log.gen.go
│   │   └── mysql_user.gen.go
│   └── tmp_code.gen.go
├── main.go
├── rerrs
│   └── rerrs.go
└── sdk
    ├── rpc.gen.go
    ├── rpc.gen_test.go
    ├── rpc.go
    └── rpc_test.go

Desc:

  • This micro gen command only covers files with the ".gen.go" suffix if the __tp-micro__gen__.lock file exists
  • Add .gen suffix to the file name of the automatically generated file, DO NOT EDIT!
  • .tmp is temporary code used to ensure successful compilation!
    It will be overwritten when micro gen is run!
    When the project is completed, it should be removed!
  • The type of handler's parameter and result must be struct!
  • You can modify the created template file __tp-micro__tpl__.go, and run the micro gen command again to update the project

Generated Default Sample

Create README.md(only)

micro newdoc command help:

NAME:
   micro newdoc - Generate a tp-micro project README.md

USAGE:
   micro newdoc [command options] [arguments...]

OPTIONS:
   --app_path value, -p value  The path(relative/absolute) of the project
Run project

micro run command help:

NAME:
     micro run - Compile and run gracefully (monitor changes) an any existing go project

USAGE:
     micro run [options] [arguments...]
 or
     micro run [options except -app_path] [arguments...] {app_path}

OPTIONS:
     --watch_exts value, -x value  Specified to increase the listening file suffix (default: ".go", ".ini", ".yaml", ".toml", ".xml")
     --notwatch value, -n value    Not watch files or directories
     --app_path value, -p value    The path(relative/absolute) of the project

example: micro run -x .yaml -p myapp or micro run

Add model

Add mysql model struct code to project template.

micro tpl command help:

NAME:
  micro tpl - Add mysql model struct code to project template

USAGE:
  micro tpl [command options] [arguments...]

OPTIONS:
  --app_path value, -p value      The path(relative/absolute) of the project
  --host value                    mysql host ip (default: "localhost")
  --port value                    mysql host port (default: "3306")
  --username value, --user value  mysql username (default: "root")
  --password value, --pwd value   mysql password
  --db value                      mysql database (default: "test")
  --table value                   mysql table
  --ssh_user value                ssh user
  --ssh_host value                ssh host ip
  --ssh_port value                ssh host port

More Micro Command

Usage

Peer(server or client) Demo
// Start a server
var peer1 = tp.NewPeer(tp.PeerConfig{
    ListenAddress: "0.0.0.0:9090", // for server role
})
peer1.Listen()

...

// Start a client
var peer2 = tp.NewPeer(tp.PeerConfig{})
var sess, err = peer2.Dial("127.0.0.1:8080")
Call-Controller-Struct API template
type Aaa struct {
    tp.CallCtx
}
func (x *Aaa) XxZz(arg *<T>) (<T>, *tp.Status) {
    ...
    return r, nil
}
  • register it to root router:
// register the call route: /aaa/xx_zz
peer.RouteCall(new(Aaa))

// or register the call route: /xx_zz
peer.RouteCallFunc((*Aaa).XxZz)
Call-Handler-Function API template
func XxZz(ctx tp.CallCtx, arg *<T>) (<T>, *tp.Status) {
    ...
    return r, nil
}
  • register it to root router:
// register the call route: /xx_zz
peer.RouteCallFunc(XxZz)
Push-Controller-Struct API template
type Bbb struct {
    tp.PushCtx
}
func (b *Bbb) YyZz(arg *<T>) *tp.Status {
    ...
    return nil
}
  • register it to root router:
// register the push route: /bbb/yy_zz
peer.RoutePush(new(Bbb))

// or register the push route: /yy_zz
peer.RoutePushFunc((*Bbb).YyZz)
Push-Handler-Function API template
// YyZz register the route: /yy_zz
func YyZz(ctx tp.PushCtx, arg *<T>) *tp.Status {
    ...
    return nil
}
  • register it to root router:
// register the push route: /yy_zz
peer.RoutePushFunc(YyZz)
Unknown-Call-Handler-Function API template
func XxxUnknownCall (ctx tp.UnknownCallCtx) (interface{}, *tp.Status) {
    ...
    return r, nil
}
  • register it to root router:
// register the unknown call route: /*
peer.SetUnknownCall(XxxUnknownCall)
Unknown-Push-Handler-Function API template
func XxxUnknownPush(ctx tp.UnknownPushCtx) *tp.Status {
    ...
    return nil
}
  • register it to root router:
// register the unknown push route: /*
peer.SetUnknownPush(XxxUnknownPush)
The mapping rule of struct(func) name to URI path:
  • AaBb -> /aa_bb
  • Aa_Bb -> /aa/bb
  • aa_bb -> /aa/bb
  • Aa__Bb -> /aa_bb
  • aa__bb -> /aa_bb
  • ABC_XYZ -> /abc/xyz
  • ABcXYz -> /abc_xyz
  • ABC__XYZ -> /abc_xyz
Plugin Demo
// NewIgnoreCase Returns a ignoreCase plugin.
func NewIgnoreCase() *ignoreCase {
    return &ignoreCase{}
}

type ignoreCase struct{}

var (
    _ tp.PostReadCallHeaderPlugin = new(ignoreCase)
    _ tp.PostReadPushHeaderPlugin = new(ignoreCase)
)

func (i *ignoreCase) Name() string {
    return "ignoreCase"
}

func (i *ignoreCase) PostReadCallHeader(ctx tp.ReadCtx) *tp.Status {
    // Dynamic transformation path is lowercase
    ctx.UriObject().Path = strings.ToLower(ctx.UriObject().Path)
    return nil
}

func (i *ignoreCase) PostReadPushHeader(ctx tp.ReadCtx) *tp.Status {
    // Dynamic transformation path is lowercase
    ctx.UriObject().Path = strings.ToLower(ctx.UriObject().Path)
    return nil
}
Register above handler and plugin
// add router group
group := peer.SubRoute("test")
// register to test group
group.RouteCall(new(Aaa), NewIgnoreCase())
peer.RouteCallFunc(XxZz, NewIgnoreCase())
group.RoutePush(new(Bbb))
peer.RoutePushFunc(YyZz)
peer.SetUnknownCall(XxxUnknownCall)
peer.SetUnknownPush(XxxUnknownPush)
Config
// SrvConfig server config
type SrvConfig struct {
    Network           string        `yaml:"network"              ini:"network"              comment:"Network; tcp, tcp4, tcp6, unix or unixpacket"`
    ListenAddress     string        `yaml:"listen_address"       ini:"listen_address"       comment:"Listen address; for server role"`
    TlsCertFile       string        `yaml:"tls_cert_file"        ini:"tls_cert_file"        comment:"TLS certificate file path"`
    TlsKeyFile        string        `yaml:"tls_key_file"         ini:"tls_key_file"         comment:"TLS key file path"`
    DefaultSessionAge time.Duration `yaml:"default_session_age"  ini:"default_session_age"  comment:"Default session max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
    DefaultContextAge time.Duration `yaml:"default_context_age"  ini:"default_context_age"  comment:"Default CALL or PUSH context max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
    SlowCometDuration time.Duration `yaml:"slow_comet_duration"  ini:"slow_comet_duration"  comment:"Slow operation alarm threshold; ns,µs,ms,s ..."`
    DefaultBodyCodec  string        `yaml:"default_body_codec"   ini:"default_body_codec"   comment:"Default body codec type id"`
    PrintDetail       bool          `yaml:"print_detail"         ini:"print_detail"         comment:"Is print body and metadata or not"`
    CountTime         bool          `yaml:"count_time"           ini:"count_time"           comment:"Is count cost time or not"`
    EnableHeartbeat   bool          `yaml:"enable_heartbeat"     ini:"enable_heartbeat"     comment:"enable heartbeat"`
}

// CliConfig client config
type CliConfig struct {
    Network             string               `yaml:"network"                ini:"network"                comment:"Network; tcp, tcp4, tcp6, unix or unixpacket"`
    LocalIP             string               `yaml:"local_ip"               ini:"local_ip"               comment:"Local IP"`
    TlsCertFile         string               `yaml:"tls_cert_file"          ini:"tls_cert_file"          comment:"TLS certificate file path"`
    TlsKeyFile          string               `yaml:"tls_key_file"           ini:"tls_key_file"           comment:"TLS key file path"`
    DefaultSessionAge   time.Duration        `yaml:"default_session_age"    ini:"default_session_age"    comment:"Default session max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
    DefaultContextAge   time.Duration        `yaml:"default_context_age"    ini:"default_context_age"    comment:"Default CALL or PUSH context max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
    DefaultDialTimeout  time.Duration        `yaml:"default_dial_timeout"   ini:"default_dial_timeout"   comment:"Default maximum duration for dialing; for client role; ns,µs,ms,s,m,h"`
    RedialTimes         int                  `yaml:"redial_times"           ini:"redial_times"           comment:"The maximum times of attempts to redial, after the connection has been unexpectedly broken; for client role"`
    Failover            int                  `yaml:"failover"               ini:"failover"               comment:"The maximum times of failover"`
    SlowCometDuration   time.Duration        `yaml:"slow_comet_duration"    ini:"slow_comet_duration"    comment:"Slow operation alarm threshold; ns,µs,ms,s ..."`
    DefaultBodyCodec    string               `yaml:"default_body_codec"     ini:"default_body_codec"     comment:"Default body codec type id"`
    PrintDetail         bool                 `yaml:"print_detail"           ini:"print_detail"           comment:"Is print body and metadata or not"`
    CountTime           bool                 `yaml:"count_time"             ini:"count_time"             comment:"Is count cost time or not"`
    HeartbeatSecond     int                  `yaml:"heartbeat_second"       ini:"heartbeat_second"       comment:"When the heartbeat interval(second) is greater than 0, heartbeat is enabled; if it's smaller than 3, change to 3 default"`
    SessMaxQuota        int                  `yaml:"sess_max_quota"         ini:"sess_max_quota"         comment:"The maximum number of sessions in the connection pool"`
    SessMaxIdleDuration time.Duration        `yaml:"sess_max_idle_duration" ini:"sess_max_idle_duration" comment:"The maximum time period for the idle session in the connection pool; ns,µs,ms,s,m,h"`
    CircuitBreaker      CircuitBreakerConfig `yaml:"circuit_breaker" ini:"circuit_breaker" comment:"Circuit breaker config"`
}

// CircuitBreakerConfig circuit breaker config
type CircuitBreakerConfig struct {
    Enable          bool          `yaml:"enable" ini:"enable" comment:"Whether to use circuit breaker"`
    ErrorPercentage int           `yaml:"error_percentage" ini:"error_percentage" comment:"break linker when the error rate exceeds the threshold during a statistical period; default 50"`
    BreakDuration   time.Duration `yaml:"break_duration" ini:"break_duration" comment:"The period of one-cycle break in milliseconds; must ≥ 1ms"`
}
Param-Tags
tag key required value desc
param meta no (name e.g.param:"<meta:id>") It indicates that the parameter is from the meta.
param swap no name (e.g.param:"<swap:id>") It indicates that the parameter is from the context swap.
param desc no (e.g.param:"<desc:id>") Parameter Description
param len no (e.g.param:"<len:3:6>") Length range [a,b] of parameter's value
param range no (e.g.param:"<range:0:10>") Numerical range [a,b] of parameter's value
param nonzero no - Not allowed to zero
param regexp no (e.g.param:"<regexp:^\\w+$>") Regular expression validation
param stat no (e.g.param:"<stat:100002:wrong password format>") Custom error code and message

NOTES:

  • param:"-" means ignore
  • Encountered untagged exportable anonymous structure field, automatic recursive resolution
  • Parameter name is the name of the structure field converted to snake format
  • If the parameter is not from meta or swap, it is the default from the body
Field-Types
base slice special
string []string [][]byte
byte []byte [][]uint8
uint8 []uint8 struct
bool []bool
int []int
int8 []int8
int16 []int16
int32 []int32
int64 []int64
uint8 []uint8
uint16 []uint16
uint32 []uint32
uint64 []uint64
float32 []float32
float64 []float64
Example
package main

import (
    tp "github.com/henrylee2cn/teleport"
    micro "github.com/xiaoenai/tp-micro"
)

type (
    // Arg arg
    Arg struct {
        A int
        B int `param:"<range:1:100>"`
        Query
        XyZ string `param:"<meta><nonzero><stat: 100002: Parameter cannot be empty>"`
    }
    Query struct {
        X string `param:"<meta>"`
    }
)

// P handler
type P struct {
    tp.CallCtx
}

// Divide divide API
func (p *P) Divide(arg *Arg) (int, *tp.Status) {
    tp.Infof("query arg x: %s, xy_z: %s", arg.Query.X, arg.XyZ)
    return arg.A / arg.B, nil
}

func main() {
    srv := micro.NewServer(micro.SrvConfig{
        ListenAddress:   ":9090",
        EnableHeartbeat: true,
    })
    group := srv.SubRoute("/static")
    group.RouteCall(new(P))
    srv.ListenAndServe()
}

Detail Example

Optimize
  • SetMessageSizeLimit sets max packet size. If maxSize<=0, set it to max uint32.
func SetMessageSizeLimit(maxMessageSize uint32)
  • SetSocketKeepAlive sets whether the operating system should send keepalive messages on the connection.
func SetSocketKeepAlive(keepalive bool)
  • SetSocketKeepAlivePeriod sets period between keep alives.
func SetSocketKeepAlivePeriod(d time.Duration)
  • SetSocketNoDelay controls whether the operating system should delay packet transmission in hopes of sending fewer packets (Nagle's algorithm). The default is true (no delay), meaning that data is sent as soon as possible after a Write.
func SetSocketNoDelay(_noDelay bool)
  • SetSocketReadBuffer sets the size of the operating system's receive buffer associated with the connection.
func SetSocketReadBuffer(bytes int)
  • SetSocketWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.
func SetSocketWriteBuffer(bytes int)

More Usage

License

Micro is under Apache v2 License. See the LICENSE file for the full license text

Documentation

Overview

Package tp-micro is a simple, powerful micro service framework based on Teleport.

Copyright 2018 HenryLee and xiaoenai. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (
	// RerrClientClosed: client is closed.
	RerrClientClosed = tp.NewStatus(100, "client is closed", "")
	// RerrInvalidParameter: Invalid Parameter
	RerrInvalidParameter = tp.NewStatus(tp.CodeBadMessage, "Invalid Parameter", "")
	// RerrInternalServerError: System is busy, please try again later
	RerrInternalServerError = tp.NewStatus(tp.CodeInternalServerError, "System is busy, please try again later", "")
	// RerrNotFound: Not Found
	RerrNotFound = tp.NewStatus(tp.CodeNotFound, "Not Found", "")
	// RerrNotOnline: User is not online
	RerrNotOnline = tp.NewStatus(tp.CodeNotFound, "Not Found", "User is not online")
	// RerrRenderFailed: Template Rendering Failed
	RerrRenderFailed = tp.NewStatus(tp.CodeInternalServerError, "Template Rendering Failed", "")
)

NOTE: error code range [-1,999]

Functions

func InnerIpPort

func InnerIpPort(port string) (string, error)

InnerIpPort returns the service's intranet address, such as '192.168.1.120:8080'.

func OuterIpPort

func OuterIpPort(port string) (string, error)

OuterIpPort returns the service's extranet address, such as '113.116.141.121:8080'.

Types

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	Enable          bool          `yaml:"enable" ini:"enable" comment:"Whether to use circuit breaker"`
	ErrorPercentage int           `` /* 151-byte string literal not displayed */
	BreakDuration   time.Duration `yaml:"break_duration" ini:"break_duration" comment:"The period of one-cycle break in milliseconds; must ≥ 1ms"`
}

CircuitBreakerConfig circuit breaker config

type CliConfig

type CliConfig struct {
	Network            string               `yaml:"network"                ini:"network"                comment:"Network; tcp, tcp4, tcp6, unix or unixpacket"`
	LocalIP            string               `yaml:"local_ip"               ini:"local_ip"               comment:"Local IP"`
	TlsCertFile        string               `yaml:"tls_cert_file"          ini:"tls_cert_file"          comment:"TLS certificate file path"`
	TlsKeyFile         string               `yaml:"tls_key_file"           ini:"tls_key_file"           comment:"TLS key file path"`
	DefaultSessionAge  time.Duration        `` /* 152-byte string literal not displayed */
	DefaultContextAge  time.Duration        `` /* 165-byte string literal not displayed */
	DefaultDialTimeout time.Duration        `` /* 139-byte string literal not displayed */
	Failover           int                  `yaml:"failover"               ini:"failover"               comment:"The maximum times of failover"`
	SlowCometDuration  time.Duration        `yaml:"slow_comet_duration"    ini:"slow_comet_duration"    comment:"Slow operation alarm threshold; ns,µs,ms,s ..."`
	DefaultBodyCodec   string               `yaml:"default_body_codec"     ini:"default_body_codec"     comment:"Default body codec type id"`
	PrintDetail        bool                 `yaml:"print_detail"           ini:"print_detail"           comment:"Is print body and metadata or not"`
	CountTime          bool                 `yaml:"count_time"             ini:"count_time"             comment:"Is count cost time or not"`
	HeartbeatSecond    int                  `` /* 189-byte string literal not displayed */
	CircuitBreaker     CircuitBreakerConfig `yaml:"circuit_breaker" ini:"circuit_breaker" comment:"Circuit breaker config"`
}

CliConfig client config Note:

yaml tag is used for github.com/henrylee2cn/cfgo
ini tag is used for github.com/henrylee2cn/ini

func (*CliConfig) Check

func (c *CliConfig) Check() error

Check check and correct config.

func (*CliConfig) Reload

func (c *CliConfig) Reload(bind cfgo.BindFunc) error

Reload Bi-directionally synchronizes config between YAML file and memory.

type Client

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

Client client peer

func NewClient

func NewClient(cfg CliConfig, linker Linker, globalLeftPlugin ...tp.Plugin) *Client

NewClient creates a client peer.

func (*Client) AsyncCall

func (c *Client) AsyncCall(
	serviceMethod string,
	arg interface{},
	result interface{},
	callCmdChan chan<- tp.CallCmd,
	setting ...tp.MessageSetting,
) tp.CallCmd

AsyncCall sends a packet and receives reply asynchronously. Note:

If the arg is []byte or *[]byte type, it can automatically fill in the body codec name;
If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure;
Do not support failover to try again.

func (*Client) Call

func (c *Client) Call(serviceMethod string, arg interface{}, result interface{}, setting ...tp.MessageSetting) tp.CallCmd

Call sends a packet and receives reply. Note:

If the arg is []byte or *[]byte type, it can automatically fill in the body codec name;
If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.

func (*Client) Close

func (c *Client) Close()

Close closes client.

func (*Client) Peer

func (c *Client) Peer() tp.Peer

Peer returns the peer.

func (*Client) PluginContainer

func (c *Client) PluginContainer() *tp.PluginContainer

PluginContainer returns the global plugin container.

func (*Client) Push

func (c *Client) Push(serviceMethod string, arg interface{}, setting ...tp.MessageSetting) *tp.Status

Push sends a packet, but do not receives reply. Note:

If the arg is []byte or *[]byte type, it can automatically fill in the body codec name;
If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.

func (*Client) RoutePush

func (c *Client) RoutePush(ctrlStruct interface{}, plugin ...tp.Plugin) []string

RoutePush registers PUSH handlers, and returns the paths.

func (*Client) RoutePushFunc

func (c *Client) RoutePushFunc(pushHandleFunc interface{}, plugin ...tp.Plugin) string

RoutePushFunc registers PUSH handler, and returns the path.

func (*Client) SetProtoFunc

func (c *Client) SetProtoFunc(protoFunc tp.ProtoFunc)

SetProtoFunc sets tp.ProtoFunc.

func (*Client) SubRoute

func (c *Client) SubRoute(pathPrefix string, plugin ...tp.Plugin) *tp.SubRouter

SubRoute adds handler group.

func (*Client) UseCallHeartbeat

func (c *Client) UseCallHeartbeat()

UseCallHeartbeat uses CALL method to ping.

func (*Client) UsePushHeartbeat

func (c *Client) UsePushHeartbeat()

UsePushHeartbeat uses PUSH method to ping.

type Linker

type Linker interface {
	// Select selects a service address by URI path.
	Select(uriPath string, exclude map[string]struct{}) (addr string, stat *tp.Status)
	// Len returns the number of nodes corresponding to the URI.
	Len(uriPath string) int
	// WatchOffline pushs service node offline notification.
	WatchOffline() <-chan string
	// Close closes the linker.
	Close()
}

Linker linker for client.

func NewStaticLinker

func NewStaticLinker(srvAddr string) Linker

NewStaticLinker creates a static linker.

type Server

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

Server server peer

func NewServer

func NewServer(cfg SrvConfig, globalLeftPlugin ...tp.Plugin) *Server

NewServer creates a server peer.

func (*Server) Close

func (s *Server) Close() error

Close closes server.

func (*Server) CountSession

func (s *Server) CountSession() int

CountSession returns the number of sessions.

func (*Server) GetSession

func (s *Server) GetSession(sessionId string) (tp.Session, bool)

GetSession gets the session by id.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(protoFunc ...tp.ProtoFunc) error

ListenAndServe turns on the listening service.

func (*Server) Peer

func (s *Server) Peer() tp.Peer

Peer returns the peer

func (*Server) PluginContainer

func (s *Server) PluginContainer() *tp.PluginContainer

PluginContainer returns the global plugin container.

func (*Server) RangeSession

func (s *Server) RangeSession(fn func(sess tp.Session) bool)

RangeSession ranges all sessions. If fn returns false, stop traversing.

func (*Server) RouteCall

func (s *Server) RouteCall(ctrlStruct interface{}, plugin ...tp.Plugin) []string

RouteCall registers CALL handlers, and returns the paths.

func (*Server) RouteCallFunc

func (s *Server) RouteCallFunc(callHandleFunc interface{}, plugin ...tp.Plugin) string

RouteCallFunc registers CALL handler, and returns the path.

func (*Server) RoutePush

func (s *Server) RoutePush(ctrlStruct interface{}, plugin ...tp.Plugin) []string

RoutePush registers PUSH handlers, and returns the paths.

func (*Server) RoutePushFunc

func (s *Server) RoutePushFunc(pushHandleFunc interface{}, plugin ...tp.Plugin) string

RoutePushFunc registers PUSH handler, and returns the path.

func (*Server) Router

func (s *Server) Router() *tp.Router

Router returns the root router of call or push handlers.

func (*Server) ServeConn

func (s *Server) ServeConn(conn net.Conn, protoFunc ...tp.ProtoFunc) (tp.Session, *tp.Status)

ServeConn serves the connection and returns a session.

func (*Server) SetBindErrorFunc

func (s *Server) SetBindErrorFunc(fn binder.ErrorFunc)

SetBindErrorFunc sets the binding or balidating error function. Note: If fn=nil, set as default.

func (*Server) SetUnknownCall

func (s *Server) SetUnknownCall(fn func(tp.UnknownCallCtx) (interface{}, *tp.Status), plugin ...tp.Plugin)

SetUnknownCall sets the default handler, which is called when no handler for CALL is found.

func (*Server) SetUnknownPush

func (s *Server) SetUnknownPush(fn func(tp.UnknownPushCtx) *tp.Status, plugin ...tp.Plugin)

SetUnknownPush sets the default handler, which is called when no handler for PUSH is found.

func (*Server) SubRoute

func (s *Server) SubRoute(pathPrefix string, plugin ...tp.Plugin) *tp.SubRouter

SubRoute adds handler group.

type SrvConfig

type SrvConfig struct {
	Network           string        `yaml:"network"              ini:"network"              comment:"Network; tcp, tcp4, tcp6, unix or unixpacket"`
	ListenAddress     string        `yaml:"listen_address"       ini:"listen_address"       comment:"Listen address; for server role"`
	TlsCertFile       string        `yaml:"tls_cert_file"        ini:"tls_cert_file"        comment:"TLS certificate file path"`
	TlsKeyFile        string        `yaml:"tls_key_file"         ini:"tls_key_file"         comment:"TLS key file path"`
	DefaultSessionAge time.Duration `` /* 148-byte string literal not displayed */
	DefaultContextAge time.Duration `` /* 161-byte string literal not displayed */
	SlowCometDuration time.Duration `yaml:"slow_comet_duration"  ini:"slow_comet_duration"  comment:"Slow operation alarm threshold; ns,µs,ms,s ..."`
	DefaultBodyCodec  string        `yaml:"default_body_codec"   ini:"default_body_codec"   comment:"Default body codec type id"`
	PrintDetail       bool          `yaml:"print_detail"         ini:"print_detail"         comment:"Is print body and metadata or not"`
	CountTime         bool          `yaml:"count_time"           ini:"count_time"           comment:"Is count cost time or not"`
	EnableHeartbeat   bool          `yaml:"enable_heartbeat"     ini:"enable_heartbeat"     comment:"enable heartbeat"`
}

SrvConfig server config Note:

yaml tag is used for github.com/henrylee2cn/cfgo
ini tag is used for github.com/henrylee2cn/ini

func (*SrvConfig) InnerIpPort

func (s *SrvConfig) InnerIpPort() string

InnerIpPort returns the service's intranet address, such as '192.168.1.120:8080'.

func (*SrvConfig) ListenPort

func (s *SrvConfig) ListenPort() string

ListenPort returns the listened port, such as '9090'.

func (*SrvConfig) OuterIpPort

func (s *SrvConfig) OuterIpPort() string

OuterIpPort returns the service's extranet address, such as '113.116.141.121:8080'.

func (*SrvConfig) PeerConfig

func (s *SrvConfig) PeerConfig() tp.PeerConfig

func (*SrvConfig) Reload

func (s *SrvConfig) Reload(bind cfgo.BindFunc) error

Reload Bi-directionally synchronizes config between YAML file and memory.

Directories

Path Synopsis
cmd
micro
Command micro is a deployment tools of tp-micro frameware.
Command micro is a deployment tools of tp-micro frameware.
Package discovery is the service discovery module implemented by ETCD.
Package discovery is the service discovery module implemented by ETCD.
examples
Package gateway is the main program for TCP and HTTP services.
Package gateway is the main program for TCP and HTTP services.
helper/agent/proto
Package agent is a generated protocol buffer package.
Package agent is a generated protocol buffer package.
helper/gray/logic/model
Package model is a generated protocol buffer package.
Package model is a generated protocol buffer package.
helper/gray/types
Package types is a generated protocol buffer package.
Package types is a generated protocol buffer package.
sdk
types
Package types is a generated protocol buffer package.
Package types is a generated protocol buffer package.
mod-html
Package html is HTML render for http client.
Package html is HTML render for http client.
micro
run
run/fsnotify
Package fsnotify implements file system notification.
Package fsnotify implements file system notification.
model
etcd
etcd package is the [ETCD](https://github.com/coreos/etcd) client v3 mirror.
etcd package is the [ETCD](https://github.com/coreos/etcd) client v3 mirror.
redis
redis (cluster) client package.
redis (cluster) client package.
sqlx
Package sqlx provides general purpose extensions to database/sql.
Package sqlx provides general purpose extensions to database/sql.
sqlx/reflectx
Package reflectx implements extensions to the standard reflect lib suitable for implementing marshalling and unmarshalling packages.
Package reflectx implements extensions to the standard reflect lib suitable for implementing marshalling and unmarshalling packages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL