代码
package main
import (
"encoding/json"
"fmt"
"net/http"
"sync"
)
var dirEngineChan sync.Map
func main() {
dHandler := dagHandler{}
http.Handle("/v1/dagScan", dHandler)
cHandler := callbackHandler{}
http.Handle("/v1/callback", cHandler)
http.ListenAndServe(":8080", nil)
}
type callbackHandler struct{}
func (h callbackHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}
type dagHandler struct{}
func (h dagHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
dag := `{
"345": [
{
"f": "5"
},
{
"a": "2",
"w": "4"
},
{
"p": "5"
}
]
}`
go dagScan(dag)
failStr := "message"
data := []byte(failStr)
res.WriteHeader(200)
res.Write(data)
}
func dagScan(dag string) {
var m map[string][]map[string]string
json.Unmarshal([]byte(dag), &m)
dagChan := make(chan interface{})
for dirID, engineIpList := range m {
for _, engineIpMap := range engineIpList {
var chanList []interface{}
for engine, ip := range engineIpMap {
dirEngineChan.Store(dirID+engine, dagChan)
resultChan, _ := dirEngineChan.Load(dirID + engine)
chanList = append(chanList, resultChan)
fmt.Println(dirID)
fmt.Println(engine)
fmt.Println(ip)
}
for _, readChan := range chanList {
dirEngineResult := <-readChan.(chan interface{})
if dirEngineResult != "ok" {
//del chan
//del the element key dirID+engine of map
fmt.Println("del the element key dirID+engine of map")
}
}
fmt.Println()
fmt.Println()
}
}
//all done
//del chan
//del the key dirID+engine element of map
//del the element of table vs_dirs
fmt.Println("del the element of table vs_dirs")
}