async

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2019 License: GPL-3.0 Imports: 8 Imported by: 0

README

async

This service transforms a source of Go function(method) into a state-machine by:

  • Creating a struct where all function local variables become struct fields
  • Rewriting control flow into a struct tree, so that we can jump to any statement we want

This allows us to write statemachines in Go with

  • Ease of writing and reading Go code
  • Ability to save, dump, modify & resume it's state
  • Type-safety of the code before & after transformation

See test folder for more info on how it works and how to use it.

The idea for this service is to leverage https://gitlab.com/albert_einstein/slct service to build asyncronous Go statemachine runtime.

Limitations:

  • Only pure structs are currently supported
  • You have to put '// :' comment after select statement and calls to other async functions. Theese serve as resume label for engine to know where to resume the process. For example "order.update" tells engine that the process should be resumed at function iwth "order" label and inside the function at label "update"
  • The generation is pretty brittle - lots of features are not supported, so see test folder on what you can currently do.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Callback_CallbackType_name = map[int32]string{
	0: "Invalid",
	1: "HTTP",
}
View Source
var Callback_CallbackType_value = map[string]int32{
	"Invalid": 0,
	"HTTP":    1,
}
View Source
var Case_CaseOp_name = map[int32]string{
	0: "Invalid",
	1: "Recv",
	2: "Send",
	3: "Time",
	4: "Close",
	5: "Default",
}
View Source
var Case_CaseOp_value = map[string]int32{
	"Invalid": 0,
	"Recv":    1,
	"Send":    2,
	"Time":    3,
	"Close":   4,
	"Default": 5,
}
View Source
var Result_ResultType_name = map[int32]string{
	0: "Invalid",
	1: "OK",
	2: "Closed",
}
View Source
var Result_ResultType_value = map[string]int32{
	"Invalid": 0,
	"OK":      1,
	"Closed":  2,
}
View Source
var Status_name = map[int32]string{
	0: "Invalid",
	1: "Waiting",
	2: "Callback_Pending",
	3: "Callback_Failed",
	4: "Callback_Skipped",
}
View Source
var Status_value = map[string]int32{
	"Invalid":          0,
	"Waiting":          1,
	"Callback_Pending": 2,
	"Callback_Failed":  3,
	"Callback_Skipped": 4,
}

Functions

func RunProcess added in v0.1.2

func RunProcess(s *State, p Process) error

Types

type Callback

type Callback struct {
	Type                 Callback_CallbackType `protobuf:"varint,1,opt,name=type,proto3,enum=async.Callback_CallbackType" json:"type,omitempty"`
	Uri                  string                `protobuf:"bytes,2,opt,name=uri,proto3" json:"uri,omitempty"`
	XXX_NoUnkeyedLiteral struct{}              `json:"-"`
	XXX_unrecognized     []byte                `json:"-"`
	XXX_sizecache        int32                 `json:"-"`
}

func (*Callback) Descriptor

func (*Callback) Descriptor() ([]byte, []int)

func (*Callback) GetType

func (m *Callback) GetType() Callback_CallbackType

func (*Callback) GetUri

func (m *Callback) GetUri() string

func (*Callback) ProtoMessage

func (*Callback) ProtoMessage()

func (*Callback) Reset

func (m *Callback) Reset()

func (*Callback) String

func (m *Callback) String() string

func (*Callback) XXX_DiscardUnknown

func (m *Callback) XXX_DiscardUnknown()

func (*Callback) XXX_Marshal

func (m *Callback) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Callback) XXX_Merge

func (dst *Callback) XXX_Merge(src proto.Message)

func (*Callback) XXX_Size

func (m *Callback) XXX_Size() int

func (*Callback) XXX_Unmarshal

func (m *Callback) XXX_Unmarshal(b []byte) error

type Callback_CallbackType

type Callback_CallbackType int32
const (
	Callback_Invalid Callback_CallbackType = 0
	Callback_HTTP    Callback_CallbackType = 1
)

func (Callback_CallbackType) EnumDescriptor

func (Callback_CallbackType) EnumDescriptor() ([]byte, []int)

func (Callback_CallbackType) String

func (x Callback_CallbackType) String() string

type Case

type Case struct {
	Op                   Case_CaseOp `protobuf:"varint,1,opt,name=op,proto3,enum=async.Case_CaseOp" json:"op,omitempty"`
	Index                int64       `protobuf:"varint,2,opt,name=index,proto3" json:"index,omitempty"`
	Chan                 string      `protobuf:"bytes,3,opt,name=chan,proto3" json:"chan,omitempty"`
	Time                 int64       `protobuf:"varint,4,opt,name=time,proto3" json:"time,omitempty"`
	Data                 []byte      `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
	XXX_NoUnkeyedLiteral struct{}    `json:"-"`
	XXX_unrecognized     []byte      `json:"-"`
	XXX_sizecache        int32       `json:"-"`
}

func (*Case) Descriptor

func (*Case) Descriptor() ([]byte, []int)

func (*Case) GetChan

func (m *Case) GetChan() string

func (*Case) GetData

func (m *Case) GetData() []byte

func (*Case) GetIndex

func (m *Case) GetIndex() int64

func (*Case) GetOp

func (m *Case) GetOp() Case_CaseOp

func (*Case) GetTime

func (m *Case) GetTime() int64

func (*Case) ProtoMessage

func (*Case) ProtoMessage()

func (*Case) Reset

func (m *Case) Reset()

func (*Case) String

func (m *Case) String() string

func (*Case) XXX_DiscardUnknown

func (m *Case) XXX_DiscardUnknown()

func (*Case) XXX_Marshal

func (m *Case) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Case) XXX_Merge

func (dst *Case) XXX_Merge(src proto.Message)

func (*Case) XXX_Size

func (m *Case) XXX_Size() int

func (*Case) XXX_Unmarshal

func (m *Case) XXX_Unmarshal(b []byte) error

type Case_CaseOp

type Case_CaseOp int32
const (
	Case_Invalid Case_CaseOp = 0
	Case_Recv    Case_CaseOp = 1
	Case_Send    Case_CaseOp = 2
	Case_Time    Case_CaseOp = 3
	Case_Close   Case_CaseOp = 4
	Case_Default Case_CaseOp = 5
)

func (Case_CaseOp) EnumDescriptor

func (Case_CaseOp) EnumDescriptor() ([]byte, []int)

func (Case_CaseOp) String

func (x Case_CaseOp) String() string

type Channel

type Channel interface {
	ChannelName() string
}

type Engine

type Engine struct {
	Resume    *Result
	Running   bool
	Pause     *Select
	Marshal   func(interface{}) ([]byte, error)
	Unmarshal func([]byte, interface{}) error
	Break     bool
	Continue  bool
}

func (*Engine) Run

func (e *Engine) Run(ss []Stmt) (s *Select, err error)

type ForLoop

type ForLoop struct {
	Cond  func() bool
	Init  func()
	Post  func()
	Stmts []Stmt
}

type Func

type Func interface {
	Source() []Stmt
}

type Process added in v0.1.2

type Process interface {
	Source() []Stmt
}

type Result

type Result struct {
	Type                 Result_ResultType `protobuf:"varint,1,opt,name=type,proto3,enum=async.Result_ResultType" json:"type,omitempty"`
	SelectID             string            `protobuf:"bytes,2,opt,name=selectID,proto3" json:"selectID,omitempty"`
	StateID              string            `protobuf:"bytes,3,opt,name=stateID,proto3" json:"stateID,omitempty"`
	CaseIndex            int64             `protobuf:"varint,4,opt,name=caseIndex,proto3" json:"caseIndex,omitempty"`
	Time                 int64             `protobuf:"varint,5,opt,name=time,proto3" json:"time,omitempty"`
	SelectSeq            int64             `protobuf:"varint,6,opt,name=selectSeq,proto3" json:"selectSeq,omitempty"`
	Data                 []byte            `protobuf:"bytes,7,opt,name=data,proto3" json:"data,omitempty"`
	XXX_NoUnkeyedLiteral struct{}          `json:"-"`
	XXX_unrecognized     []byte            `json:"-"`
	XXX_sizecache        int32             `json:"-"`
}

func (*Result) Descriptor

func (*Result) Descriptor() ([]byte, []int)

func (*Result) GetCaseIndex

func (m *Result) GetCaseIndex() int64

func (*Result) GetData

func (m *Result) GetData() []byte

func (*Result) GetSelectID

func (m *Result) GetSelectID() string

func (*Result) GetSelectSeq

func (m *Result) GetSelectSeq() int64

func (*Result) GetStateID

func (m *Result) GetStateID() string

func (*Result) GetTime

func (m *Result) GetTime() int64

func (*Result) GetType

func (m *Result) GetType() Result_ResultType

func (*Result) ProtoMessage

func (*Result) ProtoMessage()

func (*Result) Reset

func (m *Result) Reset()

func (*Result) String

func (m *Result) String() string

func (*Result) XXX_DiscardUnknown

func (m *Result) XXX_DiscardUnknown()

func (*Result) XXX_Marshal

func (m *Result) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Result) XXX_Merge

func (dst *Result) XXX_Merge(src proto.Message)

func (*Result) XXX_Size

func (m *Result) XXX_Size() int

func (*Result) XXX_Unmarshal

func (m *Result) XXX_Unmarshal(b []byte) error

type Result_ResultType

type Result_ResultType int32
const (
	Result_Invalid Result_ResultType = 0
	Result_OK      Result_ResultType = 1
	Result_Closed  Result_ResultType = 2
)

func (Result_ResultType) EnumDescriptor

func (Result_ResultType) EnumDescriptor() ([]byte, []int)

func (Result_ResultType) String

func (x Result_ResultType) String() string

type SelCase

type SelCase struct {
	Op    Case_CaseOp
	Send  func() interface{}
	Dst   func() []interface{}
	Chan  func() Channel
	Stmts []Stmt
}

type Select

type Select struct {
	SelID                string   `protobuf:"bytes,1,opt,name=selID,proto3" json:"selID,omitempty"`
	StateID              string   `protobuf:"bytes,2,opt,name=stateID,proto3" json:"stateID,omitempty"`
	Result               *Result  `protobuf:"bytes,5,opt,name=result,proto3" json:"result,omitempty"`
	Cases                []*Case  `protobuf:"bytes,3,rep,name=cases,proto3" json:"cases,omitempty"`
	Seq                  int64    `protobuf:"varint,4,opt,name=seq,proto3" json:"seq,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func Run

func Run(ss []Stmt, res *Result) (s *Select, err error)

func (*Select) Descriptor

func (*Select) Descriptor() ([]byte, []int)

func (*Select) GetCases

func (m *Select) GetCases() []*Case

func (*Select) GetResult

func (m *Select) GetResult() *Result

func (*Select) GetSelID

func (m *Select) GetSelID() string

func (*Select) GetSeq

func (m *Select) GetSeq() int64

func (*Select) GetStateID

func (m *Select) GetStateID() string

func (*Select) ProtoMessage

func (*Select) ProtoMessage()

func (*Select) Reset

func (m *Select) Reset()

func (*Select) String

func (m *Select) String() string

func (*Select) XXX_DiscardUnknown

func (m *Select) XXX_DiscardUnknown()

func (*Select) XXX_Marshal

func (m *Select) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Select) XXX_Merge

func (dst *Select) XXX_Merge(src proto.Message)

func (*Select) XXX_Size

func (m *Select) XXX_Size() int

func (*Select) XXX_Unmarshal

func (m *Select) XXX_Unmarshal(b []byte) error

type State

type State struct {
	ID                   string    `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
	Status               Status    `protobuf:"varint,2,opt,name=status,proto3,enum=async.Status" json:"status,omitempty"`
	Selects              []*Select `protobuf:"bytes,3,rep,name=selects,proto3" json:"selects,omitempty"`
	Data                 []byte    `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
	Callback             *Callback `protobuf:"bytes,6,opt,name=callback,proto3" json:"callback,omitempty"`
	RaftSeq              uint64    `protobuf:"varint,7,opt,name=raftSeq,proto3" json:"raftSeq,omitempty"`
	Lock                 string    `protobuf:"bytes,8,opt,name=lock,proto3" json:"lock,omitempty"`
	LockTill             int64     `protobuf:"varint,9,opt,name=lockTill,proto3" json:"lockTill,omitempty"`
	XXX_NoUnkeyedLiteral struct{}  `json:"-"`
	XXX_unrecognized     []byte    `json:"-"`
	XXX_sizecache        int32     `json:"-"`
}

func (*State) Descriptor

func (*State) Descriptor() ([]byte, []int)

func (*State) GetCallback

func (m *State) GetCallback() *Callback

func (*State) GetData

func (m *State) GetData() []byte

func (*State) GetID

func (m *State) GetID() string

func (*State) GetLock

func (m *State) GetLock() string

func (*State) GetLockTill

func (m *State) GetLockTill() int64

func (*State) GetRaftSeq

func (m *State) GetRaftSeq() uint64

func (*State) GetSelects

func (m *State) GetSelects() []*Select

func (*State) GetStatus

func (m *State) GetStatus() Status

func (*State) ProtoMessage

func (*State) ProtoMessage()

func (*State) Reset

func (m *State) Reset()

func (*State) String

func (m *State) String() string

func (*State) XXX_DiscardUnknown

func (m *State) XXX_DiscardUnknown()

func (*State) XXX_Marshal

func (m *State) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*State) XXX_Merge

func (dst *State) XXX_Merge(src proto.Message)

func (*State) XXX_Size

func (m *State) XXX_Size() int

func (*State) XXX_Unmarshal

func (m *State) XXX_Unmarshal(b []byte) error

type Status

type Status int32
const (
	Status_Invalid          Status = 0
	Status_Waiting          Status = 1
	Status_Callback_Pending Status = 2
	Status_Callback_Failed  Status = 3
	Status_Callback_Skipped Status = 4
)

func (Status) EnumDescriptor

func (Status) EnumDescriptor() ([]byte, []int)

func (Status) String

func (x Status) String() string

type Stmt

type Stmt struct {
	Name      string
	Switch    []SwCase
	Select    []SelCase
	For       *ForLoop
	Do        func()
	InitAsync func()
	Async     func() Func
	PostAsync func()
	Return    func()
	Break     bool
	Continue  bool
}

type SwCase

type SwCase struct {
	Cond  func() bool
	Stmts []Stmt
}

type TimeChannel added in v0.1.1

type TimeChannel struct {
	Time int64
	C    chan struct{}
}

func After

func After(t time.Duration) TimeChannel

func (TimeChannel) ChannelName added in v0.1.1

func (t TimeChannel) ChannelName() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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