This commit is contained in:
number571 2023-11-09 02:09:47 +07:00
parent 1e775eb88b
commit 89b50ad06a
18 changed files with 608 additions and 583 deletions

View File

@ -24,6 +24,10 @@
- Update `pkg`: _examples/ -> examples/
- Update `pkg/types`: move CloseAll, StopAll functions to internal/interrupt
### BUG FIXES
- Update `pkg/network`: rewrite inMapWithSet -> inQueueWithSet
<!-- ... -->
## v1.5.20

View File

@ -229,7 +229,7 @@ func testNewNetworkNode(addr string) network.INode {
return network.NewNode(
network.NewSettings(&network.SSettings{
FAddress: addr,
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: time.Minute,
FWriteTimeout: time.Minute,

View File

@ -36,7 +36,7 @@ func initNode(pCfg config.IConfig, pPrivKey asymmetric.IPrivKey, pLogger logger.
network.NewNode(
network.NewSettings(&network.SSettings{
FAddress: pCfg.GetAddress().GetTCP(),
FCapacity: pkg_settings.CNetworkCapacity,
FQueueSize: pkg_settings.CNetworkQueueSize,
FMaxConnects: pkg_settings.CNetworkMaxConns,
FReadTimeout: queueDuration,
FWriteTimeout: queueDuration,

View File

@ -59,8 +59,8 @@ const (
)
const (
CNetworkCapacity = (1 << 10) // 1024 hashes
CNetworkMaxConns = (1 << 8) // 256 conns
CNetworkQueueSize = (1 << 10) // 1024 hashes
CNetworkMaxConns = (1 << 8) // 256 conns
)
const (

View File

@ -116,7 +116,7 @@ func testRunService(wDB database.IWrapperDB, addr string, addrNode string) (*htt
node := network.NewNode(
network.NewSettings(&network.SSettings{
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: time.Minute,
FWriteTimeout: time.Minute,
@ -161,7 +161,7 @@ func testNewNetworkNode(addr string) network.INode {
return network.NewNode(
network.NewSettings(&network.SSettings{
FAddress: addr,
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: time.Minute,
FWriteTimeout: time.Minute,

View File

@ -29,7 +29,7 @@ func initNode(pCfg config.IConfig, pWrapperDB database.IWrapperDB, pLogger logge
network.NewSettings(&network.SSettings{
FAddress: pCfg.GetAddress().GetTCP(),
FMaxConnects: hls_settings.CNetworkMaxConns,
FCapacity: hls_settings.CNetworkCapacity,
FQueueSize: hls_settings.CNetworkQueueSize,
FReadTimeout: queueDuration,
FWriteTimeout: queueDuration,
FConnSettings: conn.NewSettings(&conn.SSettings{

View File

@ -710,7 +710,7 @@ func testNewNode(timeWait time.Duration, addr string, typeDB, numDB int) INode {
network.NewNode(
network.NewSettings(&network.SSettings{
FAddress: addr,
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: timeWait,
FWriteTimeout: timeWait,

View File

@ -137,7 +137,7 @@ func newNode(serviceAddress, name, dbPath string) anonymity.INode {
func nodeSettings(serviceAddress string) network.ISettings {
return network.NewSettings(&network.SSettings{
FAddress: serviceAddress,
FCapacity: (1 << 10),
FQueueSize: (1 << 10),
FMaxConnects: 1,
FConnSettings: connSettings(),
FWriteTimeout: time.Minute,

View File

@ -175,7 +175,7 @@ func newNode(serviceAddress, name, dbPath string) anonymity.INode {
func nodeSettings(serviceAddress string) network.ISettings {
return network.NewSettings(&network.SSettings{
FAddress: serviceAddress,
FCapacity: (1 << 10),
FQueueSize: (1 << 10),
FMaxConnects: 1,
FConnSettings: connSettings(),
FWriteTimeout: time.Minute,

View File

@ -96,7 +96,7 @@ func newTestConnKeeper(pDuration time.Duration) IConnKeeper {
FDuration: pDuration,
}),
network.NewNode(network.NewSettings(&network.SSettings{
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: time.Minute,
FWriteTimeout: time.Minute,

View File

@ -64,7 +64,7 @@ func handler(serviceName string) network.IHandlerF {
func nodeSettings(serviceAddress string) network.ISettings {
return network.NewSettings(&network.SSettings{
FAddress: serviceAddress,
FCapacity: (1 << 10),
FQueueSize: (1 << 10),
FMaxConnects: 2,
FConnSettings: connSettings(),
FWriteTimeout: time.Minute,

View File

@ -68,7 +68,7 @@ func handler() network.IHandlerF {
func nodeSettings(serviceAddress string) network.ISettings {
return network.NewSettings(&network.SSettings{
FAddress: serviceAddress,
FCapacity: (1 << 10),
FQueueSize: (1 << 10),
FMaxConnects: 1,
FConnSettings: connSettings(),
FWriteTimeout: time.Minute,

View File

@ -78,7 +78,7 @@ func handler(serviceName string) network.IHandlerF {
func nodeSettings(serviceAddress string) network.ISettings {
return network.NewSettings(&network.SSettings{
FAddress: serviceAddress,
FCapacity: (1 << 10),
FQueueSize: (1 << 10),
FMaxConnects: 1,
FConnSettings: connSettings(),
FWriteTimeout: time.Minute,

View File

@ -20,20 +20,34 @@ type sNode struct {
fMutex sync.Mutex
fListener net.Listener
fSettings ISettings
fHashMapping map[string]struct{}
fQueueMap *sQueueMap
fConnections map[string]conn.IConn
fHandleRoutes map[uint64]IHandlerF
}
type sQueueMap struct {
fMutex sync.Mutex
fMap map[string]struct{}
fQueue []string
fIndex int
}
// Creating a node object managed by connections with multiple nodes.
// Saves hashes of received messages to a buffer to prevent network cycling.
// Redirects messages to handle routers by keys.
func NewNode(pSett ISettings) INode {
return &sNode{
fSettings: pSett,
fHashMapping: make(map[string]struct{}, pSett.GetCapacity()),
fConnections: make(map[string]conn.IConn),
fHandleRoutes: make(map[uint64]IHandlerF),
fQueueMap: newQueueMap(pSett.GetQueueSize()),
fConnections: make(map[string]conn.IConn, pSett.GetMaxConnects()),
fHandleRoutes: make(map[uint64]IHandlerF, 128),
}
}
func newQueueMap(pSize uint64) *sQueueMap {
return &sQueueMap{
fQueue: make([]string, pSize),
fMap: make(map[string]struct{}, pSize),
}
}
@ -44,7 +58,7 @@ func (p *sNode) GetSettings() ISettings {
// Puts the hash of the message in the buffer and sends the message to all connections of the node.
func (p *sNode) BroadcastMessage(pMsg message.IMessage) error {
_ = p.inMappingWithSet(pMsg.GetHash()) // node can redirect received message
_ = p.inQueueWithSet(pMsg.GetHash()) // node can redirect received message
listErr := make([]error, 0, p.fSettings.GetMaxConnects())
@ -253,7 +267,7 @@ func (p *sNode) handleConn(pAddress string, pConn conn.IConn) {
// > or if the message already existed in the hash value store.
func (p *sNode) handleMessage(pConn conn.IConn, pMsg message.IMessage) bool {
// check message in mapping by hash
if p.inMappingWithSet(pMsg.GetHash()) {
if p.inQueueWithSet(pMsg.GetHash()) {
return true
}
@ -284,19 +298,25 @@ func (p *sNode) hasMaxConnSize() bool {
// Checks the hash of the message for existence in the hash store.
// Returns true if the hash already existed, otherwise false.
func (p *sNode) inMappingWithSet(pHash []byte) bool {
p.fMutex.Lock()
defer p.fMutex.Unlock()
func (p *sNode) inQueueWithSet(pHash []byte) bool {
p.fQueueMap.fMutex.Lock()
defer p.fQueueMap.fMutex.Unlock()
// hash already exists in queue
sHash := encoding.HexEncode(pHash)
// skey already exists
if _, ok := p.fHashMapping[sHash]; ok {
if _, ok := p.fQueueMap.fMap[sHash]; ok {
return true
}
// push skey to mapping
p.fHashMapping[sHash] = struct{}{}
// delete old value in queue
delete(p.fQueueMap.fMap, p.fQueueMap.fQueue[p.fQueueMap.fIndex])
// push hash to queue
p.fQueueMap.fQueue[p.fQueueMap.fIndex] = sHash
p.fQueueMap.fMap[sHash] = struct{}{}
// increment queue index
p.fQueueMap.fIndex = (p.fQueueMap.fIndex + 1) % len(p.fQueueMap.fQueue)
return false
}

View File

@ -50,7 +50,7 @@ func testSettings(t *testing.T, n int) {
case 1:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FReadTimeout: tcTimeWait,
FWriteTimeout: tcTimeWait,
FConnSettings: conn.NewSettings(&conn.SSettings{
@ -63,7 +63,7 @@ func testSettings(t *testing.T, n int) {
case 2:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FWriteTimeout: tcTimeWait,
FConnSettings: conn.NewSettings(&conn.SSettings{
@ -76,7 +76,7 @@ func testSettings(t *testing.T, n int) {
case 3:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: tcTimeWait,
FConnSettings: conn.NewSettings(&conn.SSettings{
@ -89,7 +89,7 @@ func testSettings(t *testing.T, n int) {
case 4:
_ = NewSettings(&SSettings{
FAddress: "test",
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: testutils.TCMaxConnects,
FReadTimeout: tcTimeWait,
FWriteTimeout: tcTimeWait,
@ -402,7 +402,7 @@ func testNodes() ([5]INode, map[INode]map[string]bool, error) {
func newTestNode(pAddr string, pMaxConns uint64, timeout time.Duration) INode {
sett := NewSettings(&SSettings{
FAddress: pAddr,
FCapacity: testutils.TCCapacity,
FQueueSize: testutils.TCCapacity,
FMaxConnects: pMaxConns,
FReadTimeout: timeout,
FWriteTimeout: timeout,

View File

@ -13,7 +13,7 @@ var (
type SSettings sSettings
type sSettings struct {
FAddress string
FCapacity uint64
FQueueSize uint64
FMaxConnects uint64
FReadTimeout time.Duration
FWriteTimeout time.Duration
@ -23,7 +23,7 @@ type sSettings struct {
func NewSettings(pSett *SSettings) ISettings {
return (&sSettings{
FAddress: pSett.FAddress,
FCapacity: pSett.FCapacity,
FQueueSize: pSett.FQueueSize,
FMaxConnects: pSett.FMaxConnects,
FReadTimeout: pSett.FReadTimeout,
FWriteTimeout: pSett.FWriteTimeout,
@ -32,8 +32,8 @@ func NewSettings(pSett *SSettings) ISettings {
}
func (p *sSettings) mustNotNull() ISettings {
if p.FCapacity == 0 {
panic(`p.FCapacity == 0`)
if p.FQueueSize == 0 {
panic(`p.FQueueSize == 0`)
}
if p.FMaxConnects == 0 {
panic(`p.FMaxConnects == 0`)
@ -54,8 +54,8 @@ func (p *sSettings) GetAddress() string {
return p.FAddress
}
func (p *sSettings) GetCapacity() uint64 {
return p.FCapacity
func (p *sSettings) GetQueueSize() uint64 {
return p.FQueueSize
}
func (p *sSettings) GetMaxConnects() uint64 {

View File

@ -27,7 +27,7 @@ type INode interface {
type ISettings interface {
GetAddress() string
GetCapacity() uint64
GetQueueSize() uint64
GetMaxConnects() uint64
GetReadTimeout() time.Duration
GetWriteTimeout() time.Duration

File diff suppressed because it is too large Load Diff