client

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 18 Imported by: 1

README

easycar client for go

How to use

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/wuqinqiang/easycar/client"
)

func main() {
	var opts []client.Option
	opts = append(opts, client.WithConnTimeout(5*time.Second))

	// new an easycar client by server uri
	cli, err := client.New("server Url", opts...)
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()

	// begin and get gid
	gid, err := cli.Begin(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Begin gid:", gid)

	var (
		groups []*client.Group
	)
	// register  branches to easycar service (tc)
	if err = cli.Register(ctx,gid,groups); err != nil {
		log.Fatal(err)
	}
	// Trigger the execution of this distributed transaction
	if err := cli.Start(ctx,gid); err != nil {
		fmt.Println("start err:", err)
	}
	fmt.Println("end gid:", gid)
}

for more examples see here:examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = &Options{
	connTimeout: 15 * time.Second,
	dailOpts: []grpc.DialOption{

		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"easycarBalancer":{}}]}`),
	},
}

DefaultOptions default for a Client Options

View Source
var (
	ErrEmpty = errors.New("The Group cannot be empty")
)

Functions

func BuildDirectTarget

func BuildDirectTarget(uri string) string

BuildDirectTarget covert uri to direct target uri:127.0.0.1:8089,127.0.0.1:8085 => direct:///127.0.0.1:8089,127.0.0.1:8085

func BuildDiscoveryTarget

func BuildDiscoveryTarget(uri string) string

BuildDiscoveryTarget covert uri to discovery target

func RegisterBalancerWithAlgorithm

func RegisterBalancerWithAlgorithm(name balancer.Algorithm)

RegisterBalancerWithAlgorithm register grpc balancer with algorithm

func RegisterBuilder

func RegisterBuilder(discovery registry.Discovery)

RegisterBuilder register Builder with discovery

Types

type Branch

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

func NewBranch

func NewBranch(uri string, action consts.BranchAction) *Branch

func (*Branch) Convert

func (branch *Branch) Convert() *proto.RegisterReq_Branch

Convert from client's Branch to pb.RegisterReq_Branch

func (*Branch) SetData

func (branch *Branch) SetData(data []byte) *Branch

SetData set branch request data

func (*Branch) SetHeader

func (branch *Branch) SetHeader(header []byte) *Branch

SetHeader set branch header

func (*Branch) SetLevel

func (branch *Branch) SetLevel(level consts.Level) *Branch

SetLevel set branch currentLevel

func (*Branch) SetProtocol

func (branch *Branch) SetProtocol(protocol Protocol) *Branch

SetProtocol set branch network protocol

func (*Branch) SetTimeout

func (branch *Branch) SetTimeout(timeout int64) *Branch

SetTimeout set timeout for request branch

type Client

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

Client client-go

func New

func New(uri string, options ...Option) (client *Client, err error)

func (*Client) Begin

func (client *Client) Begin(ctx context.Context) (gid string, err error)

func (*Client) Close

func (client *Client) Close(ctx context.Context) error

func (*Client) Commit

func (client *Client) Commit(ctx context.Context, gid string) error

func (*Client) Register

func (client *Client) Register(ctx context.Context, gid string, groups []*Group) error

func (*Client) Rollback

func (client *Client) Rollback(ctx context.Context, gid string) error

func (*Client) Start

func (client *Client) Start(ctx context.Context, gid string) (err error)

type Group

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

func NewSagaGroup

func NewSagaGroup(normalUri, compensation string, opts ...GroupOpt) *Group

NewSagaGroup create a set of branches for Saga mode

func NewTccGroup

func NewTccGroup(tryUri, confirmUri, cancelUri string, opts ...GroupOpt) *Group

NewTccGroup create a set of branches for TCC mode

func (*Group) GetTranType

func (g *Group) GetTranType() consts.TransactionType

func (*Group) SetData

func (g *Group) SetData(data []byte) *Group

func (*Group) SetHeader

func (g *Group) SetHeader(data []byte) *Group

func (*Group) SetLevel

func (g *Group) SetLevel(level consts.Level) *Group

func (*Group) SetTimeout

func (g *Group) SetTimeout(second int) *Group

type GroupOpt

type GroupOpt func(group *Group)

func LevelFixed

func LevelFixed() GroupOpt

type HandlerFn

type HandlerFn func(ctx context.Context) error

type Manger

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

func NewManger

func NewManger() *Manger

func (*Manger) AddGroups

func (m *Manger) AddGroups(groups ...*Group) *Manger

AddGroups Add Groups as normal

func (*Manger) AddNextWaitGroups

func (m *Manger) AddNextWaitGroups(groups ...*Group) *Manger

AddNextWaitGroups The group added this time will be executed after the completion of the call to the group added by the previous level call to AddNextWaitGroup example:m.AddGroups(group1,group2).AddNextWaitGroups(group3).AddGroups(group4),that means: first concurrent execution group1 and group2,then execution the group3,finally execution the group4

func (*Manger) Groups

func (m *Manger) Groups() []*Group

type Option

type Option func(options *Options)

func WithAfterFunc

func WithAfterFunc(after HandlerFn) Option

func WithBeforeFunc

func WithBeforeFunc(before HandlerFn) Option

func WithConnTimeout

func WithConnTimeout(seconds time.Duration) Option

func WithDiscovery

func WithDiscovery() Option

func WithGrpcDailOpts

func WithGrpcDailOpts(opts []grpc.DialOption) Option

func WithTls

func WithTls(tls *tls.Config) Option

type Options

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

type Protocol

type Protocol string
const (
	HTTP      Protocol = "http"
	GRPC      Protocol = "grpc"
	Undefined Protocol = "undefined"
)

Jump to

Keyboard shortcuts

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