Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta committed Jan 11, 2024
0 parents commit 41b5cda
Show file tree
Hide file tree
Showing 27 changed files with 5,768 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*.go]
indent_style = tab
indent_size = 2
max_line_length = 80

[*.md]
indent_style = space
indent_size = 4
max_line_length = 120

[*.{yml|yaml}]
indent_style = space
indent_size = 4
max_line_length = 120
22 changes: 22 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Go

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21' ]

steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install dependencies
run: go mod tidy
- name: Test
run: go test ./...
27 changes: 27 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Linter

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21' ]

steps:
- name: Install Task
uses: arduino/setup-task@v1
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
with:
ruby-version: '3.1'
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install dependencies
run: task install-deps
- name: Linter
run: task lint
48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Jetbrains IDEs
.idea

# Vim
*.swp
*.swn
*.swo

# MacOS system file
.DS_Store

# Environment variable files
*.env

# PEM files
*.pem

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Binaries compiled by Go
build/

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool
*.new
*.old
*.out

# Dependency directories
vendor/

# Redis
*.rdb

# Go workspace file
go.work

# local dev
/.dev
/pkg/debugger
/docs/cookbook.md
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
run:
skip-files:
- "examples/.*"
- "pkg/debugger/.*"

linters:
enable:
- lll

linters-settings:
lll:
line-length: 80
tab-width: 2
3 changes: 3 additions & 0 deletions .mdl_style.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
all
rule 'MD013', :line_length => 120
rule 'MD029', :style => :ordered
1 change: 1 addition & 0 deletions .mdlrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style "#{File.dirname(__FILE__)}/.mdl_style.rb"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Kentaro Hibino

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# asyncmachine-go

`asyncmachine-go` is a minimal implementation of [AsyncMachine](https://github.com/TobiaszCudnik/asyncmachine) in
Golang using _channels_ and _context_. It aims at simplicity and speed.

It can be used as a lightweight in-memory [Temporal](https://github.com/temporalio/temporal) alternative, worker for
[Asynq](https://github.com/hibiken/asynq), or to write simple consensus engines, stateful firewalls, telemetry, bots,
etc.

AsyncMachine is a relational state machine which never blocks.

## Examples

- [Expense Workflow](/examples/temporal-expense/expense_test.go) \|
[Temporal version](https://github.com/temporalio/samples-go/blob/main/expense/) \| [Go playground](https://goplay.tools/snippet/KAxlf3gm7pH)

```go
am.States{
// CreateExpenseActivity
"CreatingExpense": {
Remove: am.S{"ExpenseCreated"},
},
"ExpenseCreated": {
Remove: am.S{"CreatingExpense"},
},
// WaitForDecisionActivity
"WaitingForApproval": {
Auto: true,
Require: am.S{"ExpenseCreated"},
Remove: am.S{"ApprovalGranted"},
},
"ApprovalGranted": {
Require: am.S{"ExpenseCreated"},
Remove: am.S{"WaitingForApproval"},
},
// PaymentActivity
"PaymentInProgress": {
Auto: true,
Require: am.S{"ApprovalGranted"},
Remove: am.S{"PaymentCompleted"},
},
"PaymentCompleted": {
Require: am.S{"ExpenseCreated", "ApprovalGranted"},
Remove: am.S{"PaymentInProgress"},
},
}
```

- [FileProcessing workflow](/examples/temporal-fileprocessing/fileprocessing.go) \|
[Temporal version](https://github.com/temporalio/samples-go/blob/main/fileprocessing/) \| [Go playground](https://goplay.tools/snippet/aTo4hsyJZck)

```go
am.States{
// DownloadFileActivity
"DownloadingFile": {
Remove: am.S{"FileDownloaded"},
},
"FileDownloaded": {
Remove: am.S{"DownloadingFile"},
},
// ProcessFileActivity
"ProcessingFile": {
Auto: true,
Require: am.S{"FileDownloaded"},
Remove: am.S{"FileProcessed"},
},
"FileProcessed": {
Remove: am.S{"ProcessingFile"},
},
// UploadFileActivity
"UploadingFile": {
Auto: true,
Require: am.S{"FileProcessed"},
Remove: am.S{"FileUploaded"},
},
"FileUploaded": {
Remove: am.S{"UploadingFile"},
},
}
```

- [FileProcessing in an Asynq worker](examples/asynq-fileprocessing/fileprocessing_task.go)

```go
func HandleFileProcessingTask(ctx context.Context, t *asynq.Task) error {
var p FileProcessingPayload
if err := json.Unmarshal(t.Payload(), &p); err != nil {
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
}
log.Printf("Processing file %s", p.Filename)
machine, err := processor.FileProcessingFlow(ctx, log.Printf, p.Filename)
if err != nil {
return err
}
// save the machine state as the result
ret := machine.String()
if _, err := t.ResultWriter().Write([]byte(ret)); err != nil {
return fmt.Errorf("failed to write task result: %v", err)
}
return nil
}
```

## Documentation

- [API godoc](https://godoc.org/github.com/pancsta/asyncmachine-go/pkg/machine)
- [Manual](/docs/manual.md)
- [States](/docs/manual.md#states)
- [State Clocks](/docs/manual.md#state-clocks)
- [Auto States](/docs/manual.md#auto-states)
- [Transitions](/docs/manual.md#transitions)
- [Negotiation](/docs/manual.md#negotiation-handlers)
- [Relations](/docs/manual.md#relations)
- [Queue](/docs/manual.md#queue)

## Status

**Beta** - although the ideas behind AsyncMachine have been proven to work, the golang implementation is fairly fresh
and may suffer from bugs, memory leaks, race conditions and even panics.
[Please report bugs](https://github.com/pancsta/asyncmachine-go/issues/new).
71 changes: 71 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
version: '3'

dotenv: ['.env']

tasks:

demo:
cmds:
- task: install-deps
- task: build-protobufs
- task: test
- task: start

build:
cmds:
- go build src/machine/*
silent: true

test:
cmds:
- task: clean
- go test ./...

test-verbose:
cmds:
- go test ./... -v

clean:
cmds:
- go clean -testcache

cloc:
cmds:
- gocloc pkg/machine --not-match=_test\.go
- gocloc pkg/machine/*_test.go
- gocloc examples
- gocloc --include-lang=md docs

format:
cmds:
- gofumpt -w pkg/machine/*.go
- gofumpt -w examples/*/*.go
- goimports -w pkg/**

lint:
cmds:
- task: lint-go
- task: lint-md
# https://github.com/markdownlint/markdownlint/issues/136
- |
if grep -q "]()" docs/*.md; then
echo "Error: Empty links" >&2
exit 1
fi
lint-go:
cmds:
- golangci-lint run

lint-md:
cmds:
# TODO -g
- mdl -c .mdlrc .

install-deps:
cmds:
- go mod tidy
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
- go install mvdan.cc/gofumpt@latest
- go install golang.org/x/tools/cmd/goimports@latest
- gem install mdl
Loading

0 comments on commit 41b5cda

Please sign in to comment.