package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/client" "sync" "time" ) // var counter int var lock sync.Mutex /* { "":{ "status": } } */ var urbitBroadcast = make(map[string]map[string]string) func broadcast() { for { fmt.Println(urbitBroadcast) // send to websocket as json blob time.Sleep(250 * time.Millisecond) } } // Example func getStatus(i int, patp string) { for { // Locking the shared state lock.Lock() // Create submap _, exist := urbitBroadcast[patp] if !exist { urbitBroadcast[patp] = make(map[string]string) } //Get container running status cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { //panic(err) fmt.Println(err) } else { containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) if err != nil { urbitBroadcast[patp]["status"] = "error" } else { for _, container := range containers { for _, name := range container.Names { fasPatp := "/" + patp if name == fasPatp { urbitBroadcast[patp]["status"] = container.Status } } } } } // Unlocking the shared state lock.Unlock() // Arbitary sleep time.Sleep(5 * time.Second) } } func main() { /* Temporary Hardcode -- from config file */ piers := []string{"widwet-mornev-nallux-dozryl", "hocfur-wicnym-nallux-dozryl", "pinlyd-mattyd-nallux-dozryl", "solnys-dibmyn-nallux-dozryl", "wormun-fadwyl-nallux-dozryl", "nopmul-pollyt", "nalruc-nallux-dozryl", } go broadcast() for i, patp := range piers { go getStatus(i, patp) } // Waiting for the goroutines to complete var input string fmt.Scanln(&input) }