Coder Social home page Coder Social logo

jobqueue's Introduction

JobStack

This library provides a stack implementation for long running or otherwise expensive processing jobs. As a special case, it implements the standard http.Handler in addition to the generic interface.

Mechanism

The stack defines a maximum concurrency limit at which the jobs can be executed, and makes them wait if this limit is exceeded. The default concurrency limit is 1. It is important to note, that the stack doesn't start individual goroutines for each job, the jobs have to have their own goroutines and the stack should be called from those.

Besides limiting the concurrency level, it is also possible to limit the number of pending jobs, either by setting the maximum stack size, or a timeout for the jobs, or both.

Example

func processJobs(jobs []func()) (dropped, timedOut int) {
	stack := jobqueue.With(Options{
		MaxConcurrency: 256,
		MaxStackSize:   256 * 256,
		Timeout:        9 * time.Millisecond,
	})

	defer stack.Close()

	d := newCounter()
	to := newCounter()
	var wg sync.WaitGroup
	wg.Add(len(jobs))
	for _, j := range jobs {
		go func(j func()) {
			err := stack.Do(j)
			switch err {
			case jobqueue.ErrStackFull:
				d.inc()
			case jobqueue.ErrTimeout:
				to.inc()
			}

			wg.Done()
		}(j)
	}

	wg.Wait()
	dropped = d.value()
	timedOut = to.value()
	return
}

Two-step example

func processInSharedStack(s *jobqueue.Stack, job func()) error {
	done, err := s.Wait()
	if err != nil {
		return err
	}

	job()
	done()
	return nil
}

jobqueue's People

Contributors

aryszka avatar szuecs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jobqueue's Issues

license missing

I checked skipper's dependencies for licenses and this is an unlicensed dependency. Would be great if you can add a license that can used by us. thanks!

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.