sagafunc

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 4 Imported by: 0

README

go-saga-func

This package is an implementation of Choreography-based saga pattern in golang

Saga pattern used in distributed systems, particularly microservices architectures, to manage data consistency across multiple services. The purpose is to ensures data consistency across multiple services involved in a single business transaction.

Overall, the saga pattern is a powerful tool for managing data consistency in distributed systems but requires careful consideration and implementation due to it's complexities.

You can read more details about saga pattern here:

Installation

The following command is used for install this package into your golang project

go get github.com/Muruyung/go-saga-func@latest

Getting Started

How to implement it is quite simple, you only need to add step for execution function and rollback function. The following code is an example of its implementation:

Example condition without error

This code has output Result: 25

package main

import (
    "fmt"
    sagafunc "github.com/Muruyung/go-saga-func"
)

func main() {
    var (
        saga         = sagafunc.NewSaga()
        exampleValue = 10
        err          error
    )

    err = sagaTest.AddStep(&saga.Step{
        ProcessName: "step 1",
        ExecutionFunc: func() error {
            exampleValue += 5
            return nil
        },
        RollbackFunc: func() error {
            exampleValue -= 5
            return nil
        },
    })
    if err != nil {
        fmt.Println(err)
        return
    }

    err = sagaTest.AddStep(&saga.Step{
        ProcessName: "step 2",
        ExecutionFunc: func() error {
            exampleValue += 10
            return nil
        },
        RollbackFunc: func() error {
            exampleValue -= 10
            return nil
        },
    })
    if err != nil {
        fmt.Println(err)
        return
    }

    err = sagaTest.ExecStart()
    fmt.Printf("Result: %v\n", exampleValue)
    if err != nil {
        fmt.Println(err)
        return
    }
}
Example condition with error and rollback

This code has output Result: 10 and detail error

package main

import (
    "fmt"
    sagafunc "github.com/Muruyung/go-saga-func"
)

func main() {
    var (
        saga         = sagafunc.NewSaga()
        exampleValue = 10
        err          error
    )

    err = sagaTest.AddStep(&saga.Step{
        ProcessName: "step 1",
        ExecutionFunc: func() error {
            exampleValue += 5
            return nil
        },
        RollbackFunc: func() error {
            exampleValue -= 5
            return nil
        },
    })
    if err != nil {
        fmt.Println(err)
        return
    }

    err = sagaTest.AddStep(&saga.Step{
        ProcessName: "step 2",
        ExecutionFunc: func() error {
            exampleValue += 10
            return fmt.Errorf("some detail error here")
        },
        RollbackFunc: func() error {
            exampleValue -= 10
            return nil
        },
    })
    if err != nil {
        fmt.Println(err)
        return
    }

    err = sagaTest.ExecStart()
    fmt.Printf("Result: %v\n", exampleValue)
    if err != nil {
        fmt.Println(err)
        return
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result struct {
	ExecutionError error
	RollbackErrors *multierror.Error
}

type Saga

type Saga interface {
	AddStep(step *Step) error
	ExecStart() error
}

func NewSaga

func NewSaga(skipRollbackError ...bool) Saga

NewSaga initialize new saga (default FALSE)

  • Set parameter TRUE if you won't execute rollback on error func
  • Set parameter FALSE (or empty this parameter) if you want execute all rollback

type Step

type Step struct {
	ProcessName   string
	ExecutionFunc func() error
	RollbackFunc  func() error
}

Jump to

Keyboard shortcuts

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