tars

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2019 License: BSD-3-Clause Imports: 42 Imported by: 0

README

TarsGo Document Build Status Codacy Badge

About

  • Tarsgo is high performance RPC framework in Golang programing language using the tars protocol.

  • Go has become popular for programming with the rise of containerization technology such as docker, k8s, and etcd.

  • Go's goroutine concurrency mechanism means Go is very suitable for large-scale high-concurrency back-end server program development. The Go language has near C/C++ performance and near Python productivity.

  • At Tencent, part of the existing C++ development team has gradually turned into Go developers. Tars, a widely used RPC framework, supports C++, Java, NodeJS, and PHP, and now Go. The combination with Go language has become a general trend. Therefore, in the voice of users, we launched Tarsgo, and we have applied to Tencent map application, YingYongbao application, Internet plus and other projects.

  • Learn more about the whole Tars architecture and design at Introduction.

Function & features

  • Tars2go tool: tars file is automatically generated and converted into Go language, contains RPC server/client code;
  • Serialization and deserialization of tars protocol in Go.
  • Auto service discovery.
  • TCP/UDP/Http server & Client.
  • The support of local logging and remote logging.
  • The support of statistical reporting, property statistics,and anormaly reporting.
  • The support of set division.
  • The support of protocol buffers. See more in pb2tarsgo
  • The support of filter.
  • The support of zipkin opentracing.

Install

  • For install OSS and other basic servers, see the Installation document. For quick install OSS and other basic servers, see more about Deploy
  • Requires Go 1.9.x or above (see https://golang.org/doc/install for help installing Go)
  • go get -u github.com/TarsCloud/TarsGo/tars

Quickstart

Performance

Usage

1 server
  • Below is a full example illustrating how to use tarsgo to build a server.
1.1 interface definition

Create a tars file, like hello.tars, under $GOPATH/src (for example, $GOPATH/src/TestApp/TestServer/hello.tars).

For more detail about tars protocol, see tars_protocol Tars protocol is a binary ,IDL-based protocol similar to protocolbuffers.

module TestApp
{
	
	interface Hello
	{
	    int test();
	    int testHello(string sReq, out string sRsp);
	};
	
};	
1.2 compile interface definition file
1.2.1 build tars2go

Compile and install the tars2go tools.

go install $GOPATH/src/github.com/TarsCloud/TarsGo/tars/tools/tars2go
1.2.2 compile the tars file and translate into go file
tars2go --outdir=./vendor hello.tars
1.3 implement the interface
package main

import (
    "github.com/TarsCloud/TarsGo/tars"

    "TestApp"
)

type HelloImp struct {
}

//implete the Test interface
func (imp *HelloImp) Test() (int32, error) {
    return 0, nil 
}

//implete the testHello interface

func (imp *HelloImp) TestHello(in string, out *string) (int32, error) {
    *out = in
    return 0, nil 
}


func main() { //Init servant
    imp := new(HelloImp)                                    //New Imp
    app := new(TestApp.Hello)                               //New init the A Tars
    cfg := tars.GetServerConfig()                           //Get Config File Object
    app.AddServant(imp, cfg.App+"."+cfg.Server+".HelloObj") //Register Servant
    tars.Run()
}

illustration:

  • HelloImp is the struct where you implement the Hello & Test interface, notice that Test & Hello must start with upper letter to be exported, which is the only place distinguished from tars file definition.
  • TestApp.Hello is generated by tar2go tools, which could be found in ./vendor/TestApp/Hello_IF.go, with a package named TestApp which is same as module TestApp in the tars file.
  • tars.GetServerConfig() is used to get server side configuration.
  • cfg.App+"."+cfg.Server+".HelloObj" is the object name bind to the Servant, which client will use this name to access the server.
1.4 ServerConfig

tars.GetServerConfig() return a server config,which is defined as below:

type serverConfig struct {
	Node      string
	App       string
	Server    string
	LogPath   string
	LogSize   string
	LogLevel  string
	Version   string
	LocalIP   string
	BasePath  string
	DataPath  string
	config    string
	notify    string
	log       string
	netThread int
	Adapters  map[string]adapterConfig

	Container   string
	Isdocker    bool
	Enableset   bool
	Setdivision string
}


  • Node: local tarsnode address, only if u use tars platform to deploy will use this parameter.
  • APP: The application name.
  • Server: The server name.
  • LogPath: The directory to save logs.
  • LogSize: The size when ratate logs.
  • LogLevel: The rotate log level.
  • Version: Tarsgo version.
  • LocalIP: Local ip address.
  • BasePath: Base path for the binary.
  • DataPath: Path for store some cache file.
  • config: The configuration center for getting configuration, like tars.tarsconfig.ConfigObj.
  • notify: The notify center for report notify report , like tars.tarsnotify.NotifyObj
  • log: The remote log center , like tars.tarslog.LogObj
  • netThread: Reserved for controlling the go routine that receive and send packages.
  • Adapters: The specify configuration for each adapters
  • Container: Reserved for later use, to store the container name.
  • Isdocker: Reserved for later use, to specify if the server running inside a container.
  • Enableset: True if used set division.
  • Setdivision: To specify which set division ,like gray.sz.*

A server side configuration look like:

<tars>
  <application>
      enableset=Y
      setdivision=gray.sz.*
    <server>
       node=tars.tarsnode.ServerObj@tcp -h 10.120.129.226 -p 19386 -t 60000
       app=TestApp
       server=HelloServer
       localip=10.120.129.226
       local=tcp -h 127.0.0.1 -p 20001 -t 3000
       basepath=/usr/local/app/tars/tarsnode/data/TestApp.HelloServer/bin/
       datapath=/usr/local/app/tars/tarsnode/data/TestApp.HelloServer/data/
       logpath=/usr/local/app/tars/app_log/
       logsize=10M
       config=tars.tarsconfig.ConfigObj
       notify=tars.tarsnotify.NotifyObj
       log=tars.tarslog.LogObj
       #timeout for deactiving , ms.
       deactivating-timeout=2000
       logLevel=DEBUG
    </server>
  </application>
</tars>

1.5 Adapter

An adapter represent a bind ip port for certain object. app.AddServant(imp, cfg.App+"."+cfg.Server+".HelloObj") in the server implement code example , fulfill the binding of the adapter configuration and implement for the HelloObj. A full example for adapter, see below:

<tars>
  <application>
    <server>
       #each adapter configuration 
       <TestApp.HelloServer.HelloObjAdapter>
            #allow Ip for white list.
            allow
            # ip and port to listen on  
            endpoint=tcp -h 10.120.129.226 -p 20001 -t 60000
            #handlegroup
            handlegroup=TestApp.HelloServer.HelloObjAdapter
            #max connection 
            maxconns=200000
            #portocol, only tars for now.
            protocol=tars
            #max capbility in handle queue.
            queuecap=10000
            #timeout in ms for the request in the queue.
            queuetimeout=60000
            #servant 
            servant=TestApp.HelloServer.HelloObj
            #threads in handle server side implement code. goroutine for golang.
            threads=5
       </TestApp.HelloServer.HelloObjAdapter>
    </server>
  </application>
</tars>
1.6 start the server

The command for starting the server:

./HelloServer --config=config.conf

See below for a full example of config.conf ,We will explain the client side configuration later.

<tars>
  <application>
    enableset=n
    setdivision=NULL
    <server>
       node=tars.tarsnode.ServerObj@tcp -h 10.120.129.226 -p 19386 -t 60000
       app=TestApp
       server=HelloServer
       localip=10.120.129.226
       local=tcp -h 127.0.0.1 -p 20001 -t 3000
       basepath=/usr/local/app/tars/tarsnode/data/TestApp.HelloServer/bin/
       datapath=/usr/local/app/tars/tarsnode/data/TestApp.HelloServer/data/
       logpath=/usr/local/app/tars/app_log/
       logsize=10M
       config=tars.tarsconfig.ConfigObj
       notify=tars.tarsnotify.NotifyObj
       log=tars.tarslog.LogObj
       deactivating-timeout=2000
       logLevel=DEBUG
       <TestApp.HelloServer.HelloObjAdapter>
            allow
            endpoint=tcp -h 10.120.129.226 -p 20001 -t 60000
            handlegroup=TestApp.HelloServer.HelloObjAdapter
            maxconns=200000
            protocol=tars
            queuecap=10000
            queuetimeout=60000
            servant=TestApp.HelloServer.HelloObj
            threads=5
       </TestApp.HelloServer.HelloObjAdapter>
    </server>
    <client>
       locator=tars.tarsregistry.QueryObj@tcp -h 10.120.129.226 -p 17890
       sync-invoke-timeout=3000
       async-invoke-timeout=5000
       refresh-endpoint-interval=60000
       report-interval=60000
       sample-rate=100000
       max-sample-count=50
       asyncthread=3
       modulename=TestApp.HelloServer
    </client>
  </application>
</tars>
2 client

User can write a client side code easily without writing any protocol-specified communicating code.

2.1 client example

A client side example:

package main

import (
    "fmt"
    "github.com/TarsCloud/TarsGo/tars"
    "TestApp"
)
//tars.Communicator should only init once and be global
var comm *tars.Communicator

func main() {
    comm = tars.NewCommunicator()
    obj := "TestApp.TestServer.HelloObj@tcp -h 127.0.0.1 -p 10015 -t 60000"
    app := new(TestApp.Hello)
    comm.StringToProxy(obj, app)
	var req string="Hello World"
    var out string
    ret, err := app.TestHello(req, &out)
    if err != nil {
        fmt.Println(err)
        return
    }   
    fmt.Println(ret, out)
}


illustration:

  • package TestApp was generated by tars2go tool using the tars protocol file.
  • comm: A Communicator is used for communicating with the server side which should only init once and be global.
  • obj: object name which to specify the server ip and port. Usually, we just need the object name before the "@" character.
  • app: Application that associated with the interface in the tars file. In this case, it's TestApp.Hello.
  • StringToProxy: StringToProxy method is used for binding the object name and the application, if don't do this, communicator won't know who to communicate with for the application.
  • req, res: In and out parameter which define in the tars file for the TestHello method.
  • app.TestHello is used to call the method defined in the tars file ,and ret and err will be returned .
2.2 communicator

A communicator represent a group of resources for sending and receiving packages for the client side, which in the end manages the socket communicating for each object. U will only need one communicator in a program.

var comm *tars.Communicato
comm = tars.NewCommunicator()
comm.SetProperty("property", "tars.tarsproperty.PropertyObj")
comm.SetProperty("locator", "tars.tarsregistry.QueryObj@tcp -h ... -p ...")

Description:

  • The communicator's configuration file format will be described later.
  • Communicators can be configured without a configuration file, and all parameters have default values.
  • The communicator can also be initialized directly through the "SetProperty" method.
  • If you don't want a configuration file, you must set the locator parameter yourself.

Communicator attribute description:

  • locator:The address of the registry service must be in the format "ip port". If you do not need the registry to locate the service, you do not need to configure this item. *important async-invoke-timeout:The maximum timeout (in milliseconds) for client calls. The default value for this configuration is 3000.
  • sync-invoke-timeout:Unused for tarsgo right now.
  • refresh-endpoint-interval:The interval (in milliseconds) for periodically accessing the registry to obtain information. The default value for this configuration is one minute.
  • stat:The address of the service is called between modules. If this item is not configured, it means that the reported data will be directly discarded.
  • property:The address that the service reports its attribute. If it is not configured, this means that the reported data is directly discarded.
  • report-interval:Unused for tarsgo for now.
  • asyncthread: Discarded for tarsgo.
  • modulename:The module name, the default value is the name of the executable program.

The format of the communicator's configuration file is as follows:

<tars>
  <application>
    #The configuration required by the proxy
    <client>
        #address
        locator                     = tars.tarsregistry.QueryObj@tcp -h 127.0.0.1 -p 17890
        #The maximum timeout (in milliseconds) for synchronous calls.
        sync-invoke-timeout         = 3000
        #The maximum timeout (in milliseconds) for asynchronous calls.
        async-invoke-timeout        = 5000
        #The maximum timeout (in milliseconds) for synchronous calls.
        refresh-endpoint-interval   = 60000
        #Used for inter-module calls
        stat                        = tars.tarsstat.StatObj
        #Address used for attribute reporting
        property                    = tars.tarsproperty.PropertyObj
        #report time interval
        report-interval             = 60000
        #The number of threads that process asynchronous responses
        asyncthread                 = 3
        #The module name
        modulename                  = Test.HelloServer
    </client>
  </application>
</tars>
2.3 Timeout control

if u want to use timeout control in the client side, use TarsSetTimeout which in ms.

app := new(TestApp.Hello)
comm.StringToProxy(obj, app)
app.TarsSetTimeout(3000)
2.4 Call interface

This section details how the Tars client remotely invokes the server.

First, briefly describe the addressing mode of the Tars client. Secondly, it will introduce the calling method of the client, including but not limited to one-way calling, synchronous calling, asynchronous calling, hash calling, and so on.

2.4.1. Introduction to addressing mode

The addressing mode of the Tars service can usually be divided into two ways: the service name is registered in the master and the service name is not registered in the master. A master is a name server (routing server) dedicated to registering service node information.

The service name added in the name server is implemented through the operation management platform.

For services that are not registered with the master, it can be classified as direct addressing, that is, the ip address of the service provider needs to be specified before calling the service. The client needs to specify the specific address of the HelloObj object when calling the service:

that is: Test.HelloServer.HelloObj@tcp -h 127.0.0.1 -p 9985

Test.HelloServer.HelloObj: Object name

tcp:Tcp protocol

-h:Specify the host address, here is 127.0.0.1

-p:Port, here is 9985

If HelloServer is running on two servers, app is initialized as follows:

obj:= "Test.HelloServer.HelloObj@tcp -h 127.0.0.1 -p 9985:tcp -h 192.168.1.1 -p 9983"
app := new(TestApp.Hello)
comm.StringToProxy(obj, app)

The address of HelloObj is set to the address of the two servers. At this point, the request will be distributed to two servers (distribution method can be specified, not introduced here). If one server is down, the request will be automatically assigned to another one, and the server will be restarted periodically.

For services registered in the master, the service is addressed based on the service name. When the client requests the service, it does not need to specify the specific address of the HelloServer, but it needs to specify the address of the registry when generating the communicator or initializing the communicator.

The following shows the address of the registry by setting the parameters of the communicator:

var *tars.Communicator
comm = tars.NewCommunicator()
comm.SetProperty("locator", "tars.tarsregistry.QueryObj@tcp -h ... -p ...")

Since the client needs to rely on the registry's address, the registry must also be fault-tolerant. The registry's fault-tolerant method is the same as above, specifying the address of the two registry.

2.4.2. One-way call

TODO. Unsupported yet in tarsgo.

2.4.3. Synchronous call
package main

import (
    "fmt"
    "github.com/TarsCloud/TarsGo/tars"
    "TestApp"
)

var *tars.Communicator
func main() {
    comm = tars.NewCommunicator()
    obj := "TestApp.TestServer.HelloObj@tcp -h 127.0.0.1 -p 10015 -t 60000"
    app := new(TestApp.Hello)
    comm.StringToProxy(obj, app)
	var req string="Hello World"
    var out string
    ret, err := app.TestHello(req, &out)
    if err != nil {
        fmt.Println(err)
        return
    }   
    fmt.Println(ret, out)
}

2.4.4 Asynchronous call

tarsgo can use Asynchronous call easily using go routine. Unlike cpp, we don't need to implement a callback function.

package main

import (
    "fmt"
    "github.com/TarsCloud/TarsGo/tars"
    "time"
    "TestApp"
)
var *tars.Communicator
func main() {
    comm = tars.NewCommunicator()
    obj := "TestApp.TestServer.HelloObj@tcp -h 127.0.0.1 -p 10015 -t 60000"
    app := new(TestApp.Hello)
    comm.StringToProxy(obj, app)
	go func(){
		var req string="Hello World"
    	var out string
    	ret, err := app.TestHello(req, &out)
    	if err != nil {
        	fmt.Println(err)
        	return
    	} 
		fmt.Println(ret, out)
	}()
    time.Sleep(1)  
}

2.4.5 call by set

Client can call Server by set through configuration file mentioned about. Which enableset will be y and setdivision will set like gray.sz.* . See https://github.com/TarsCloud/Tars/blob/master/docs-en/tars_idc_set.md for more detail. If u want call by set manually, tarsgo will support this feature soon.

2.4.6. Hash call

Since multiple servers can be deployed, client requests are randomly distributed to the server, but in some cases, it is desirable that certain requests are always sent to a particular server. In this case, Tars provides a simple way to achieve which is called hash-call. Tarsgo will support this feature soon.

3 return code defined by tars.
//Define the return code given by the TARS service
const int TARSSERVERSUCCESS       = 0;    //Server-side processing succeeded
const int TARSSERVERDECODEERR     = -1;   //Server-side decoding exception
const int TARSSERVERENCODEERR     = -2;   //Server-side encoding exception
const int TARSSERVERNOFUNCERR     = -3;   //There is no such function on the server side
const int TARSSERVERNOSERVANTERR  = -4;   //The server does not have the Servant object
const int TARSSERVERRESETGRID     = -5;   // server grayscale state is inconsistent
const int TARSSERVERQUEUETIMEOUT  = -6;   //server queue exceeds limit
const int TARSASYNCCALLTIMEOUT    = -7;   // Asynchronous call timeout
const int TARSINVOKETIMEOUT       = -7;   //call timeout
const int TARSPROXYCONNECTERR     = -8;   //proxy link exception
const int TARSSERVEROVERLOAD      = -9;   //Server overload, exceeding queue length
const int TARSADAPTERNULL         = -10;  //The client routing is empty, the service does not exist or all services are down.
const int TARSINVOKEBYINVALIDESET = -11;  //The client calls the set rule illegally
const int TARSCLIENTDECODEERR     = -12;  //Client decoding exception
const int TARSSERVERUNKNOWNERR    = -99;  //The server is in an abnormal position
4 log

A quick example for using tarsgo rotating log

TLOG := tars.GetLogger("TLOG")
TLOG.Debug("Debug logging")

This is will create a *Rogger.Logger ,which was defined in tars/util/rogger, and after GetLogger was called, and a logfile is created under Logpath defined in the config.conf which name is cfg.App + "." + cfg.Server + "_" + name ,and will be rotated after 100MB(default) , and max rotated file is 10(default).

if u don't want to rotate log by file size. For example , u want to rotate by day, then use:

TLOG := tars.GetDayLogger("TLOG",1)
TLOG.Debug("Debug logging")

for rotating by hour, use GetHourLogger("TLOG",1). If u want to log to remote server ,which is defined in config.conf named tars.tarslog.LogObj. A full tars file definition can be found in tars/protocol/res/LogF.tars. U have to setup a log server before doing this. A log server can be found under Tencent/Tars/cpp/framework/LogServer .A quick example,

TLOG := GetRemoteLogger("TLOG")
TLOG.Debug("Debug logging")

if u want to set the loglevel , u can set it from OSS platform provided by tars project under Tencent/Tars/web. If u want to customize ur logger, see more detail in tars/util/rogger , tars/logger.go and tars/remotelogger.go

5 Service management

The Tars server framework supports dynamic receiving commands to handle related business logic, such as dynamic update configuration.

tarsgo currently has tars.viewversion / tars.setloglevel administration commands for now. User can send admin command from oss to see what version is or setting loglevel mentioned about.

if u want to defined ur own admin commands, see this example

func helloAdmin(who string ) (string, error) {
	return who, nil
}
tars.RegisterAdmin("tars.helloAdmin",  helloAdmin)

Then u can send self-defined admin command "tars.helloAdmin tarsgo" and tarsgo will be shown in browser.

Illustration:

// A function  should be in this format
type adminFn func(string) (string, error)

//then u should registry this function using

func RegisterAdmin(name string, fn adminFn)
6 Statistical reporting

Reporting statistics information is the logic of reporting the time-consuming information and other information to tarsstat inside the Tars framework. No user development is required. After the relevant information is correctly set during program initialization, it can be automatically reported inside the framework (including the client and the server).

After the client call the reporting interface, it is temporarily stored in memory. When it reaches a certain time point, it is reported to the tarsstat service (the default is once reporting 1 minute). We call the time gap between the two reporting time points as a statistical interval, and perform the operations such as accumulating and comparing the same key in a statistical interval. The sample code is as follows:

//for error
ReportStat(msg, 0, 1, 0)

//for success
ReportStat(msg, 1, 0, 0)


//func ReportStat(msg *Message, succ int32, timeout int32, exec int32)
//see more detail in tars/statf.go

Description:

  • Normally, we don't have to concert about the Statistical reporting ,the tarsgo framework will do this report after every client call server ,no matter success or failure. And the success rate ,fail rate ,average cost time and so on will be shown in the web management system if u setup properly.
  • If the main service is deployed on the web management system, you do not need to define Communicator set the configurations of tarsregistry, tarsstat, etc., the service will be automatically reported.
  • If the main service or program is not deployed on the web management system, you need to define the Communicator, set the tarsregistry, tarsstat, etc., so that you can view the service monitoring of the called service on the web management system.
  • The reported data is reported regularly and can be set in the configuration of the communicator.
7 Anormaly reporting

For better monitoring, the TARS framework supports reporting abnormal situation directly to tarsnotify in the program and can be viewed on the WEB management page.

The framework provides three macros to report different kinds of exceptions:

tars.reportNotifyInfo("Get data from mysql error!")

Info is a string, which can directly report the string to tarsnotify. The reported string can be seen on the page, subsequently, we can alarm according to the reported information.

8 Attribute(Property) Statistics

In order to facilitate business statistics, the TARS framework also supports the display of information on the web management platform.

The types of statistics currently supported include the following:

  • Sum(sum) //calculate the sum of every report value.
  • Average(avg) //calculate the average of every report value
  • Distribution(distr) //calculate the distribution of every report,which parameter is a list, and calculate the probability distribution of each interval
  • Maximum(max) //calculate the maximum of every report value .
  • Minimum(min) // calculate the minimum of every report value .
  • Count(count) //calculate the count of report times

The sample code is as follows:

    sum := tars.NewSum()
    count := tars.NewCount()
    max := tars.NewMax()
    min := tars.NewMin()
    d := []int{10, 20, 30, 50} 
    distr := tars.NewDistr(d)
    p := tars.CreatePropertyReport("testproperty", sum, count, max, min, distr)
    for i := 0; i < 5; i++ {
        v := rand.Intn(100)
        p.Report(v)

    }   

Description:

  • Data is reported regularly, and can be set in the configuration of the communicator, currently once per minute;
  • Create a PropertyReportPtr function: The parameter createPropertyReport can be any collection of statistical methods, the example uses six statistical methods, usually only need to use one or two;
  • Note that when you call createPropertyReport, you must create and save the created object after the service is enabled, and then just take the object to report, do not create it each time you use.
9 remote configuration

User can setup remote configuration from OSS. See more detail in https://github.com/TarsCloud/TarsFramework/blob/master/docs-en/tars_config.md . That is an example to illustrate how to use this api to get configuration file from remote.

import "github.com/TarsCloud/TarsGo/tars"
...
cfg := tars.GetServerConfig()
remoteConf := tars.NewRConf(cfg.App, cfg.Server, cfg.BasePath)
config, _ := remoteConf.GetConfig("test.conf")

...

10 setting.go

setting.go in package tars is used to control tarsgo performance and characteristics .Some option should be updated from Getserverconfig().

//number of worker routine to handle client request
//zero means  no control ,just one goroutine for a client request.
//runtime.NumCpu() usually best performance in the benchmark.
var MaxInvoke int = 0

const (
	//for now, some option should update from remote config

	//version
	TarsVersion string = "1.0.0"

	//server

	AcceptTimeout time.Duration = 500 * time.Millisecond
	//zero for not set read deadline for Conn (better  performance)
	ReadTimeout time.Duration = 0 * time.Millisecond
	//zero for not set write deadline for Conn (better performance)
	WriteTimeout time.Duration = 0 * time.Millisecond
	//zero for not set deadline for invoke user interface (better performance)
	HandleTimeout  time.Duration = 0 * time.Millisecond
	IdleTimeout    time.Duration = 600000 * time.Millisecond
	ZombileTimeout time.Duration = time.Second * 10
	QueueCap       int           = 10000000

	//client
	ClientQueueLen     int           = 10000
	ClientIdleTimeout  time.Duration = time.Second * 600
	ClientReadTimeout  time.Duration = time.Millisecond * 100
	ClientWriteTimeout time.Duration = time.Millisecond * 3000
	ReqDefaultTimeout  int32         = 3000
	ObjQueueMax        int32         = 10000

	//report
	PropertyReportInterval time.Duration = 10 * time.Second
	StatReportInterval     time.Duration = 10 * time.Second

	//mainloop
	MainLoopTicker time.Duration = 10 * time.Second

	//adapter
	AdapterProxyTicker     time.Duration = 10 * time.Second
	AdapterProxyResetCount int           = 5

	//communicator default ,update from remote config
	refreshEndpointInterval int = 60000
	reportInterval          int = 10000
	AsyncInvokeTimeout      int = 3000

	//tcp network config
	TCPReadBuffer  = 128 * 1024 * 1024
	TCPWriteBuffer = 128 * 1024 * 1024
	TCPNoDelay     = false
)


11 HTTP Support

tars.TarsHttpMux is multiplexer like http.ServeMux,the pattern parameter is used as the interface name in monitoring report.

Here is a sample of http server:

package main

import (
	"net/http"
	"github.com/TarsCloud/TarsGo/tars"
)

func main() {
	mux := &tars.TarsHttpMux{}
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello tafgo"))
	})

    cfg := tars.GetServerConfig()
	tars.AddHttpServant(mux, cfg.App+"."+cfg.Server+".HttpObj") //Register http server
	tars.Run()
}


12 Using Context
context

In the past, TarsGo did not use context in the generated client code, or in the implementation code passed in by the user. This makes us want to pass some framework information, such as client ip, port, etc., or the user passes some information about the call chain to the framework, which is difficult to implement. Through a refactoring of the interface, the context is supported, and the these information will be implemented through the context. This refactoring is designed to be compatible with older user behavior and is fully compatible.

Server use context

type ContextTestImp struct {
}
//only need to add  ctx context.Context parameter
func (imp *ContextTestImp) Add(ctx context.Context, a int32, b int32, c *int32) (int32, error) {
	//We can use context to get some usefull infomation we need ,such Client ,ip ,port and tracing infomation
	//read more detail under tars/util/current
	ip, ok := current.GetClientIPFromContext(ctx)
    if !ok {
        logger.Error("Error getting ip from context")
    }  
	return 0, nil
}
//just change AddServant into AddServantWithContext
app.AddServantWithContext(imp, cfg.App+"."+cfg.Server+".ContextTestObj")

Client use context


    ctx := context.Background()
    c := make(map[string]string)
    c["a"] = "b" 
// juse change app.Add into app.AddWithContext, now u can pass context to framework, 
//if u want to setting request package's context,u can pass a optional parameter ,just like c,which is  ...[string]string
    ret, err := app.AddWithContext(ctx, i, i*2, &out, c)

Read full demo client and server under examples/ContextTestServer

13 filter & zipkin plugin

For supporting writing plugin,we add filter to the framework. We have client filter and server filter.

//ServerFilter ,dispatch and f is passed as parameter , for dispatching user's implement. 
//req and resp is  
type ServerFilter func(ctx context.Context, d Dispatch, f interface{}, req *requestf.RequestPacket, resp *requestf.ResponsePacket, withContext bool) (err error)
//
type ClientFilter func(ctx context.Context, msg *Message, invoke Invoke, timeout time.Duration) (err error)
//RegisterServerFilter registers the server side filter
//func RegisterServerFilter(f ServerFilter)
//RegisterClientFilter registers the client side filter
//func RegisterClientFilter(f ClientFilter)

Having these filters ,now we can add opentracing for every request. Let's take a look at client side filter for opentracing.

//ZipkinClientFilter returns a client side tars filter, for hooking zipking opentracing.
func ZipkinClientFilter() tars.ClientFilter {
	return func(ctx context.Context, msg *tars.Message, invoke tars.Invoke, timeout time.Duration) (err error) {
		var pCtx opentracing.SpanContext
		req := msg.Req
		//If span context is passed in the context, we use this context as parent span ,else start a new span.
		//The method name of the rpc request , is used as span's name .
		if parent := opentracing.SpanFromContext(ctx); parent != nil {
			pCtx = parent.Context()
		}
		cSpan := opentracing.GlobalTracer().StartSpan(
			req.SFuncName,
			opentracing.ChildOf(pCtx),
			ext.SpanKindRPCClient,
		)
		defer cSpan.Finish()
		cfg := tars.GetServerConfig()

		//set additional information for the span ,like method, interface, protocol, vesion, ip and port etc.
		cSpan.SetTag("client.ipv4", cfg.LocalIP)
		cSpan.SetTag("tars.interface", req.SServantName)
		cSpan.SetTag("tars.method", req.SFuncName)
		cSpan.SetTag("tars.protocol", "tars")
		cSpan.SetTag("tars.client.version", tars.TarsVersion)

		//inject the span context into the request package's status ,which is map[string]string
		if req.Status != nil {
			err = opentracing.GlobalTracer().Inject(cSpan.Context(), opentracing.TextMap, opentracing.TextMapCarrier(req.Status))
			if err != nil {
				logger.Error("inject span to status error:", err)
			}
		} else {
			s := make(map[string]string)
			err = opentracing.GlobalTracer().Inject(cSpan.Context(), opentracing.TextMap, opentracing.TextMapCarrier(s))
			if err != nil {
				logger.Error("inject span to status error:", err)
			} else {
				req.Status = s
			}
		}
		//Nothing tho change , just invoke the request.
		err = invoke(ctx, msg, timeout)
		if err != nil {
			//invoke error ,logging the error information to the span.
			ext.Error.Set(cSpan, true)
			cSpan.LogFields(oplog.String("event", "error"), oplog.String("message", err.Error()))
		}

		return err
	}

Server will add a filters ,which exact the span context from the reqeust pacakge's status, and start a new span.

Reqed more under TarsGo/tars/plugin/zipkintracing For client side and server side example code , read ZipkinTraceClient & ZipkinTraceServer under the examples.

Documentation

Index

Constants

View Source
const (
	PACKAGE_LESS = iota
	PACKAGE_FULL
	PACKAGE_ERROR
)
View Source
const (

	//TarsVersion is tars vesion
	TarsVersion string = "1.2.1"

	//AcceptTimeout accept timeout
	AcceptTimeout time.Duration = 500 * time.Millisecond
	//ReadTimeout zero for not set read deadline for Conn (better  performance)
	ReadTimeout time.Duration = 0 * time.Millisecond
	//WriteTimeout zero for not set write deadline for Conn (better performance)
	WriteTimeout time.Duration = 0 * time.Millisecond
	//HandleTimeout zero for not set deadline for invoke user interface (better performance)
	HandleTimeout time.Duration = 0 * time.Millisecond
	//IdleTimeout idle timeout
	IdleTimeout time.Duration = 600000 * time.Millisecond
	//ZombileTimeout zombile timeout
	ZombileTimeout time.Duration = time.Second * 10
	//QueueCap queue gap
	QueueCap int = 10000000

	//ClientQueueLen client queue length
	ClientQueueLen int = 10000
	//ClientIdleTimeout client idle timeout
	ClientIdleTimeout time.Duration = time.Second * 600
	//ClientReadTimeout client read timeout
	ClientReadTimeout time.Duration = time.Millisecond * 100
	//ClientWriteTimeout client write timeout
	ClientWriteTimeout time.Duration = time.Millisecond * 3000
	//ReqDefaultTimeout request default timeout
	ReqDefaultTimeout int32 = 3000
	//ObjQueueMax obj queue max number
	ObjQueueMax int32 = 10000

	//MaxlogOneTime is the max logs for reporting in one time.
	MaxlogOneTime int = 2000

	//PropertyReportInterval property report interval
	PropertyReportInterval time.Duration = 10 * time.Second
	//StatReportInterval stat report interval
	StatReportInterval time.Duration = 10 * time.Second

	//MainLoopTicker main loop ticker
	MainLoopTicker time.Duration = 10 * time.Second

	//AdapterProxyTicker adapter proxy ticker
	AdapterProxyTicker time.Duration = 10 * time.Second
	//AdapterProxyResetCount adapter proxy reset count
	AdapterProxyResetCount int = 5

	//AsyncInvokeTimeout async invoke timeout
	AsyncInvokeTimeout int = 3000

	//TCPReadBuffer tcp read buffer length
	TCPReadBuffer = 128 * 1024 * 1024
	//TCPWriteBuffer tcp write buffer length
	TCPWriteBuffer = 128 * 1024 * 1024
	//TCPNoDelay set tcp no delay
	TCPNoDelay = false
)

Variables

View Source
var MaxInvoke = 0

MaxInvoke number of woker routine to handle client request zero means no contorl ,just one goroutine for a client request. runtime.NumCpu() usually best performance in the benchmark.

View Source
var TLOG = rogger.GetLogger("TLOG")

TLOG is the logger for tars framework.

Functions

func AddHttpServant

func AddHttpServant(mux *TarsHttpMux, obj string)

AddHttpServant add http servant handler for obj.

func AddServant

func AddServant(v dispatch, f interface{}, obj string)

AddServant add dispatch and interface for object.

func AddServantWithContext

func AddServantWithContext(v dispatch, f interface{}, obj string)

AddServantWithContext add dispatch and interface for object, which have ctx,context

func AddServantWithProtocol added in v1.2.4

func AddServantWithProtocol(proto transport.TarsProtoCol, obj string)

AddServantWithProtocol adds a servant with protocol and obj

func GetClientConfig

func GetClientConfig() *clientConfig

GetClientConfig : Get client config

func GetDayLogger

func GetDayLogger(name string, numDay int) *rogger.Logger

GetDayLogger Get a logger roll by day

func GetHourLogger

func GetHourLogger(name string, numHour int) *rogger.Logger

GetHourLogger Get a logger roll by hour

func GetLogger

func GetLogger(name string) *rogger.Logger

GetLogger Get a logger

func GetRemoteLogger

func GetRemoteLogger(name string) *rogger.Logger

GetRemoteLogger returns a remote logger

func GetServerConfig

func GetServerConfig() *serverConfig

GetServerConfig : Get server config

func Init

func Init()

Init should run before GetServerConfig & GetClientConfig , or before run and Init should be only run once

func RegisterAdmin

func RegisterAdmin(name string, fn adminFn)

RegisterAdmin register admin functions

func RegisterClientFilter

func RegisterClientFilter(f ClientFilter)

RegisterClientFilter registers the Client filter , and will be executed in every request.

func RegisterServerFilter

func RegisterServerFilter(f ServerFilter)

RegisterServerFilter register the server filter.

func ReportStat

func ReportStat(msg *Message, succ int32, timeout int32, exec int32)

ReportStat is same as ReportStatFromClient.

func ReportStatBase

func ReportStatBase(head *statf.StatMicMsgHead, body *statf.StatMicMsgBody, FromServer bool)

ReportStatBase is base method for report statitics.

func ReportStatFromClient

func ReportStatFromClient(msg *Message, succ int32, timeout int32, exec int32)

ReportStatFromClient report the statics from client.

func ReportStatFromServer

func ReportStatFromServer(InterfaceName, MasterName string, ReturnValue int32, TotalRspTime int64)

ReportStatFromServer reports statics from server side.

func Run

func Run()

Run the application

func TarsRequest

func TarsRequest(rev []byte) (int, int)

TarsRequest parse full tars request from package

Types

type AdapterProxy

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

AdapterProxy : Adapter proxy

func (*AdapterProxy) Close

func (c *AdapterProxy) Close()

Close : Close the client

func (*AdapterProxy) GetPoint

func (c *AdapterProxy) GetPoint() *endpointf.EndpointF

GetPoint : Get an endpoint

func (*AdapterProxy) New

func (c *AdapterProxy) New(point *endpointf.EndpointF, comm *Communicator) error

New : Construct an adapter proxy

func (*AdapterProxy) ParsePackage

func (c *AdapterProxy) ParsePackage(buff []byte) (int, int)

ParsePackage : Parse packet from bytes

func (*AdapterProxy) Recv

func (c *AdapterProxy) Recv(pkg []byte)

Recv : Recover read channel when closed for timeout

func (*AdapterProxy) Send

func (c *AdapterProxy) Send(req *requestf.RequestPacket) error

Send : Send packet

type Admin

type Admin struct {
}

Admin struct

func (*Admin) Notify

func (a *Admin) Notify(command string) (string, error)

Notify handler for cmds from admin

func (*Admin) Shutdown

func (a *Admin) Shutdown() error

Shutdown shutdown all servant by admin

type Avg

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

Avg for counting average for the report value.

func NewAvg

func NewAvg() *Avg

NewAvg new and init the average struct.

func (*Avg) Desc

func (a *Avg) Desc() string

Desc return the Desc methods for the Avg struct.

func (*Avg) Get

func (a *Avg) Get() (out string)

Get gets the result of the average counting.

func (*Avg) Set

func (a *Avg) Set(in int)

Set sets the value for the average counting.

type ClientFilter

type ClientFilter func(ctx context.Context, msg *Message, invoke Invoke, timeout time.Duration) (err error)

ClientFilter is used for filter request & response for client, for implementing plugins like opentracing

type Communicator

type Communicator struct {
	Client *clientConfig
	// contains filtered or unexported fields
}

Communicator struct

func NewCommunicator

func NewCommunicator() *Communicator

NewCommunicator returns a new communicator. A Communicator is used for communicating with the server side which should only init once and be global!!!

func (*Communicator) GetLocator

func (c *Communicator) GetLocator() string

GetLocator returns locator as string

func (*Communicator) GetProperty

func (c *Communicator) GetProperty(key string) (string, bool)

GetProperty returns communicator property value as string and true for key, or empty string and false for not exists key

func (*Communicator) GetPropertyBool

func (c *Communicator) GetPropertyBool(key string) (bool, bool)

GetPropertyBool returns communicator property value as bool and true for key, or false and false for not exists key

func (*Communicator) GetPropertyInt

func (c *Communicator) GetPropertyInt(key string) (int, bool)

GetPropertyInt returns communicator property value as int and true for key, or 0 and false for not exists key

func (*Communicator) SetLocator

func (c *Communicator) SetLocator(obj string)

SetLocator sets locator with obj

func (*Communicator) SetProperty

func (c *Communicator) SetProperty(key string, value interface{})

SetProperty sets communicator property with a string key and an interface value. var comm *tars.Communicator comm = tars.NewCommunicator() e.g. comm.SetProperty("locator", "tars.tarsregistry.QueryObj@tcp -h ... -p ...")

func (*Communicator) StringToProxy

func (c *Communicator) StringToProxy(servant string, p ProxyPrx)

StringToProxy sets the servant of ProxyPrx p with a string servant

type Count

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

Count is for counting the total of reporting

func NewCount

func NewCount() *Count

NewCount new and init the counting struct.

func (*Count) Desc

func (c *Count) Desc() string

Desc return the Count string.

func (*Count) Get

func (c *Count) Get() (out string)

Get gets the total times of the reporting values.

func (*Count) Set

func (c *Count) Set(in int)

Set sets the value for counting.

type Dispatch

type Dispatch func(context.Context, interface{}, *requestf.RequestPacket, *requestf.ResponsePacket, bool) error

Dispatch server side Dispatch

type Distr

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

Distr is used for counting the distribution of the reporting values.

func NewDistr

func NewDistr(in []int) (d *Distr)

NewDistr new and int the Distr

func (*Distr) Desc

func (d *Distr) Desc() string

Desc return the descrition string of the Distr struct.

func (*Distr) Get

func (d *Distr) Get() string

Get get the distribution of the reporting values.

func (*Distr) Set

func (d *Distr) Set(in int)

Set sets the value for counting distribution.

type EndpointManager

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

EndpointManager is a struct which contains endpoint information.

func (*EndpointManager) GetAllEndpoint

func (e *EndpointManager) GetAllEndpoint() []*endpoint.Endpoint

GetAllEndpoint returns all endpoint information as a array(support not tars service).

func (*EndpointManager) GetHashEndpoint

func (e *EndpointManager) GetHashEndpoint(hashcode int64) *endpoint.Endpoint

GetHashEndpoint returns hash endpoint information.

func (*EndpointManager) GetHashProxy

func (e *EndpointManager) GetHashProxy(hashcode int64) *AdapterProxy

GetHashProxy returns hash adapter information.

func (*EndpointManager) GetNextEndpoint

func (e *EndpointManager) GetNextEndpoint() *endpoint.Endpoint

GetNextEndpoint returns the endpoint basic information.

func (*EndpointManager) GetNextValidProxy

func (e *EndpointManager) GetNextValidProxy() *AdapterProxy

GetNextValidProxy returns polling adapter information.

func (*EndpointManager) Init

func (e *EndpointManager) Init(objName string, comm *Communicator) error

Init endpoint struct.

func (*EndpointManager) SelectAdapterProxy

func (e *EndpointManager) SelectAdapterProxy(msg *Message) *AdapterProxy

SelectAdapterProxy returns selected adapter.

type Invoke

type Invoke func(ctx context.Context, msg *Message, timeout time.Duration) (err error)

Invoke is used for Invoke tars server service

type Max

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

Max struct is for counting the Max value for the reporting value.

func NewMax

func NewMax() *Max

NewMax new and init the Max struct.

func (*Max) Desc

func (m *Max) Desc() string

Desc return the descrition for the Max struct.

func (*Max) Get

func (m *Max) Get() (out string)

Get gets the max value.

func (*Max) Set

func (m *Max) Set(in int)

Set sets a value for counting max.

type Message

type Message struct {
	Req  *requestf.RequestPacket
	Resp *requestf.ResponsePacket

	Obj *ObjectProxy
	Ser *ServantProxy
	Adp *AdapterProxy

	BeginTime int64
	EndTime   int64
	Status    int32
	// contains filtered or unexported fields
}

Message is a struct contains servant information

func (*Message) Cost

func (m *Message) Cost() int64

Cost calculate the cost time

func (*Message) End

func (m *Message) End()

End define the endtime

func (*Message) Init

func (m *Message) Init()

Init define the begintime

func (*Message) SetHashCode

func (m *Message) SetHashCode(code int64)

SetHashCode set hash code

type Min

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

Min is the struct for counting the min value.

func NewMin

func NewMin() *Min

NewMin new and init the min struct.

func (*Min) Desc

func (m *Min) Desc() string

Desc return the descrition string of the Min strcut.

func (*Min) Get

func (m *Min) Get() (out string)

Get get the min value for the Min struct.

func (*Min) Set

func (m *Min) Set(in int)

Set sets a value for counting min value.

type NodeFHelper

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

NodeFHelper is helper struct.

func (*NodeFHelper) KeepAlive

func (n *NodeFHelper) KeepAlive(adapter string)

KeepAlive sends the keepalive pacakage to the node.

func (*NodeFHelper) ReportVersion

func (n *NodeFHelper) ReportVersion(version string)

ReportVersion report the tars version to the node.

func (*NodeFHelper) SetNodeInfo

func (n *NodeFHelper) SetNodeInfo(comm *Communicator, node string, app string, server string)

SetNodeInfo sets the node location for the given Communicator.

type NotifyHelper

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

NotifyHelper is the helper struct for the Notify service.

func (*NotifyHelper) ReportNotifyInfo

func (n *NotifyHelper) ReportNotifyInfo(info string)

ReportNotifyInfo report the info to the notify server.

func (*NotifyHelper) SetNotifyInfo

func (n *NotifyHelper) SetNotifyInfo(comm *Communicator, notify string, app string, server string, container string)

SetNotifyInfo sets the notify server location for the Communicator.

type ObjectProxy

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

ObjectProxy is struct contains proxy information

func (*ObjectProxy) Init

func (obj *ObjectProxy) Init(comm *Communicator, objName string)

Init proxy

func (*ObjectProxy) Invoke

func (obj *ObjectProxy) Invoke(ctx context.Context, msg *Message, timeout time.Duration) error

Invoke get proxy information

func (*ObjectProxy) TarsSetProtocol

func (obj *ObjectProxy) TarsSetProtocol(proto model.Protocol)

type ObjectProxyFactory

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

ObjectProxyFactory is a struct contains proxy information(add)

func (*ObjectProxyFactory) GetObjectProxy

func (o *ObjectProxyFactory) GetObjectProxy(objName string) *ObjectProxy

GetObjectProxy get objectproxy

func (*ObjectProxyFactory) Init

func (o *ObjectProxyFactory) Init(comm *Communicator)

Init ObjectProxyFactory

type PropertyReport

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

PropertyReport struct.

func CreatePropertyReport

func CreatePropertyReport(key string, argvs ...ReportMethod) (ptr *PropertyReport)

CreatePropertyReport creats the property report instance with the key.

func (*PropertyReport) Report

func (p *PropertyReport) Report(in int)

Report reports a value.

type PropertyReportHelper

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

PropertyReportHelper is helper struct for property report.

var ProHelper *PropertyReportHelper

ProHelper is global.

func (*PropertyReportHelper) AddToReport

func (p *PropertyReportHelper) AddToReport(pr *PropertyReport)

AddToReport adds the user's PropertyReport to the PropertyReportHelper

func (*PropertyReportHelper) Init

func (p *PropertyReportHelper) Init(comm *Communicator, node string)

Init inits the PropertyReportHelper

func (*PropertyReportHelper) ReportToServer

func (p *PropertyReportHelper) ReportToServer()

ReportToServer report to the remote propertyreport server.

func (*PropertyReportHelper) Run

func (p *PropertyReportHelper) Run()

Run start the properting report goroutine.

type ProxyPrx

type ProxyPrx interface {
	SetServant(s.Servant)
}

ProxyPrx interface

type RConf

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

RConf struct for geting remote config.

func NewRConf

func NewRConf(app string, server string, path string) *RConf

NewRConf init a Rconf, path should be getting from GetServerConfig().BasePath

func (*RConf) GetConfig

func (c *RConf) GetConfig(filename string) (config string, err error)

GetConfig gets the remote config and save it to the path, also return the content.

func (*RConf) GetConfigList

func (c *RConf) GetConfigList() (flist []string, err error)

GetConfigList is discarded.

type RemoteTimeWriter

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

RemoteTimeWriter writer for writing remote log.

func NewRemoteTimeWriter

func NewRemoteTimeWriter() *RemoteTimeWriter

NewRemoteTimeWriter new and init RemoteTimeWriter

func (*RemoteTimeWriter) EnablePrefix

func (rw *RemoteTimeWriter) EnablePrefix(hasAppNamePrefix bool)

EnablePrefix puts prefix before logs.

func (*RemoteTimeWriter) EnableSqarewrapper

func (rw *RemoteTimeWriter) EnableSqarewrapper(hasSquareBracket bool)

EnableSqarewrapper enables SquareBracket wrapper for the logs.

func (*RemoteTimeWriter) EnableSufix

func (rw *RemoteTimeWriter) EnableSufix(hasSufix bool)

EnableSufix puts sufix after logs.

func (*RemoteTimeWriter) InitFormat

func (rw *RemoteTimeWriter) InitFormat(s string)

InitFormat sets the log format.

func (*RemoteTimeWriter) InitServerInfo

func (rw *RemoteTimeWriter) InitServerInfo(app string, server string, filename string, setdivision string)

InitServerInfo init the remote log server info.

func (*RemoteTimeWriter) NeedPrefix

func (rw *RemoteTimeWriter) NeedPrefix() bool

NeedPrefix return if need prefix for the logger.

func (*RemoteTimeWriter) SetFileNameConcatStr

func (rw *RemoteTimeWriter) SetFileNameConcatStr(s string)

SetFileNameConcatStr sets the filename concat string.

func (*RemoteTimeWriter) SetLogType

func (rw *RemoteTimeWriter) SetLogType(logType string)

SetLogType sets the log type.

func (*RemoteTimeWriter) SetPrefix

func (rw *RemoteTimeWriter) SetPrefix(enable bool)

func (*RemoteTimeWriter) SetSeparator

func (rw *RemoteTimeWriter) SetSeparator(s string)

SetSeparator set seprator between logs.

func (*RemoteTimeWriter) Sync2remote

func (rw *RemoteTimeWriter) Sync2remote()

Sync2remote syncs the log buffer to remote.

func (*RemoteTimeWriter) Write

func (rw *RemoteTimeWriter) Write(b []byte)

Write Writes the logs to the buffer.

type ReportMethod

type ReportMethod interface {
	Desc() string
	Set(int)
	Get() string
	// contains filtered or unexported methods
}

ReportMethod is the interface for all kinds of report methods.

type ServantProxy

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

ServantProxy is the struct for proxy servants.

func Dail

func Dail(servant string) *ServantProxy

Dail returns servant proxy

func (*ServantProxy) Init

func (s *ServantProxy) Init(comm *Communicator, objName string)

Init init the ServantProxy struct.

func (*ServantProxy) TarsSetProtocol

func (s *ServantProxy) TarsSetProtocol(proto model.Protocol)

TarsSetProtocol tars set model protocol

func (*ServantProxy) TarsSetTimeout

func (s *ServantProxy) TarsSetTimeout(t int)

TarsSetTimeout sets the timeout for client calling the server , which is in ms.

func (*ServantProxy) Tars_invoke

func (s *ServantProxy) Tars_invoke(ctx context.Context, ctype byte,
	sFuncName string,
	buf []byte,
	status map[string]string,
	reqContext map[string]string,
	Resp *requestf.ResponsePacket) error

Tars_invoke is use for client inoking server.

type ServantProxyFactory

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

ServantProxyFactory is ServantProxy' factory struct.

func (*ServantProxyFactory) GetServantProxy

func (o *ServantProxyFactory) GetServantProxy(objName string) *ServantProxy

GetServantProxy gets the ServanrProxy for the object.

func (*ServantProxyFactory) Init

func (o *ServantProxyFactory) Init(comm *Communicator)

Init init the ServantProxyFactory.

type ServerFilter

type ServerFilter func(ctx context.Context, d Dispatch, f interface{}, req *requestf.RequestPacket, resp *requestf.ResponsePacket, withContext bool) (err error)

ServerFilter is used for add Filter for server dispatcher ,for implementing plugins like opentracing.

type StatFHelper

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

StatFHelper is helper struct for stat reporting.

var StatReport *StatFHelper

StatReport is global.

func (*StatFHelper) Init

func (s *StatFHelper) Init(comm *Communicator, node string)

Init init the StatFHelper.

func (*StatFHelper) ReportMicMsg

func (s *StatFHelper) ReportMicMsg(stStatInfo StatInfo, fromServer bool)

ReportMicMsg report the Statinfo ,from server shows whether it comes from server.

func (*StatFHelper) Run

func (s *StatFHelper) Run()

Run runs the reporting

type StatInfo

type StatInfo struct {
	Head statf.StatMicMsgHead
	Body statf.StatMicMsgBody
}

StatInfo struct contains stat info' head and body.

type Sum

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

Sum report methods.

func NewSum

func NewSum() *Sum

NewSum new and init the sum report methods.

func (*Sum) Desc

func (s *Sum) Desc() string

Desc return the descriptor string for the sum method.

func (*Sum) Get

func (s *Sum) Get() (out string)

Get gets the result of the sum report method.

func (*Sum) Set

func (s *Sum) Set(in int)

Set sets a value tho the sum method.

type TarsHttpConf

type TarsHttpConf struct {
	Container string
	AppName   string
	IP        string
	Port      int32
	Version   string
	SetId     string
}

TarsHttpConf is configuration for tars http server.

type TarsHttpMux

type TarsHttpMux struct {
	http.ServeMux
	// contains filtered or unexported fields
}

TarsHttpMux is http.ServeMux for tars http server.

func (*TarsHttpMux) ServeHTTP

func (mux *TarsHttpMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the server for the TarsHttpMux.

func (*TarsHttpMux) SetConfig

func (mux *TarsHttpMux) SetConfig(cfg *TarsHttpConf)

SetConfig sets the cfg tho the TarsHttpMux.

type TarsProtocol

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

TarsProtocol is struct for dispatch with tars protocol.

func NewTarsProtocol

func NewTarsProtocol(dispatcher dispatch, imp interface{}, withContext bool) *TarsProtocol

NewTarsProtocol return a Tarsprotocol with dipatcher and implement interface. withContext explain using context or not.

func (*TarsProtocol) Invoke

func (s *TarsProtocol) Invoke(ctx context.Context, req []byte) (rsp []byte)

Invoke puts the request as []byte and call the dispather, and then return the response as []byte.

func (*TarsProtocol) InvokeTimeout

func (s *TarsProtocol) InvokeTimeout(pkg []byte) []byte

InvokeTimeout indicates how to deal with timeout.

func (*TarsProtocol) ParsePackage

func (s *TarsProtocol) ParsePackage(buff []byte) (int, int)

ParsePackage parse the []byte according to the tars protocol.

type TarsResponseWriter

type TarsResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

TarsResponseWriter is http.ResponseWriter for tars.

func (*TarsResponseWriter) WriteHeader

func (w *TarsResponseWriter) WriteHeader(code int)

WriteHeader is used for write the http header with the http code.

Directories

Path Synopsis
examples
plugin
res/adminf
Package adminf comment This file war generated by tars2go 1.1 Generated from AdminF.tars
Package adminf comment This file war generated by tars2go 1.1 Generated from AdminF.tars
res/basef
Package basef comment This file war generated by tars2go 1.1 Generated from BaseF.tars
Package basef comment This file war generated by tars2go 1.1 Generated from BaseF.tars
res/configf
Package configf comment This file war generated by tars2go 1.1 Generated from ConfigF.tars Package configf comment This file war generated by tars2go 1.1 Generated from ConfigF.tars Package configf comment This file war generated by tars2go 1.1 Generated from ConfigF.tars
Package configf comment This file war generated by tars2go 1.1 Generated from ConfigF.tars Package configf comment This file war generated by tars2go 1.1 Generated from ConfigF.tars Package configf comment This file war generated by tars2go 1.1 Generated from ConfigF.tars
res/endpointf
Package endpointf comment This file war generated by tars2go 1.1 Generated from EndpointF.tars
Package endpointf comment This file war generated by tars2go 1.1 Generated from EndpointF.tars
res/logf
Package logf comment This file war generated by tars2go 1.1 Generated from LogF.tars Package logf comment This file war generated by tars2go 1.1 Generated from LogF.tars
Package logf comment This file war generated by tars2go 1.1 Generated from LogF.tars Package logf comment This file war generated by tars2go 1.1 Generated from LogF.tars
res/nodef
Package nodef comment This file war generated by tars2go 1.1 Generated from NodeF.tars Package nodef comment This file war generated by tars2go 1.1 Generated from NodeF.tars
Package nodef comment This file war generated by tars2go 1.1 Generated from NodeF.tars Package nodef comment This file war generated by tars2go 1.1 Generated from NodeF.tars
res/notifyf
Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars
Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars Package notifyf comment This file war generated by tars2go 1.1 Generated from NotifyF.tars
res/propertyf
Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars
Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars Package propertyf comment This file war generated by tars2go 1.1 Generated from PropertyF.tars
res/queryf
Package queryf comment This file war generated by tars2go 1.1 Generated from QueryF.tars
Package queryf comment This file war generated by tars2go 1.1 Generated from QueryF.tars
res/requestf
Package requestf comment This file war generated by tars2go 1.1 Generated from RequestF.tars Package requestf comment This file war generated by tars2go 1.1 Generated from RequestF.tars
Package requestf comment This file war generated by tars2go 1.1 Generated from RequestF.tars Package requestf comment This file war generated by tars2go 1.1 Generated from RequestF.tars
res/statf
Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars
Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars Package statf comment This file war generated by tars2go 1.1 Generated from StatF.tars
tools
pb2tarsgo/protoc-gen-go/tarsrpc
Package tarsrpc outputs tars service descriptions in Go code.
Package tarsrpc outputs tars service descriptions in Go code.
util
conf
Package conf implements parse the taf config.
Package conf implements parse the taf config.
set
Package set implement
Package set implement

Jump to

Keyboard shortcuts

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