status.go 1.2 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
package node

import (
	"fmt"
	entity "github.com/eolinker/goku/server/entity/console-entity"
	"sync"
	"time"
)

const EXPIRE  = time.Second * 10
var(
	manager = _StatusManager{
		locker:sync.RWMutex{},
		lastHeartBeat:make(map[string]time.Time),
	}
)
type _StatusManager struct {
	locker sync.RWMutex
	lastHeartBeat map[string]time.Time
}

func (m *_StatusManager)refresh(id string)  {
	t:=time.Now()
	m.locker.Lock()

	m.lastHeartBeat[id]=t

	m.locker.Unlock()
}

func (m *_StatusManager)stop(id string)  {

	m.locker.Lock()

	delete(m.lastHeartBeat,id)

	m.locker.Unlock()
}
func  (m *_StatusManager)get(id string) (time.Time, bool)  {
	m.locker.RLock()
	t,b:=m.lastHeartBeat[id]
	m.locker.RUnlock()
	return t,b
}

func Refresh(ip string,port string)  {
	id:=fmt.Sprintf("%s:%d",ip,port)
	manager.refresh(id)
}

func NodeStop(ip,port string)  {
	id:=fmt.Sprintf("%s:%d",ip,port)
	manager.stop(id)
}

func IsLive(ip string,port string) bool  {
	id:=fmt.Sprintf("%s:%d",ip,port)
	t,has:=manager.get(id)
	if !has{
		return false
	}

	if  time.Now().Sub(t) > EXPIRE{
		return false
	}
	return true
}
func ResetNodeStatus(nodes... *entity.Node)  {
	for _, node:=range nodes{

		if IsLive(node.NodeIP,node.NodePort){
			node.NodeStatus = 1
		}else{
			node.NodeStatus = 0
		}
	}
}