global.go 1.4 KB
package protocol

import (
	"sort"
	"sync"
)

var(
	SuccessRequestMap  *sync.Map
	ErrorRequestMap *sync.Map
)

const(
	StatusOK ="OK"
	StatusFial ="Fail"
)

func init(){
	SuccessRequestMap =new(sync.Map) //&sync.Map{}
	ErrorRequestMap = new(sync.Map)
	//SuccessRequestMap.Store("/auth/login",&Request{Host:"1237.0.0.1"})
}

func Exists(status string,key string)bool{
	m :=GetMap(status)
	if _,ok:=m.Load(key);ok{
		return true
	}
	return false
}

func SetRequest(status string,key string,req *Request){
	m :=GetMap(status)
	if _,ok:=m.Load(key);ok{
		return
	}
	m.Store(key,req)
}

func GetRequest(status string,key string)(*Request){
	m :=GetMap(status)
	if v,ok:=m.Load(key);ok{
		return v.(*Request)
	}
	return nil
}

func GetRequests(status string)([]*Request){
	m :=GetMap(status)
	var listKey []string
	var list []*Request
	m.Range(func(key,value interface{})bool{
		listKey = append(listKey,key.(string))
		return true
	})
	sort.Strings(listKey)
	for i:=range listKey{
		r :=GetRequest(status,listKey[i])
		if r!=nil{
			list = append(list,r)
		}
	}
	return list
}

func DeleteRequest(status string,key string){
	m :=GetMap(status)
	m.Delete(key)
}

func DeleteAll(status string){
	m :=GetMap(status)
	m.Range(func(key,value interface{})bool{
		m.Delete(key)
		return true
	})
}

func GetMap(status string) *sync.Map{
	if status==StatusOK{
		return SuccessRequestMap
	}
	return ErrorRequestMap
}