Use atomic.Value to store hashes

This commit is contained in:
Kusakabe Si 2021-12-04 03:51:50 +00:00
parent c239162159
commit 4ee81f7691
4 changed files with 53 additions and 53 deletions

View File

@ -569,34 +569,23 @@ func (device *Device) process_UpdateSuperParamsMsg(peer *Peer, State_hash string
return err
}
if SuperParams.PeerAliveTimeout <= 0 {
device.log.Errorf("SuperParams.PeerAliveTimeout <= 0: %v", SuperParams.PeerAliveTimeout)
return fmt.Errorf("SuperParams.PeerAliveTimeout <= 0: %v", SuperParams.PeerAliveTimeout)
device.log.Errorf("SuperParams.PeerAliveTimeout <= 0: %v, please check the config of the supernode", SuperParams.PeerAliveTimeout)
return fmt.Errorf("SuperParams.PeerAliveTimeout <= 0: %v, please check the config of the supernode", SuperParams.PeerAliveTimeout)
}
if SuperParams.SendPingInterval <= 0 {
device.log.Errorf("SuperParams.SendPingInterval <= 0: %v", SuperParams.SendPingInterval)
return fmt.Errorf("SuperParams.SendPingInterval <= 0: %v", SuperParams.SendPingInterval)
device.log.Errorf("SuperParams.SendPingInterval <= 0: %v, please check the config of the supernode", SuperParams.SendPingInterval)
return fmt.Errorf("SuperParams.SendPingInterval <= 0: %v, please check the config of the supernode", SuperParams.SendPingInterval)
}
if SuperParams.HttpPostInterval <= 0 {
device.log.Errorf("SuperParams.HttpPostInterval <= 0: %v", SuperParams.HttpPostInterval)
return fmt.Errorf("SuperParams.HttpPostInterval <= 0: %v", SuperParams.HttpPostInterval)
if SuperParams.HttpPostInterval < 0 {
device.log.Errorf("SuperParams.HttpPostInterval < 0: %v, please check the config of the supernode", SuperParams.HttpPostInterval)
return fmt.Errorf("SuperParams.HttpPostInterval < 0: %v, please check the config of the supernode", SuperParams.HttpPostInterval)
}
device.EdgeConfig.DynamicRoute.PeerAliveTimeout = SuperParams.PeerAliveTimeout
if device.EdgeConfig.DynamicRoute.SendPingInterval <= 0 {
device.EdgeConfig.DynamicRoute.SendPingInterval = SuperParams.SendPingInterval
device.Chan_SendPingStart <- struct{}{}
} else {
device.EdgeConfig.DynamicRoute.SendPingInterval = SuperParams.SendPingInterval
}
if device.SuperConfig.HttpPostInterval <= 0 {
device.SuperConfig.HttpPostInterval = SuperParams.HttpPostInterval
device.Chan_HttpPostStart <- struct{}{}
} else {
device.SuperConfig.HttpPostInterval = SuperParams.HttpPostInterval
}
device.EdgeConfig.DynamicRoute.SendPingInterval = SuperParams.SendPingInterval
device.SuperConfig.HttpPostInterval = SuperParams.HttpPostInterval
device.Chan_SendPingStart <- struct{}{}
device.Chan_HttpPostStart <- struct{}{}
if SuperParams.AdditionalCost >= 0 {
device.EdgeConfig.DynamicRoute.AdditionalCost = SuperParams.AdditionalCost
}

View File

@ -1,5 +1,5 @@
nodename: NodeSuper
postscript: ""
postscript: example_config/echo.sh test
privkeyv4: mL5IW0GuqbjgDeOJuPHBU2iJzBPNKhaNEXbIGwwYWWk=
privkeyv6: +EdOKIoBp/EvIusHDsvXhV1RJYbyN3Qr8nxlz35wl3I=
listenport: 3000

View File

@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"net/http"
@ -79,12 +80,12 @@ type HttpPeerInfo struct {
}
type PeerState struct {
NhTableState string
PeerInfoState string
SuperParamState string
JETSecret mtypes.JWTSecret
httpPostCount uint64
LastSeen time.Time
NhTableState atomic.Value // string
PeerInfoState atomic.Value // string
SuperParamState atomic.Value // string
JETSecret atomic.Value // mtypes.JWTSecret
httpPostCount atomic.Value // uint64
LastSeen atomic.Value // time.Time
}
type client struct {
@ -160,7 +161,7 @@ func get_api_peers(old_State_hash string) (api_peerinfo mtypes.API_Peers, StateH
PSKey: peerinfo.PSKey,
Connurl: &mtypes.API_connurl{},
}
if httpobj.http_PeerState[peerinfo.PubKey].LastSeen.Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now()) {
if httpobj.http_PeerState[peerinfo.PubKey].LastSeen.Load().(time.Time).Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now()) {
connV4 := httpobj.http_device4.GetConnurl(peerinfo.NodeID)
connV6 := httpobj.http_device6.GetConnurl(peerinfo.NodeID)
if connV4 != "" {
@ -239,7 +240,7 @@ func get_superparams(w http.ResponseWriter, r *http.Request) {
AdditionalCost: httpobj.http_PeerID2Info[NodeID].AdditionalCost,
}
SuperParamStr, _ := json.Marshal(SuperParams)
httpobj.http_PeerState[PubKey].SuperParamState = State
httpobj.http_PeerState[PubKey].SuperParamState.Store(State)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(SuperParamStr))
@ -291,7 +292,7 @@ func get_peerinfo(w http.ResponseWriter, r *http.Request) {
}
// Do something
httpobj.http_PeerState[PubKey].PeerInfoState = State
httpobj.http_PeerState[PubKey].PeerInfoState.Store(State)
http_PeerInfo_2peer := make(mtypes.API_Peers)
for PeerPubKey, peerinfo := range httpobj.http_PeerInfo {
@ -372,7 +373,7 @@ func get_nhtable(w http.ResponseWriter, r *http.Request) {
return
}
httpobj.http_PeerState[PubKey].NhTableState = State
httpobj.http_PeerState[PubKey].NhTableState.Store(State)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(httpobj.http_NhTableStr))
@ -404,7 +405,7 @@ func get_peerstate(w http.ResponseWriter, r *http.Request) {
}
for _, peerinfo := range httpobj.http_sconfig.Peers {
LastSeenStr := httpobj.http_PeerState[peerinfo.PubKey].LastSeen.String()
LastSeenStr := httpobj.http_PeerState[peerinfo.PubKey].LastSeen.Load().(time.Time).String()
hs.PeerInfo[peerinfo.NodeID] = HttpPeerInfo{
Name: peerinfo.Name,
LastSeen: LastSeenStr,
@ -470,7 +471,8 @@ func post_nodeinfo(w http.ResponseWriter, r *http.Request) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return JWTSecret[:], nil
JWTSecretB := JWTSecret.Load().(mtypes.JWTSecret)
return JWTSecretB[:], nil
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
@ -486,7 +488,7 @@ func post_nodeinfo(w http.ResponseWriter, r *http.Request) {
client_PostCount := token_claims.PostCount
client_body_hash := token_claims.BodyHash
if client_PostCount < httpPostCount {
if client_PostCount < httpPostCount.Load().(uint64) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Request body: postcount too small: %v", httpPostCount)))
return
@ -514,7 +516,7 @@ func post_nodeinfo(w http.ResponseWriter, r *http.Request) {
httpobj.http_PeerIPs[PubKey].LocalIPv4 = client_report.LocalV4s
httpobj.http_PeerIPs[PubKey].LocalIPv6 = client_report.LocalV6s
httpobj.http_PeerState[PubKey].httpPostCount = client_PostCount + 1
httpobj.http_PeerState[PubKey].httpPostCount.Store(client_PostCount + 1)
applied_pones := make([]mtypes.PongMsg, 0, len(client_report.Pongs))
for _, pong_msg := range client_report.Pongs {

View File

@ -152,8 +152,8 @@ func Super(configPath string, useUAPI bool, printExample bool, bindmode string)
if sconfig.PeerAliveTimeout <= 0 {
return fmt.Errorf("PeerAliveTimeout must > 0 : %v", sconfig.PeerAliveTimeout)
}
if sconfig.HttpPostInterval <= 0 {
return fmt.Errorf("HttpPostInterval must > 0 : %v", sconfig.HttpPostInterval)
if sconfig.HttpPostInterval < 0 {
return fmt.Errorf("HttpPostInterval must >= 0 : %v", sconfig.HttpPostInterval)
} else if sconfig.HttpPostInterval > sconfig.PeerAliveTimeout {
return fmt.Errorf("HttpPostInterval must <= PeerAliveTimeout : %v", sconfig.HttpPostInterval)
}
@ -349,7 +349,16 @@ func super_peeradd(peerconf mtypes.SuperPeerInfo) error {
}
}
httpobj.http_PeerID2Info[peerconf.NodeID] = peerconf
httpobj.http_PeerState[peerconf.PubKey] = &PeerState{}
PS := PeerState{}
PS.NhTableState.Store("") // string
PS.PeerInfoState.Store("") // string
PS.SuperParamState.Store("") // string
PS.JETSecret.Store(mtypes.JWTSecret{}) // mtypes.JWTSecret
PS.httpPostCount.Store(uint64(0)) // uint64
PS.LastSeen.Store(time.Time{}) // time.Time
httpobj.http_PeerState[peerconf.PubKey] = &PS
httpobj.http_PeerIPs[peerconf.PubKey] = &HttpPeerLocalIP{}
return nil
}
@ -405,15 +414,15 @@ func Event_server_event_hendler(graph *path.IG, events *mtypes.SUPER_Events) {
httpobj.RLock()
PubKey := httpobj.http_PeerID2Info[NodeID].PubKey
if reg_msg.Node_id < mtypes.Special_NodeID {
httpobj.http_PeerState[PubKey].LastSeen = time.Now()
httpobj.http_PeerState[PubKey].JETSecret = reg_msg.JWTSecret
httpobj.http_PeerState[PubKey].httpPostCount = reg_msg.HttpPostCount
if httpobj.http_PeerState[PubKey].NhTableState == reg_msg.NhStateHash == false {
httpobj.http_PeerState[PubKey].NhTableState = reg_msg.NhStateHash
httpobj.http_PeerState[PubKey].LastSeen.Store(time.Now())
httpobj.http_PeerState[PubKey].JETSecret.Store(reg_msg.JWTSecret)
httpobj.http_PeerState[PubKey].httpPostCount.Store(reg_msg.HttpPostCount)
if httpobj.http_PeerState[PubKey].NhTableState.Load().(string) == reg_msg.NhStateHash == false {
httpobj.http_PeerState[PubKey].NhTableState.Store(reg_msg.NhStateHash)
should_push_nh = true
}
if httpobj.http_PeerState[PubKey].PeerInfoState == reg_msg.PeerStateHash == false {
httpobj.http_PeerState[PubKey].PeerInfoState = reg_msg.PeerStateHash
if httpobj.http_PeerState[PubKey].PeerInfoState.Load().(string) == reg_msg.PeerStateHash == false {
httpobj.http_PeerState[PubKey].PeerInfoState.Store(reg_msg.PeerStateHash)
should_push_peer = true
}
}
@ -507,11 +516,11 @@ func PushNhTable(force bool) {
header.SetTTL(0)
copy(buf[path.EgHeaderLen:], body)
for pkstr, peerstate := range httpobj.http_PeerState {
isAlive := peerstate.LastSeen.Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now())
isAlive := peerstate.LastSeen.Load().(time.Time).Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now())
if !isAlive {
continue
}
if force || peerstate.NhTableState != httpobj.http_NhTable_Hash {
if force || peerstate.NhTableState.Load().(string) != httpobj.http_NhTable_Hash {
if peer := httpobj.http_device4.LookupPeerByStr(pkstr); peer != nil && peer.GetEndpointDstStr() != "" {
httpobj.http_device4.SendPacket(peer, path.ServerUpdate, buf, device.MessageTransportOffsetContent)
}
@ -542,11 +551,11 @@ func PushPeerinfo(force bool) {
header.SetTTL(0)
copy(buf[path.EgHeaderLen:], body)
for pkstr, peerstate := range httpobj.http_PeerState {
isAlive := peerstate.LastSeen.Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now())
isAlive := peerstate.LastSeen.Load().(time.Time).Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now())
if !isAlive {
continue
}
if force || peerstate.PeerInfoState != httpobj.http_PeerInfo_hash {
if force || peerstate.PeerInfoState.Load().(string) != httpobj.http_PeerInfo_hash {
if peer := httpobj.http_device4.LookupPeerByStr(pkstr); peer != nil {
httpobj.http_device4.SendPacket(peer, path.ServerUpdate, buf, device.MessageTransportOffsetContent)
}
@ -577,11 +586,11 @@ func PushServerParams(force bool) {
header.SetTTL(0)
copy(buf[path.EgHeaderLen:], body)
for pkstr, peerstate := range httpobj.http_PeerState {
isAlive := peerstate.LastSeen.Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now())
isAlive := peerstate.LastSeen.Load().(time.Time).Add(mtypes.S2TD(httpobj.http_sconfig.PeerAliveTimeout)).After(time.Now())
if !isAlive {
continue
}
if force || peerstate.SuperParamState != httpobj.http_SuperParams_Hash {
if force || peerstate.SuperParamState.Load().(string) != httpobj.http_SuperParams_Hash {
if peer := httpobj.http_device4.LookupPeerByStr(pkstr); peer != nil {
httpobj.http_device4.SendPacket(peer, path.ServerUpdate, buf, device.MessageTransportOffsetContent)
}