Golang WaitGroup Examples

Golang WaitGroup - 30 examples found. These are the top rated real world Golang examples of sync.WaitGroup extracted from open source projects. You can rate examples to help us improve the quality of examples.
Frequently Used Methods
Example #1
0
func TestBalancer(t *testing.T) { // {{{
	b := queue.NewBalancer("TestCacheAPISearch", PoolSize, QueueSize)
	t.Logf("[Balancer] Created new: '%s'", b.Name())

	b.Run()
	defer b.Close()
	t.Logf("[Balancer] Started: '%s'", b.Info())

	var j *TestBalancerJob
	var wg sync.WaitGroup
	startedAt := time.Now()

	for i := 0; i < JobsQuantity; i++ {
		j = &TestBalancerJob{Message: "Get worker info from Balancer"}
		j.Initialize()
		t.Logf("[TestBalancerJob:%d] Created: '%s'", i, j.Message)

		wg.Add(1)
		b.Dispatch(j)
		go finalizeTestBalancerJob(j, i, &wg, t)
	}

	wg.Wait()
	finishedAt := time.Now()
	t.Logf("[Balancer] Executed %d of tasks -> [%.6fs]", JobsQuantity, finishedAt.Sub(startedAt).Seconds())
} // }}}
Example #2
0
func (hp *httpProxy) Serve(wg *sync.WaitGroup) {
	defer func() {
		wg.Done()
	}()
	ln, err := net.Listen("tcp", hp.addr)
	if err != nil {
		fmt.Println("listen http failed:", err)
		return
	}
	host, _, _ := net.SplitHostPort(hp.addr)
	var pacURL string
	if host == "" || host == "0.0.0.0" {
		pacURL = fmt.Sprintf("http://<hostip>:%s/pac", hp.port)
	} else if hp.addrInPAC == "" {
		pacURL = fmt.Sprintf("http://%s/pac", hp.addr)
	} else {
		pacURL = fmt.Sprintf("http://%s/pac", hp.addrInPAC)
	}
	info.Printf("listen http %s, PAC url %s\n", hp.addr, pacURL)

	for {
		conn, err := ln.Accept()
		if err != nil {
			errl.Printf("http proxy(%s) accept %v\n", ln.Addr(), err)
			if isErrTooManyOpenFd(err) {
				connPool.CloseAll()
			}
			time.Sleep(time.Millisecond)
			continue
		}
		c := newClientConn(conn, hp)
		go c.serve()
	}
}
Example #3
0
// Work turns on the worker
func (w *Worker) Work(wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		select {
		// safely stop the worker
		case <-w.stop:
			return
		case task := <-w.reader:
			tasks, err := w.processFn(task)
			if err != nil {
				if task.Retries < MaxRetries-1 {
					task.Retries++
					w.writer <- task
					continue
				}
			}

			// submit any new tasks returned by the old one
			if tasks != nil {
				for _, t := range tasks {
					w.writer <- t
				}
			}
		}
	}
}
Example #4
0
func main() {
	introText := "SIMPLE TWITTER REFORMATTER \n (╯°□°)╯︵ ┻━┻) \n"
	fmt.Printf(introText)

	key := flag.String("key", "nokey", "Twitter consumer key")
	secret := flag.String("sec", "nosecret", "Twitter consumer secret")
	debug := flag.Bool("debug", false, "Debug logging level")
	numTweets := flag.Int("num", 3, "Number of tweets to retrieve")

	flag.Parse()

	access_token, err := getBearerToken(*key, *secret, *debug)
	if err != nil || access_token == "" {
		log.Fatal("Could not retrieve token to make twitter API request")
		os.Exit(1)
	}

	// Create a very basic channel with tweets getting passed into the expander
	// Wait for it to finish executing before quiting.
	var tweetChannel chan string = make(chan string)
	var wg sync.WaitGroup
	wg.Add(1)
	go tweetRetriever(access_token, *numTweets, tweetChannel, &wg, *debug)
	go textExpander(tweetChannel)
	wg.Wait()
}
Example #5
0
func main() {
	var wg sync.WaitGroup
	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)

	go func() {
		sig := <-sc
		running = false
		fmt.Printf("main:Got signal:%v", sig)
	}()
	fmt.Printf("main:Mock get id process start!\n")
	db, err := GetDatabase()
	if err != nil {
		fmt.Printf("main:GetDatabase error:%s\n", err.Error())
		return
	}
	idGenerator, err := GetIdGenerator(db, idKey)
	if err != nil {
		fmt.Printf("main:GetIdGenerator error:%s\n", err.Error())
		return
	}
	wg.Add(1)
	go MockGetId(idGenerator, db, &wg)
	wg.Wait()
}
Example #6
0
// trapSignal wait on listed signals for pre-defined behaviors
func (a *app) trapSignal(wg *sync.WaitGroup) {
	ch := make(chan os.Signal, 10)
	signal.Notify(ch, syscall.SIGTERM, syscall.SIGHUP)
	for {
		sig := <-ch
		switch sig {
		case syscall.SIGTERM:
			// this ensures a subsequent TERM will trigger standard go behaviour of terminating
			signal.Stop(ch)
			// roll through all initialized http servers and stop them
			for _, s := range a.sds {
				go func(s httpdown.Server) {
					defer wg.Done()
					if err := s.Stop(); err != nil {
						a.errors <- probe.NewError(err)
					}
				}(s)
			}
			return
		case syscall.SIGHUP:
			// we only return here if there's an error, otherwise the new process
			// will send us a TERM when it's ready to trigger the actual shutdown.
			if _, err := a.net.StartProcess(); err != nil {
				a.errors <- err.Trace()
			}
		}
	}
}