Git Product home page Git Product logo

pool's People

Contributors

deankarn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pool's Issues

Example don't work

"Batch Work" example from readme
go get
go build

.\main.go:12: undefined: pool.NewLimited
.\main.go:44: cannot use func literal (type func(pool.WorkUnit) (interface {}, error)) as type pool.WorkFunc in return argument
.\main.go:50: wu.IsCancelled undefined (type pool.WorkUnit has no field or method IsCancelled)

A suggestion about Usage and documentation

In the Batch Work example,The email.Value will be nil when called batch.Cancel() that will cause Panic as a result of wrong type desertation

for email := range batch.Results() {    
      if err := email.Error(); err != nil {
            // handle error
            // maybe call batch.Cancel()
        }

        // use return value 
       // The email.Value will be nil when called batch.Cancel() that will cause Panic as a result of wrong type desertation
        fmt.Println(email.Value().(bool)) 
    }

It might be good to write like this

for email := range batch.Results() {

        if err := email.Error(); err != nil {
            // handle error
            // maybe call batch.Cancel()
        }else{
       // use return value 
        fmt.Println(email.Value().(bool)) 
      }

Should the pool instance be reused?

Hey @joeybloggs, thanks for this package. The abstractions make it dead simple to start and manage worker pools.

How do you recommend I use the pool instance? Should I create and reuse it(by making it package scope) if I am only ever going to create and use a batch inside a function? Here's the code:

package util

import (
	"runtime"

	jsoniter "github.com/json-iterator/go"
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
	"gopkg.in/go-playground/pool.v3"
)

// jsoniter.ConfigFastest marshals the float with 6 digits precision (lossy),
// which is significantly faster. It also does not escape HTML.
var json = jsoniter.ConfigFastest

// Unmarshal uses jsoniter for efficiently unmarshalling the byte stream into
// the struct pointer.
func Unmarshal(bs []byte, v interface{}) error {
	// See https://github.com/json-iterator/go/blob/master/example_test.go#L69-L88
	iter := json.BorrowIterator(bs)
	defer json.ReturnIterator(iter)

	iter.ReadVal(v)
	if iter.Error != nil {
		log.WithError(iter.Error).
			Error("Got error while trying to unmarshal value into given struct")
		return errors.Wrap(iter.Error, "util: unmarshal using jsoniter.ConfigFastest failed")
	}
	return nil
}

func unmarshalWorker(bs []byte, val interface{}) pool.WorkFunc {
	return func(wu pool.WorkUnit) (interface{}, error) {
		if wu.IsCancelled() {
			// return values not used
			return nil, nil
		}

		return nil, Unmarshal(bs, val)
	}
}

// BulkUnmarshal uses worker pool
func BulkUnmarshal(bytesSlice [][]byte, vals []interface{}) error {
	if len(bytesSlice) != len(vals) {
		return errors.New(
			"util: bulk unmarshal failed: length of bytes slice did not match targets",
		)
	}

	p := pool.NewLimited(uint(runtime.NumCPU() * 2))
	defer p.Close()

	batch := p.Batch()

	go func() {
		for idx, bs := range bytesSlice {
			batch.Queue(unmarshalWorker(bs, vals[idx]))
		}

		// DO NOT FORGET THIS OR GOROUTINES WILL DEADLOCK
		// if calling Cancel() it calles QueueComplete() internally
		batch.QueueComplete()
	}()

	for res := range batch.Results() {
		if err := res.Error(); err != nil {
			batch.Cancel()
			return errors.Wrap(err, "util: bulk unmarshal failed")
		}
	}

	return nil
}

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.