Giter Site home page Giter Site logo

devilcrises / eventpool Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yudhasubki/eventpool

0.0 0.0 0.0 8 KB

Golang workerpool with min and max to spawn the workers. Dynamically to spawn or kill the worker based on idling time.

License: MIT License

Go 100.00%

eventpool's Introduction

eventpool

Limiting Golang concurrency using the Queue and PubSub mechanism. Every function registered is needed to set the topic to deliver the message in the right pool.

Installation

To install this package, you need to setup your Go workspace. The simplest way to install the library is to run:

go get -u github.com/yudhasubki/eventpool

Usage

func main() {
	eventPool := eventpool.New()
	eventPool.SubscriberRegistry("test", func(id string, message io.Reader) error {
		var buf bytes.Buffer
		_, err := io.Copy(&buf, message)
		if err != nil {
			return err
		}
		defer buf.Reset()

		fmt.Println(id, " ", buf.String())
		time.Sleep(3 * time.Second)

		return nil
	}, eventpool.SetMinFlight(2),
		eventpool.SetMaxFlight(10),
		eventpool.SetPoolThreshold(2),
		eventpool.SetThreshold(40))
	eventPool.Start()

	for i := 0; i < 10; i++ {
		eventPool.Publish("test", eventpool.SendString("Hai 1"))
		eventPool.Publish("test", eventpool.SendString("Hai 2"))
		eventPool.Publish("test", eventpool.SendString("Hai 3"))
		eventPool.Publish("test", eventpool.SendString("Hai 4"))
		eventPool.Publish("test", eventpool.SendString("Hai 5"))
		eventPool.Publish("test", eventpool.SendString("Hai 6"))
	}
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

eventpool's People

Contributors

yudhasubki avatar

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.