client_go

package module
v0.0.0-...-5a30137 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

easycar client for go

install

go get -u github.com/easycar/client-go

How to use

package main

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

	client "github.com/easycar/client-go"
)

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.AddGroup(false, groups...).Register(ctx); err != nil {
		log.Fatal(err)
	}
	// Trigger the execution of this distributed transaction
	if err := cli.Start(ctx); err != nil {
		fmt.Println("start err:", err)
	}
	fmt.Println("end gid:", gid)
}

for more examples see here:examples

Documentation

Index

Constants

View Source
const (
	TransactionUnknown TranType = "unknown"
	TCC                TranType = "tcc"
	SAGA               TranType = "saga"

	ActionUnknown        = "unknown"
	Try           Action = "try"
	Confirm       Action = "confirm"
	Cancel        Action = "cancel"

	// Normal and Compensation branch type for SAGA
	Normal       Action = "normal"
	Compensation Action = "compensation"
)

Variables

View Source
var DefaultOptions = &Options{
	connTimeout: 15 * time.Second,
}

DefaultOptions default for a Client Options

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

Functions

func ConvertBranchActionToGrpc

func ConvertBranchActionToGrpc(action Action) proto.Action

ConvertBranchActionToGrpc Convert action to pb.action

func ConvertTranTypeToGrpc

func ConvertTranTypeToGrpc(tranType TranType) proto.TranType

ConvertTranTypeToGrpc Convert tranType to pb.TranType

Types

type Action

type Action consts.BranchAction

type Branch

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

func NewBranch

func NewBranch(uri string, action Action) *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 Level)

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 easycar client

func New

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

func (*Client) AddGroup

func (client *Client) AddGroup(skip bool, groups ...*Group) *Client

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) Register

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

func (*Client) SetGid

func (client *Client) SetGid(gid string) *Client

func (*Client) Start

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

type Group

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

func NewSagaGroup

func NewSagaGroup(normalUri, compensation string) *Group

NewSagaGroup create a set of branches for Saga mode

func NewTccGroup

func NewTccGroup(tryUri, confirmUri, cancelUri string) *Group

NewTccGroup create a set of branches for TCC mode

func (*Group) GetTranType

func (g *Group) GetTranType() TranType

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 Level) *Group

func (*Group) SetTimeout

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

type Level

type Level consts.Level

type Option

type Option func(options *Options)

func WithConnTimeout

func WithConnTimeout(seconds time.Duration) Option

func WithDiscovery

func WithDiscovery(discovery registry.Discovery) Option

func WithGrpcDailOpts

func WithGrpcDailOpts(opts []grpc.DialOption) Option

func WithTactics

func WithTactics(name balancer.TacticsName) 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"
)

type TranType

type TranType consts.TransactionType

Jump to

Keyboard shortcuts

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