Git Product home page Git Product logo

workergroup's Introduction

workergroup

workergroup is a Go package that helps you run multiple goroutines and wait for them to finish. It provides a simple and convenient way to manage the lifecycle of multiple concurrent tasks in your Go program. With workergroup, you can easily add new goroutines to the group, and stop them all at once using the Stop method. You can also wait for all goroutines to finish using the Wait method. Additionally, workergroup comes with a helper function for running an HTTP server as a goroutine, making it easy to gracefully shutdown your server when needed.

Basic Usage

package main

import (
	"context"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"

	"github.com/yozel/workergroup"
)

func main() {
	wgx := workergroup.New()
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt)
	go func() {
		<-sigs
		log.Println("shutting down")
		wgx.Stop()
	}()

	wgx.Add("worker1", func(ctx context.Context) {
		log.Println("worker 1 started")
		for i := 0; i < 10; i++ {
			select {
			case <-ctx.Done():
				return
			default:
				log.Println("worker1 running")
				time.Sleep(1 * time.Second)
			}
		}
		log.Println("worker1 stopped")
	})

	wgx.Add("worker2", func(ctx context.Context) {
		log.Println("worker 2 started")
		for i := 0; i < 5; i++ {
			select {
			case <-ctx.Done():
				return
			default:
				log.Println("worker2 running")
				time.Sleep(1 * time.Second)
			}
		}
		log.Println("worker2 stopped")
	})

	server := &http.Server{
		Addr: ":8080",
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("hello"))
		}),
	}

	wgx.Add("worker3", workergroup.HttpServerWorker(server))

	wgx.Wait()
}

workergroup's People

Contributors

yozel avatar

Stargazers

 avatar

Watchers

 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.