Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Feb 7, 2024
0 parents commit 5092a0b
Show file tree
Hide file tree
Showing 20 changed files with 1,079 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
24 changes: 24 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: golangci-lint
on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.21.6'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: test
on:
workflow_dispatch:
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21.6'
- name: build container
run: docker compose -f ./testdata/_compose.yaml up -d
- name: Build
run: go build .
- name: Test
run: go test -v ./...
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

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

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
45 changes: 45 additions & 0 deletions .golangci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"linters": {
"disable-all": true,
"enable": [
"govet",
"revive",
"goimports",
"misspell",
"ineffassign",
"gofmt"
]
},
"linters-settings": {
"govet": {
"check-shadowing": false
},
"gofmt": {
"simplify": false
}
},
"run": {
"skip-dirs": [
"vendor",
"tests"
],
"tests": false,
"timeout": "10m"
},
"issues": {
"exclude-rules": [
{
"linters": "govet",
"text": "^(nilness|structtag)"
},
{
"linters": "revive",
"text": "should have comment"
},
{
"linters": "revive",
"text": "should be of the form"
}
]
}
}
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) 2024 yuuki takezawa

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.
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# protoactor-go-persistence-pg

Go package with persistence provider for Proto Actor (Go) based on PostgreSQL.

using [pgx v5](https://github.com/jackc/pgx)

# Usage

```go
package main

import (
"context"

"github.com/asynkron/protoactor-go/actor"
"github.com/asynkron/protoactor-go/persistence"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)

type Actor struct {
persistence.Mixin
}

func (a *Actor) Receive(ctx actor.Context) {
// example
}

func main() {

conf, _ := pgxpool.ParseConfig("postgres://postgres:postgres@localhost:5432/sample?sslmode=disable&pool_max_conns=10")

system := actor.NewActorSystem()
ctx := context.Background()
conn, _ := pgxpool.NewWithConfig(ctx, conf)
provider, _ := persistencepg.New(3, persistencepg.NewTable(), conn, system.Logger())

props := actor.PropsFromProducer(func() actor.Actor { return &Actor{} },
actor.WithReceiverMiddleware(persistence.Using(provider)))

pid, _ := system.Root.SpawnNamed(props, "persistent")
}

```

# Default table schema

use ulid as id(varchar(26)) and json as payload

```sql
CREATE TABLE journals
(
id VARCHAR(26) NOT NULL,
payload JSONB NOT NULL,
sequence_number BIGINT,
actor_name VARCHAR(255),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE (id),
UNIQUE (actor_name, sequence_number)
);

CREATE TABLE snapshots
(
id VARCHAR(26) NOT NULL,
payload JSONB NOT NULL,
sequence_number BIGINT,
actor_name VARCHAR(255),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE (id),
UNIQUE (actor_name, sequence_number)
);

```

## change table name

use the interface to change the table name.

for journal table and snapshot table.

```go
// Schemaer is the interface that wraps the basic methods for a schema.
type Schemaer interface {
// JournalTableName returns the name of the journal table.
JournalTableName() string
// SnapshotTableName returns the name of the snapshot table.
SnapshotTableName() string
// ID returns the name of the id column.
ID() string
// Payload returns the name of the payload column.
Payload() string
// ActorName returns the name of the actor name column.
ActorName() string
// SequenceNumber returns the name of the sequence number column.
SequenceNumber() string
// Created returns the name of the created at column.
Created() string
// CreateTable returns the sql statement to create the table.
CreateTable() []string
}
```
50 changes: 50 additions & 0 deletions envelope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package persistencepg

import (
"encoding/json"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)

// struct
type envelope struct {
ID string
Message json.RawMessage
EventIndex int
ActorName string
EventName string
}

type messageEnvelope struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
}

func newEnvelope(message proto.Message) ([]byte, error) {
typeName := proto.MessageName(message)
bytes, err := json.Marshal(message)
if err != nil {
return nil, err
}
return json.Marshal(&messageEnvelope{Payload: bytes, Type: string(typeName)})
}

func (envelope *envelope) message() (proto.Message, error) {
me := &messageEnvelope{}
err := json.Unmarshal(envelope.Message, me)
if err != nil {
return nil, err
}
mt, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(me.Type))
if err != nil {
return nil, err
}
pm := mt.New().Interface()
err = json.Unmarshal(me.Payload, pm)
if err != nil {
return nil, err
}
return pm, nil
}
22 changes: 22 additions & 0 deletions envelope_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package persistencepg

import (
"testing"

"github.com/oklog/ulid/v2"
"github.com/ytake/protoactor-go-persistence-pg/testdata"
)

func TestEnvelope(t *testing.T) {
t.Run("newEnvelope", func(t *testing.T) {
evt := &testdata.UserCreated{
UserID: ulid.Make().String(),
UserName: "test",
Email: "",
}
_, err := newEnvelope(evt)
if err != nil {
t.Errorf("expected nil, got %v", err)
}
})
}
43 changes: 43 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module github.com/ytake/protoactor-go-persistence-pg

go 1.21.6

require (
github.com/asynkron/protoactor-go v0.0.0-20240204165126-fb0ab3e1e075
github.com/jackc/pgx/v5 v5.5.3
github.com/oklog/ulid/v2 v2.1.0
google.golang.org/protobuf v1.32.0
)

require (
github.com/Workiva/go-datastructures v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/lithammer/shortuuid/v4 v4.0.0 // indirect
github.com/lmittmann/tint v1.0.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/orcaman/concurrent-map v1.0.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.44.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
Loading

0 comments on commit 5092a0b

Please sign in to comment.