Debian Codegraph @

examples/concurrent_shutdown/concurrent_shutdown.go

raw symbols SCIP
1 // Copyright © 2013, 2014, The Go-LXC Authors. All rights reserved.
2 // Use of this source code is governed by a LGPLv2.1
3 // license that can be found in the LICENSE file.
4
5 //go:build linux && cgo
6 // +build linux,cgo
7
8 package main
9
10 import (
11 "flag"
12 "log"
13 "runtime"
14 "strconv"
15 "sync"
16 "time"
17
18 "github.com/lxc/go-lxc"
19 )
20
21 var (
22 lxcpath string
23 count int
24 )
25
26 func init() {
27 runtime.GOMAXPROCS(runtime.NumCPU())
28 flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path")
29 flag.IntVar(&count, "count", 10, "Number of containers")
30 flag.Parse()
31 }
32
33 func main() {
34 var wg sync.WaitGroup
35
36 for i := 0; i < count; i++ {
37 wg.Add(1)
38 go func(i int) {
39 c, err := lxc.NewContainer(strconv.Itoa(i), lxcpath)
40 if err != nil {
41 log.Fatalf("ERROR: %s\n", err.Error())
42 }
43 defer c.Release()
44
45 log.Printf("Shutting down the container (%d)...\n", i)
46 if err := c.Shutdown(30 * time.Second); err != nil {
47 if err = c.Stop(); err != nil {
48 log.Fatalf("ERROR: %s\n", err.Error())
49 }
50 }
51 wg.Done()
52 }(i)
53 }
54 wg.Wait()
55 }