GithubHelp home page GithubHelp logo

ahab94 / engine Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 11 KB

A library that makes it very easy to write concurrent code in golang.

Go 100.00%
concurrency golang go task-scheduler concurrent-programming

engine's Introduction

Engine

A library that makes it very easy to write concurrent code in golang. Engine lets you spawn n number of workers that are ready to execute your code.

Parallelism vs Concurrency

Concurrency means multiple tasks which start, run, and complete in overlapping time periods, in no specific order. Parallelism is when multiple tasks OR several part of a unique task literally run at the same time, e.g. on a multi-core processor. Remember that Concurrency and parallelism are NOT the same thing. Must watch Rob Pike's talk.

Concurrency is Hard

As wonderfully summed up by dm03514...

"The Go language provides absolutely amazing concurrency primitives and truly achieves making concurrency a first class citizen. 
Unfortunately ensuring concurrent correctness requires the combination of many different techniques in order to minimize the chances of concurrency related errors."

How Engine makes concurrent execution easy and robust?

Engine is designed to spawn n workers that each maintain a goroutine. It then dispatches work that run concurrently on these available workers as simple as engine.Do(...). It handles queueing of execution and error handling of each execution specifically without affecting other executions.

Engine achieves this by formulating a pattern for any execution. Any execution must adhere to the following interface...

type Executable interface {
	Execute() error
	IsCompleted() bool
	OnSuccess()
	OnFailure(err error)
}

An example of an executable...

type testTask struct {
	ID     int
	Fail   bool
	Delay  string
	Status string
}

func (t *testTask) Execute() error {
	duration, err := time.ParseDuration(t.Delay)
	if err != nil {
		logger.Warnf("parse duration error... overriding Delay as 1 second")
		duration = time.Second
	}
	time.Sleep(duration)

	if t.Fail {
		return errors.New("some error")
	}
	return nil
}

func (t *testTask) OnFailure(err error) {
	t.Status = "failed"
}

func (t *testTask) OnSuccess() {
	t.Status = "completed"
}

func (t *testTask) IsCompleted() bool {
	return t.Status == "completed"
}
e := NewEngine(context.TODO()) // spawn engine's instance...
e.Start(20) // start your engine with 20 workers...
done1 := d.Do(task1) // executes task1...
done2 := d.Do(task2) // executes task2...
done3 := d.Do(task3) // executes task3...
...
done100 := d.Do(task100) // executes task100...
<-done55 // optional: ability to block logic until the task is completed

This not only lets you run your work concurrently but also allows you to handle error specifically on each type of work with a simple task pattern.

Issues?

If you are using this library and encounter any issues please feel free to open an issue.

engine's People

Contributors

ahab94 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

shahzadhaider1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.