本文整理匯總了Golang中sync.WaitGroup類的典型用法代碼示例。如果您正苦於以下問題:Golang WaitGroup類的具體用法?Golang WaitGroup怎麽用?Golang WaitGroup使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了WaitGroup類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestBalancer
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())
} // }}}
示例2: Serve
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()
}
}
示例3: Work
// 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
}
}
}
}
}
示例4: main
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()
}
示例5: main
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()
}
示例6: trapSignal
// 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()
}
}
}
}
示例7: createRunningPod
func createRunningPod(wg *sync.WaitGroup, c *client.Client, name, ns, image string, labels map[string]string) {
defer GinkgoRecover()
defer wg.Done()
pod := &api.Pod{
TypeMeta: unversioned.TypeMeta{
Kind: "Pod",
},
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: name,
Image: image,
},
},
DNSPolicy: api.DNSDefault,
},
}
_, err := c.Pods(ns).Create(pod)
expectNoError(err)
expectNoError(waitForPodRunningInNamespace(c, name, ns))
}
示例8: NewHttpService
func NewHttpService(context interface {
Acquire()
Release()
}, server *http.Server) (s *HttpService, err error) {
s = &HttpService{}
addr := server.Addr
if addr == "" {
addr = ":http"
}
s.listener, err = net.Listen("tcp", addr)
if err != nil {
return
}
var w sync.WaitGroup
w.Add(1)
context.Acquire()
go func() {
defer context.Release()
l := s.listener
w.Done()
server.Serve(l)
}()
return
}
示例9: StartGoogleNews
// StartGoogleNews start collecting google news
func StartGoogleNews(googleLoopCounterDelay int) {
fmt.Println("startgoogle news launched!")
fmt.Println(googleLoopCounterDelay)
for t := range time.Tick(time.Duration(googleLoopCounterDelay) * time.Second) {
_ = t
fmt.Println("loop will start")
var wsg sync.WaitGroup
n := make(chan GoogleNewsResponseData)
// cs := make(chan int)
for _, v := range TopicsList() {
wsg.Add(1)
go func(v TopicIdentity) {
go GoogleNewsRequester(googleURLConstructor(v.Initial), v, n, &wsg)
result := <-n
GoogleNewsRW(result, &wsg)
}(v)
}
wsg.Wait()
close(n)
// cache index news keys
newsCache.NewsIndexCache()
}
}
示例10: NewFakeNeverRateLimiter
func NewFakeNeverRateLimiter() RateLimiter {
wg := sync.WaitGroup{}
wg.Add(1)
return &fakeNeverRateLimiter{
wg: wg,
}
}
示例11: dialWebsocket
func dialWebsocket(db *sql.DB, wg *sync.WaitGroup, i int) {
origin := "http://localhost/"
url := "ws://localhost:8080/scoreboard"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
res, err := scoreboard.CollectLastResult(db)
if err != nil {
log.Fatal(err)
}
html_res := res.ToHTML(false)
var msg = make([]byte, len(html_res))
if _, err = ws.Read(msg); err != nil {
log.Fatal(err)
}
if string(msg) != html_res {
log.Fatalln("Received result invalid",
html_res, msg)
}
wg.Done()
}
示例12: TestWrite
func TestWrite(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
go TCPServer(t, &wg)
// Give the fake TCP server some time to start:
time.Sleep(time.Millisecond * 100)
i := Instrumental{
Host: "127.0.0.1",
ApiToken: "abc123token",
Prefix: "my.prefix",
}
i.Connect()
// Default to gauge
m1, _ := telegraf.NewMetric(
"mymeasurement",
map[string]string{"host": "192.168.0.1"},
map[string]interface{}{"myfield": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
m2, _ := telegraf.NewMetric(
"mymeasurement",
map[string]string{"host": "192.168.0.1", "metric_type": "set"},
map[string]interface{}{"value": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
// Simulate a connection close and reconnect.
metrics := []telegraf.Metric{m1, m2}
i.Write(metrics)
i.Close()
// Counter and Histogram are increments
m3, _ := telegraf.NewMetric(
"my_histogram",
map[string]string{"host": "192.168.0.1", "metric_type": "histogram"},
map[string]interface{}{"value": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
// We will drop metrics that simply won't be accepted by Instrumental
m4, _ := telegraf.NewMetric(
"bad_values",
map[string]string{"host": "192.168.0.1", "metric_type": "counter"},
map[string]interface{}{"value": "\" 3:30\""},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
m5, _ := telegraf.NewMetric(
"my_counter",
map[string]string{"host": "192.168.0.1", "metric_type": "counter"},
map[string]interface{}{"value": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
metrics = []telegraf.Metric{m3, m4, m5}
i.Write(metrics)
wg.Wait()
i.Close()
}
示例13: emitterMetaPrinter
func emitterMetaPrinter(emitter chan contentBuffer.EmitterData, wg *sync.WaitGroup) {
for {
ed := <-emitter
fmt.Printf("%d-%d %s\n", ed.LineStart, ed.LineEnd, ed.NodePath)
wg.Done()
}
}
示例14: RawJsonInternalOutputProtocol
// a raw byte Key, and a json value
func RawJsonInternalOutputProtocol(writer io.Writer) (*sync.WaitGroup, chan<- KeyValue) {
w := bufio.NewWriter(writer)
in := make(chan KeyValue, 100)
tab := []byte("\t")
newline := []byte("\n")
var wg sync.WaitGroup
wg.Add(1)
go func() {
for kv := range in {
kBytes, ok := kv.Key.([]byte)
if !ok {
Counter("RawJsonInternalOutputProtocol", "key is not []byte", 1)
log.Printf("failed type casting %v", kv.Key)
continue
}
vBytes, err := json.Marshal(kv.Value)
if err != nil {
Counter("RawJsonInternalOutputProtocol", "unable to json encode value", 1)
log.Printf("%s - failed encoding %v", err, kv.Value)
continue
}
w.Write(kBytes)
w.Write(tab)
w.Write(vBytes)
w.Write(newline)
}
w.Flush()
wg.Done()
}()
return &wg, in
}
示例15: client
func client(configuration *Configuration, result *Result, done *sync.WaitGroup) {
defer func() {
if r := recover(); r != nil {
fmt.Println("caught recover: ", r)
os.Exit(1)
}
}()
myclient := MyClient(result, time.Duration(connectTimeout)*time.Millisecond,
time.Duration(readTimeout)*time.Millisecond,
time.Duration(writeTimeout)*time.Millisecond)
for result.requests < configuration.requests {
for _, tmpUrl := range configuration.urls {
req, _ := http.NewRequest(configuration.method, tmpUrl, bytes.NewReader(configuration.postData))
if configuration.keepAlive == true {
req.Header.Add("Connection", "keep-alive")
} else {
req.Header.Add("Connection", "close")
}
if len(configuration.authHeader) > 0 {
req.Header.Add("Authorization", configuration.authHeader)
}
if len(configuration.contentType) > 0 {
req.Header.Add("Content-Type", contentType)
} else if len(configuration.postData) > 0 {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
resp, err := myclient.Do(req)
result.requests++
if err != nil {
result.networkFailed++
continue
}
_, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
result.networkFailed++
continue
}
if resp.StatusCode == http.StatusOK {
result.success++
} else {
result.badFailed++
}
resp.Body.Close()
}
}
done.Done()
}