mirror of
https://github.com/number571/go-peer.git
synced 2026-09-12 19:50:00 +05:00
update
This commit is contained in:
parent
dd52c2a424
commit
b7fcca11d4
@ -9,10 +9,13 @@
|
||||
- Update `pkg/network/anonymity/queue`: WithNetworkSettings -> SetNetworkSettings
|
||||
- Update `examles`: delete not docker examples in anon_filesharing, anon_messenger, echo_service
|
||||
- Update `cmd/hidden_lake`: delete docker run from makefiles
|
||||
- Update `pkg/network`: delete conn.settings.SetNetworkKey, append conn.SetVSettings
|
||||
- Update `pkg/network/anonymity/queue`: move networkMask, networkKey params to IVSettings
|
||||
|
||||
### BUG FIXES
|
||||
|
||||
- Update `cmd/hidden_lake/composite`: fix install_hlc.sh
|
||||
- Update `pkg/network/message`: fix payloadLength
|
||||
|
||||
## v1.6.6
|
||||
|
||||
|
||||
1
TODO.md
1
TODO.md
@ -13,6 +13,7 @@
|
||||
11. config _ms -> yaml ms
|
||||
12. conn.NewConn(sett, pNetworkKey, pAddress)
|
||||
13. Update HL scheme
|
||||
14. pkg: RWMutex
|
||||
|
||||
### Tests
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -122,7 +122,6 @@ func testRunService(wDB database.IDBWrapper, addr string, addrNode string) (*htt
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
FConnSettings: conn.NewSettings(&conn.SSettings{
|
||||
FNetworkKey: testutils.TCNetworkKey,
|
||||
FWorkSizeBits: testutils.TCWorkSize,
|
||||
FLimitMessageSizeBytes: testutils.TCMessageSize,
|
||||
FWaitReadTimeout: time.Hour,
|
||||
@ -131,6 +130,9 @@ func testRunService(wDB database.IDBWrapper, addr string, addrNode string) (*htt
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: testutils.TCNetworkKey,
|
||||
}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: testutils.TCCapacity,
|
||||
@ -179,7 +181,6 @@ func testNewNetworkNode(addr string) network.INode {
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
FConnSettings: conn.NewSettings(&conn.SSettings{
|
||||
FNetworkKey: testutils.TCNetworkKey,
|
||||
FWorkSizeBits: testutils.TCWorkSize,
|
||||
FLimitMessageSizeBytes: testutils.TCMessageSize,
|
||||
FWaitReadTimeout: time.Hour,
|
||||
@ -188,6 +189,9 @@ func testNewNetworkNode(addr string) network.INode {
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: testutils.TCNetworkKey,
|
||||
}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: testutils.TCCapacity,
|
||||
|
||||
@ -67,7 +67,10 @@ func HandleMessageAPI(pCtx context.Context, pCfg config.IConfig, pDBWrapper data
|
||||
}
|
||||
|
||||
netMsg, err := net_message.LoadMessage(
|
||||
pNode.GetSettings().GetConnSettings(),
|
||||
net_message.NewSettings(&net_message.SSettings{
|
||||
FNetworkKey: pNode.GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: pNode.GetSettings().GetConnSettings().GetWorkSizeBits(),
|
||||
}),
|
||||
string(msgStringAsBytes),
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
@ -30,7 +30,6 @@ func initNode(pCfg config.IConfig, pDBWrapper database.IDBWrapper, pLogger logge
|
||||
FReadTimeout: queueDuration,
|
||||
FWriteTimeout: queueDuration,
|
||||
FConnSettings: conn.NewSettings(&conn.SSettings{
|
||||
FNetworkKey: cfgSettings.GetNetworkKey(),
|
||||
FWorkSizeBits: cfgSettings.GetWorkSizeBits(),
|
||||
FLimitMessageSizeBytes: cfgSettings.GetMessageSizeBytes(),
|
||||
FLimitVoidSizeBytes: cfgSettings.GetLimitVoidSizeBytes(),
|
||||
@ -40,6 +39,9 @@ func initNode(pCfg config.IConfig, pDBWrapper database.IDBWrapper, pLogger logge
|
||||
FWriteTimeout: queueDuration,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: cfgSettings.GetNetworkKey(),
|
||||
}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: hls_settings.CNetworkQueueCapacity,
|
||||
|
||||
@ -11,6 +11,8 @@ import (
|
||||
http_logger "github.com/number571/go-peer/internal/logger/http"
|
||||
"github.com/number571/go-peer/pkg/logger"
|
||||
"github.com/number571/go-peer/pkg/network/anonymity"
|
||||
"github.com/number571/go-peer/pkg/network/anonymity/queue"
|
||||
"github.com/number571/go-peer/pkg/network/conn"
|
||||
)
|
||||
|
||||
func HandleConfigSettingsAPI(pWrapper config.IWrapper, pLogger logger.ILogger, pNode anonymity.INode) http.HandlerFunc {
|
||||
@ -58,8 +60,12 @@ func HandleConfigSettingsAPI(pWrapper config.IWrapper, pLogger logger.ILogger, p
|
||||
return
|
||||
}
|
||||
|
||||
pNode.GetNetworkNode().GetSettings().GetConnSettings().SetNetworkKey(networkKey)
|
||||
pNode.GetMessageQueue().SetNetworkSettings(pkg_settings.CNetworkMask, networkKey)
|
||||
pNode.GetNetworkNode().SetVSettings(
|
||||
conn.NewVSettings(&conn.SVSettings{FNetworkKey: networkKey}),
|
||||
)
|
||||
pNode.GetMessageQueue().SetVSettings(
|
||||
queue.NewVSettings(&queue.SVSettings{FNetworkKey: networkKey}),
|
||||
)
|
||||
|
||||
pLogger.PushInfo(logBuilder.WithMessage(http_logger.CLogSuccess))
|
||||
api.Response(pW, http.StatusOK, "success: set network key")
|
||||
|
||||
@ -203,6 +203,7 @@ func testNewNode(dbPath, addr string) anonymity.INode {
|
||||
FParallel: 1,
|
||||
FDuration: 500 * time.Millisecond,
|
||||
}),
|
||||
queue.NewVSettings(&queue.SVSettings{}),
|
||||
client.NewClient(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FMessageSizeBytes: testutils.TCMessageSize,
|
||||
@ -232,6 +233,7 @@ func testNewNetworkNode(addr string) network.INode {
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: testutils.TCCapacity,
|
||||
|
||||
@ -55,7 +55,6 @@ func initNode(
|
||||
FReadTimeout: queueMaxDuration,
|
||||
FWriteTimeout: queueMaxDuration,
|
||||
FConnSettings: conn.NewSettings(&conn.SSettings{
|
||||
FNetworkKey: cfgSettings.GetNetworkKey(),
|
||||
FWorkSizeBits: cfgSettings.GetWorkSizeBits(),
|
||||
FLimitMessageSizeBytes: cfgSettings.GetMessageSizeBytes(),
|
||||
FLimitVoidSizeBytes: cfgSettings.GetLimitVoidSizeBytes(),
|
||||
@ -65,6 +64,9 @@ func initNode(
|
||||
FWriteTimeout: queueMaxDuration,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: cfgSettings.GetNetworkKey(),
|
||||
}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: hls_settings.CNetworkQueueCapacity,
|
||||
@ -74,7 +76,6 @@ func initNode(
|
||||
queue.NewMessageQueue(
|
||||
queue.NewSettings(&queue.SSettings{
|
||||
FNetworkMask: hls_settings.CNetworkMask,
|
||||
FNetworkKey: cfgSettings.GetNetworkKey(),
|
||||
FWorkSizeBits: cfgSettings.GetWorkSizeBits(),
|
||||
FMainCapacity: hls_settings.CQueueMainCapacity,
|
||||
FVoidCapacity: hls_settings.CQueueVoidCapacity,
|
||||
@ -83,6 +84,9 @@ func initNode(
|
||||
FDuration: queueDuration,
|
||||
FRandDuration: queueRandDuration,
|
||||
}),
|
||||
queue.NewVSettings(&queue.SVSettings{
|
||||
FNetworkKey: cfgSettings.GetNetworkKey(),
|
||||
}),
|
||||
client.NewClient(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FMessageSizeBytes: cfgSettings.GetMessageSizeBytes(),
|
||||
|
||||
@ -25,6 +25,8 @@ type tsConn struct{}
|
||||
|
||||
func (p *tsConn) Close() error { return nil }
|
||||
func (p *tsConn) GetSettings() conn.ISettings { return nil }
|
||||
func (p *tsConn) GetVSettings() conn.IVSettings { return nil }
|
||||
func (p *tsConn) SetVSettings(conn.IVSettings) {}
|
||||
func (p *tsConn) GetSocket() net.Conn { return &tsNetConn{} }
|
||||
func (p *tsConn) WriteMessage(context.Context, net_message.IMessage) error { return nil }
|
||||
func (p *tsConn) ReadMessage(context.Context, chan<- struct{}) (net_message.IMessage, error) {
|
||||
|
||||
@ -5,4 +5,6 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type IServiceF func(context.Context, *sync.WaitGroup, chan<- error)
|
||||
type (
|
||||
IServiceF func(context.Context, *sync.WaitGroup, chan<- error)
|
||||
)
|
||||
|
||||
@ -797,7 +797,6 @@ func testNewNode(timeWait time.Duration, addr string, typeDB, numDB, retryNum in
|
||||
FReadTimeout: timeWait,
|
||||
FWriteTimeout: timeWait,
|
||||
FConnSettings: conn.NewSettings(&conn.SSettings{
|
||||
FNetworkKey: networkKey,
|
||||
FWorkSizeBits: testutils.TCWorkSize,
|
||||
FLimitMessageSizeBytes: testutils.TCMessageSize,
|
||||
FLimitVoidSizeBytes: limitVoidSize,
|
||||
@ -807,6 +806,9 @@ func testNewNode(timeWait time.Duration, addr string, typeDB, numDB, retryNum in
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: networkKey,
|
||||
}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: testutils.TCCapacity,
|
||||
@ -816,7 +818,6 @@ func testNewNode(timeWait time.Duration, addr string, typeDB, numDB, retryNum in
|
||||
queue.NewMessageQueue(
|
||||
queue.NewSettings(&queue.SSettings{
|
||||
FNetworkMask: networkMask,
|
||||
FNetworkKey: networkKey,
|
||||
FWorkSizeBits: testutils.TCWorkSize,
|
||||
FMainCapacity: testutils.TCQueueCapacity,
|
||||
FVoidCapacity: testutils.TCQueueCapacity,
|
||||
@ -824,6 +825,9 @@ func testNewNode(timeWait time.Duration, addr string, typeDB, numDB, retryNum in
|
||||
FLimitVoidSizeBytes: limitVoidSize,
|
||||
FDuration: time.Second,
|
||||
}),
|
||||
queue.NewVSettings(&queue.SVSettings{
|
||||
FNetworkKey: networkKey,
|
||||
}),
|
||||
client.NewClient(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FMessageSizeBytes: testutils.TCMessageSize,
|
||||
|
||||
@ -77,8 +77,9 @@ func newNode(serviceName, address string) anonymity.INode {
|
||||
FMaxConnects: 256,
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
FConnSettings: newConnSettings(msgSize, workSize, networkKey),
|
||||
FConnSettings: newConnSettings(workSize, msgSize),
|
||||
}),
|
||||
newVSettings(networkKey),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: 1024,
|
||||
@ -88,13 +89,15 @@ func newNode(serviceName, address string) anonymity.INode {
|
||||
queue.NewMessageQueue(
|
||||
queue.NewSettings(&queue.SSettings{
|
||||
FNetworkMask: networkMask,
|
||||
FNetworkKey: networkKey,
|
||||
FWorkSizeBits: workSize,
|
||||
FDuration: 2 * time.Second,
|
||||
FParallel: 1,
|
||||
FMainCapacity: 32,
|
||||
FVoidCapacity: 32,
|
||||
}),
|
||||
queue.NewVSettings(&queue.SVSettings{
|
||||
FNetworkKey: networkKey,
|
||||
}),
|
||||
client.NewClient(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FKeySizeBits: keySize,
|
||||
@ -107,9 +110,14 @@ func newNode(serviceName, address string) anonymity.INode {
|
||||
)
|
||||
}
|
||||
|
||||
func newConnSettings(mSize, wSize uint64, nKey string) conn.ISettings {
|
||||
func newVSettings(nKey string) conn.IVSettings {
|
||||
return conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: nKey,
|
||||
})
|
||||
}
|
||||
|
||||
func newConnSettings(wSize, mSize uint64) conn.ISettings {
|
||||
return conn.NewSettings(&conn.SSettings{
|
||||
FNetworkKey: nKey,
|
||||
FWorkSizeBits: wSize,
|
||||
FLimitMessageSizeBytes: mSize,
|
||||
FLimitVoidSizeBytes: 8192,
|
||||
|
||||
@ -77,8 +77,9 @@ func newNode(serviceName, address string) anonymity.INode {
|
||||
FMaxConnects: 256,
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
FConnSettings: newConnSettings(msgSize, workSize, networkKey),
|
||||
FConnSettings: newConnSettings(workSize, msgSize),
|
||||
}),
|
||||
newVSettings(networkKey),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: 1024,
|
||||
@ -88,13 +89,15 @@ func newNode(serviceName, address string) anonymity.INode {
|
||||
queue.NewMessageQueue(
|
||||
queue.NewSettings(&queue.SSettings{
|
||||
FNetworkMask: networkMask,
|
||||
FNetworkKey: networkKey,
|
||||
FWorkSizeBits: workSize,
|
||||
FDuration: 2 * time.Second,
|
||||
FParallel: 1,
|
||||
FMainCapacity: 32,
|
||||
FVoidCapacity: 32,
|
||||
}),
|
||||
queue.NewVSettings(&queue.SVSettings{
|
||||
FNetworkKey: networkKey,
|
||||
}),
|
||||
client.NewClient(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FKeySizeBits: keySize,
|
||||
@ -107,9 +110,14 @@ func newNode(serviceName, address string) anonymity.INode {
|
||||
)
|
||||
}
|
||||
|
||||
func newConnSettings(mSize, wSize uint64, nKey string) conn.ISettings {
|
||||
func newVSettings(nKey string) conn.IVSettings {
|
||||
return conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: nKey,
|
||||
})
|
||||
}
|
||||
|
||||
func newConnSettings(wSize uint64, mSize uint64) conn.ISettings {
|
||||
return conn.NewSettings(&conn.SSettings{
|
||||
FNetworkKey: nKey,
|
||||
FWorkSizeBits: wSize,
|
||||
FLimitMessageSizeBytes: mSize,
|
||||
FLimitVoidSizeBytes: 8192,
|
||||
|
||||
@ -26,6 +26,7 @@ func main() {
|
||||
FMainCapacity: 1 << 5,
|
||||
FVoidCapacity: 1 << 5,
|
||||
}),
|
||||
queue.NewVSettings(&queue.SVSettings{}),
|
||||
client.NewClient(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FMessageSizeBytes: (1 << 12),
|
||||
|
||||
@ -24,11 +24,9 @@ type sMessageQueue struct {
|
||||
fMutex sync.RWMutex
|
||||
fState state.IState
|
||||
|
||||
fSettings ISettings
|
||||
fClient client.IClient
|
||||
|
||||
fNetworkMask uint64
|
||||
fNetworkKey string
|
||||
fSettings ISettings
|
||||
fVSettings IVSettings
|
||||
fClient client.IClient
|
||||
|
||||
fMainPool *sMainPool
|
||||
fVoidPool *sVoidPool
|
||||
@ -46,19 +44,18 @@ type sVoidPool struct {
|
||||
fReceiver asymmetric.IPubKey
|
||||
}
|
||||
|
||||
func NewMessageQueue(pSett ISettings, pClient client.IClient) IMessageQueue {
|
||||
func NewMessageQueue(pSettings ISettings, pVSettings IVSettings, pClient client.IClient) IMessageQueue {
|
||||
return &sMessageQueue{
|
||||
fState: state.NewBoolState(),
|
||||
fSettings: pSett,
|
||||
fClient: pClient,
|
||||
fNetworkMask: pSett.GetNetworkMask(),
|
||||
fNetworkKey: pSett.GetNetworkKey(),
|
||||
fState: state.NewBoolState(),
|
||||
fSettings: pSettings,
|
||||
fVSettings: pVSettings,
|
||||
fClient: pClient,
|
||||
fMainPool: &sMainPool{
|
||||
fQueue: make(chan net_message.IMessage, pSett.GetMainCapacity()),
|
||||
fRawQueue: make(chan message.IMessage, pSett.GetMainCapacity()),
|
||||
fQueue: make(chan net_message.IMessage, pSettings.GetMainCapacity()),
|
||||
fRawQueue: make(chan message.IMessage, pSettings.GetMainCapacity()),
|
||||
},
|
||||
fVoidPool: &sVoidPool{
|
||||
fQueue: make(chan net_message.IMessage, pSett.GetVoidCapacity()),
|
||||
fQueue: make(chan net_message.IMessage, pSettings.GetVoidCapacity()),
|
||||
fReceiver: asymmetric.NewRSAPrivKey(pClient.GetPrivKey().GetSize()).GetPubKey(),
|
||||
},
|
||||
}
|
||||
@ -68,6 +65,10 @@ func (p *sMessageQueue) GetSettings() ISettings {
|
||||
return p.fSettings
|
||||
}
|
||||
|
||||
func (p *sMessageQueue) GetVSettings() IVSettings {
|
||||
return p.getVSettings()
|
||||
}
|
||||
|
||||
func (p *sMessageQueue) GetClient() client.IClient {
|
||||
return p.fClient
|
||||
}
|
||||
@ -129,12 +130,11 @@ func (p *sMessageQueue) runMainPoolFiller(pCtx context.Context, pWg *sync.WaitGr
|
||||
}
|
||||
}
|
||||
|
||||
func (p *sMessageQueue) SetNetworkSettings(pNetworkMask uint64, pNetworkKey string) {
|
||||
func (p *sMessageQueue) SetVSettings(pVSettings IVSettings) {
|
||||
p.fMutex.Lock()
|
||||
defer p.fMutex.Unlock()
|
||||
|
||||
p.fNetworkMask = pNetworkMask
|
||||
p.fNetworkKey = pNetworkKey
|
||||
p.fVSettings = pVSettings
|
||||
|
||||
// clear all old queue state
|
||||
// not clear fMainPool.RawQueue
|
||||
@ -189,17 +189,17 @@ func (p *sMessageQueue) DequeueMessage(pCtx context.Context) net_message.IMessag
|
||||
}
|
||||
|
||||
func (p *sMessageQueue) fillMainPool(pCtx context.Context, pMsg message.IMessage) error {
|
||||
oldNetworkMask, oldNetworkKey := p.getNetworkSettings()
|
||||
oldNSettings := p.getVSettings()
|
||||
chNetMsg := make(chan net_message.IMessage)
|
||||
|
||||
go func() {
|
||||
chNetMsg <- net_message.NewMessage(
|
||||
net_message.NewSettings(&net_message.SSettings{
|
||||
FWorkSizeBits: p.fSettings.GetWorkSizeBits(),
|
||||
FNetworkKey: oldNetworkKey,
|
||||
FNetworkKey: oldNSettings.GetNetworkKey(),
|
||||
}),
|
||||
payload.NewPayload(
|
||||
oldNetworkMask,
|
||||
p.fSettings.GetNetworkMask(),
|
||||
pMsg.ToBytes(),
|
||||
),
|
||||
p.fSettings.GetParallel(),
|
||||
@ -212,10 +212,7 @@ func (p *sMessageQueue) fillMainPool(pCtx context.Context, pMsg message.IMessage
|
||||
return pCtx.Err()
|
||||
|
||||
case netMsg := <-chNetMsg:
|
||||
newNetworkMask, newNetworkKey := p.getNetworkSettings()
|
||||
settingsChanged := (newNetworkMask != oldNetworkMask) || (newNetworkKey != oldNetworkKey)
|
||||
|
||||
if !settingsChanged {
|
||||
if p.nSettingsNotChanged(oldNSettings) {
|
||||
p.fMainPool.fQueue <- netMsg
|
||||
}
|
||||
return nil
|
||||
@ -242,16 +239,16 @@ func (p *sMessageQueue) fillVoidPool(pCtx context.Context) error {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
oldNetworkMask, oldNetworkKey := p.getNetworkSettings()
|
||||
oldNSettings := p.getVSettings()
|
||||
chNetMsg := make(chan net_message.IMessage)
|
||||
go func() {
|
||||
chNetMsg <- net_message.NewMessage(
|
||||
net_message.NewSettings(&net_message.SSettings{
|
||||
FWorkSizeBits: p.fSettings.GetWorkSizeBits(),
|
||||
FNetworkKey: oldNetworkKey,
|
||||
FNetworkKey: oldNSettings.GetNetworkKey(),
|
||||
}),
|
||||
payload.NewPayload(
|
||||
oldNetworkMask,
|
||||
p.fSettings.GetNetworkMask(),
|
||||
msg.ToBytes(),
|
||||
),
|
||||
p.fSettings.GetParallel(),
|
||||
@ -264,19 +261,21 @@ func (p *sMessageQueue) fillVoidPool(pCtx context.Context) error {
|
||||
return pCtx.Err()
|
||||
|
||||
case netMsg := <-chNetMsg:
|
||||
newNetworkMask, newNetworkKey := p.getNetworkSettings()
|
||||
settingsChanged := (newNetworkMask != oldNetworkMask) || (newNetworkKey != oldNetworkKey)
|
||||
|
||||
if !settingsChanged {
|
||||
if p.nSettingsNotChanged(oldNSettings) {
|
||||
p.fVoidPool.fQueue <- netMsg
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *sMessageQueue) getNetworkSettings() (uint64, string) {
|
||||
func (p *sMessageQueue) getVSettings() IVSettings {
|
||||
p.fMutex.RLock()
|
||||
defer p.fMutex.RUnlock()
|
||||
|
||||
return p.fNetworkMask, p.fNetworkKey
|
||||
return p.fVSettings
|
||||
}
|
||||
|
||||
func (p *sMessageQueue) nSettingsNotChanged(oldNSettings IVSettings) bool {
|
||||
currNSettings := p.getVSettings()
|
||||
return currNSettings.GetNetworkKey() == oldNSettings.GetNetworkKey()
|
||||
}
|
||||
|
||||
@ -76,6 +76,7 @@ func TestRunStopQueue(t *testing.T) {
|
||||
FParallel: 1,
|
||||
FDuration: 100 * time.Millisecond,
|
||||
}),
|
||||
NewVSettings(&SVSettings{}),
|
||||
client,
|
||||
)
|
||||
|
||||
@ -144,7 +145,6 @@ func TestQueue(t *testing.T) {
|
||||
queue := NewMessageQueue(
|
||||
NewSettings(&SSettings{
|
||||
FNetworkMask: 1,
|
||||
FNetworkKey: "old_network_key",
|
||||
FWorkSizeBits: 10,
|
||||
FMainCapacity: testutils.TCQueueCapacity,
|
||||
FVoidCapacity: testutils.TCQueueCapacity,
|
||||
@ -152,6 +152,9 @@ func TestQueue(t *testing.T) {
|
||||
FDuration: 100 * time.Millisecond,
|
||||
FRandDuration: 100 * time.Millisecond,
|
||||
}),
|
||||
NewVSettings(&SVSettings{
|
||||
FNetworkKey: "old_network_key",
|
||||
}),
|
||||
client.NewClient(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FMessageSizeBytes: testutils.TCMessageSize,
|
||||
@ -203,15 +206,19 @@ func testQueue(queue IMessageQueue) error {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
|
||||
// clear old messages
|
||||
queue.SetNetworkSettings(3, "new_network_key")
|
||||
newNetworkKey := "new_network_key"
|
||||
queue.SetVSettings(NewVSettings(&SVSettings{
|
||||
FNetworkKey: newNetworkKey,
|
||||
}))
|
||||
|
||||
nVSettings := queue.GetVSettings()
|
||||
if nVSettings.GetNetworkKey() != newNetworkKey {
|
||||
return errors.New("incorrect set variable settings")
|
||||
}
|
||||
|
||||
msgs := make([]net_message.IMessage, 0, 3)
|
||||
for i := 0; i < 3; i++ {
|
||||
msg := queue.DequeueMessage(ctx)
|
||||
if msg.GetPayload().GetHead() != 3 {
|
||||
return errors.New("got invalid header")
|
||||
}
|
||||
msgs = append(msgs, msg)
|
||||
msgs = append(msgs, queue.DequeueMessage(ctx))
|
||||
}
|
||||
|
||||
for i := 0; i < len(msgs)-1; i++ {
|
||||
|
||||
@ -11,7 +11,6 @@ var (
|
||||
type SSettings sSettings
|
||||
type sSettings struct {
|
||||
FNetworkMask uint64
|
||||
FNetworkKey string
|
||||
FWorkSizeBits uint64
|
||||
FMainCapacity uint64
|
||||
FVoidCapacity uint64
|
||||
@ -24,7 +23,6 @@ type sSettings struct {
|
||||
func NewSettings(pSett *SSettings) ISettings {
|
||||
return (&sSettings{
|
||||
FNetworkMask: pSett.FNetworkMask,
|
||||
FNetworkKey: pSett.FNetworkKey,
|
||||
FWorkSizeBits: pSett.FWorkSizeBits,
|
||||
FMainCapacity: pSett.FMainCapacity,
|
||||
FVoidCapacity: pSett.FVoidCapacity,
|
||||
@ -48,8 +46,7 @@ func (p *sSettings) mustNotNull() ISettings {
|
||||
if p.FDuration == 0 {
|
||||
panic(`p.FDuration == 0`)
|
||||
}
|
||||
// pFNetworkMask, p.FNetworkKey, FWorkSizeBits.FWorkSizeBits,
|
||||
// p.FRandDuration, p.FLimitVoidSizeBytes can be = 0
|
||||
// p.FNetworkMask, p.FWorkSizeBits, p.FRandDuration, p.FLimitVoidSizeBytes can be = 0
|
||||
return p
|
||||
}
|
||||
|
||||
@ -57,10 +54,6 @@ func (p *sSettings) GetNetworkMask() uint64 {
|
||||
return p.FNetworkMask
|
||||
}
|
||||
|
||||
func (p *sSettings) GetNetworkKey() string {
|
||||
return p.FNetworkKey
|
||||
}
|
||||
|
||||
func (p *sSettings) GetWorkSizeBits() uint64 {
|
||||
return p.FWorkSizeBits
|
||||
}
|
||||
|
||||
@ -12,7 +12,9 @@ import (
|
||||
|
||||
type IMessageQueue interface {
|
||||
types.IRunner
|
||||
SetNetworkSettings(uint64, string)
|
||||
|
||||
SetVSettings(IVSettings)
|
||||
GetVSettings() IVSettings
|
||||
|
||||
GetSettings() ISettings
|
||||
GetClient() client.IClient
|
||||
@ -22,9 +24,8 @@ type IMessageQueue interface {
|
||||
}
|
||||
|
||||
type ISettings interface {
|
||||
net_message.ISettings
|
||||
GetNetworkMask() uint64
|
||||
|
||||
GetWorkSizeBits() uint64
|
||||
GetMainCapacity() uint64
|
||||
GetVoidCapacity() uint64
|
||||
GetParallel() uint64
|
||||
@ -32,3 +33,7 @@ type ISettings interface {
|
||||
GetRandDuration() time.Duration
|
||||
GetLimitVoidSizeBytes() uint64
|
||||
}
|
||||
|
||||
type IVSettings interface {
|
||||
GetNetworkKey() string
|
||||
}
|
||||
|
||||
25
pkg/network/anonymity/queue/v_settings.go
Normal file
25
pkg/network/anonymity/queue/v_settings.go
Normal file
@ -0,0 +1,25 @@
|
||||
package queue
|
||||
|
||||
var (
|
||||
_ ISettings = &sSettings{}
|
||||
)
|
||||
|
||||
type SVSettings sVSettings
|
||||
type sVSettings struct {
|
||||
FNetworkKey string
|
||||
}
|
||||
|
||||
func NewVSettings(pSett *SVSettings) IVSettings {
|
||||
return (&sVSettings{
|
||||
FNetworkKey: pSett.FNetworkKey,
|
||||
}).mustNotNull()
|
||||
}
|
||||
|
||||
func (p *sVSettings) mustNotNull() IVSettings {
|
||||
// p.FNetworkKey can be = 0
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *sVSettings) GetNetworkKey() string {
|
||||
return p.FNetworkKey
|
||||
}
|
||||
@ -18,27 +18,41 @@ var (
|
||||
)
|
||||
|
||||
type sConn struct {
|
||||
fMutex sync.Mutex
|
||||
fSocket net.Conn
|
||||
fSettings ISettings
|
||||
fMutex sync.RWMutex
|
||||
fSocket net.Conn
|
||||
fSettings ISettings
|
||||
fVSettings IVSettings
|
||||
}
|
||||
|
||||
func NewConn(pSett ISettings, pAddr string) (IConn, error) {
|
||||
func NewConn(pSett ISettings, pVSett IVSettings, pAddr string) (IConn, error) {
|
||||
dialer := &net.Dialer{Timeout: pSett.GetDialTimeout()}
|
||||
conn, err := dialer.Dial("tcp", pAddr)
|
||||
if err != nil {
|
||||
return nil, utils.MergeErrors(ErrCreateConnection, err)
|
||||
}
|
||||
return LoadConn(pSett, conn), nil
|
||||
return LoadConn(pSett, pVSett, conn), nil
|
||||
}
|
||||
|
||||
func LoadConn(pSett ISettings, pConn net.Conn) IConn {
|
||||
func LoadConn(pSett ISettings, pVSett IVSettings, pConn net.Conn) IConn {
|
||||
return &sConn{
|
||||
fSettings: pSett,
|
||||
fSocket: pConn,
|
||||
fSocket: pConn,
|
||||
fSettings: pSett,
|
||||
fVSettings: pVSett,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *sConn) GetVSettings() IVSettings {
|
||||
return p.getVSettings()
|
||||
}
|
||||
|
||||
// not used from pkg/network
|
||||
func (p *sConn) SetVSettings(pVSettings IVSettings) {
|
||||
p.fMutex.Lock()
|
||||
defer p.fMutex.Unlock()
|
||||
|
||||
p.fVSettings = pVSettings
|
||||
}
|
||||
|
||||
func (p *sConn) GetSettings() ISettings {
|
||||
return p.fSettings
|
||||
}
|
||||
@ -85,7 +99,11 @@ func (p *sConn) ReadMessage(pCtx context.Context, pChRead chan<- struct{}) (net_
|
||||
}
|
||||
|
||||
// try unpack message from bytes
|
||||
msg, err := net_message.LoadMessage(p.fSettings, dataBytes)
|
||||
sett := net_message.NewSettings(&net_message.SSettings{
|
||||
FWorkSizeBits: p.fSettings.GetWorkSizeBits(),
|
||||
FNetworkKey: p.getVSettings().GetNetworkKey(),
|
||||
})
|
||||
msg, err := net_message.LoadMessage(sett, dataBytes)
|
||||
if err != nil {
|
||||
return nil, utils.MergeErrors(ErrInvalidMessageBytes, err)
|
||||
}
|
||||
@ -197,3 +215,10 @@ func (p *sConn) recvDataBytes(pCtx context.Context, pMustLen uint64, pInitTimeou
|
||||
|
||||
return dataRaw, nil
|
||||
}
|
||||
|
||||
func (p *sConn) getVSettings() IVSettings {
|
||||
p.fMutex.RLock()
|
||||
defer p.fMutex.RUnlock()
|
||||
|
||||
return p.fVSettings
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ const (
|
||||
tcBody = "hello, world!"
|
||||
)
|
||||
|
||||
func TestSettingsNetworkKey(t *testing.T) {
|
||||
func TestSettings(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
@ -26,27 +26,6 @@ func TestSettingsNetworkKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettings(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sett := NewSettings(&SSettings{
|
||||
FWorkSizeBits: testutils.TCWorkSize,
|
||||
FLimitMessageSizeBytes: testutils.TCMessageSize,
|
||||
FWaitReadTimeout: time.Hour,
|
||||
FDialTimeout: time.Minute,
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
})
|
||||
|
||||
networkKey := "hello, world!"
|
||||
sett.SetNetworkKey(networkKey)
|
||||
|
||||
if sett.GetNetworkKey() != networkKey {
|
||||
t.Error("got invalid network key")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func testSettings(t *testing.T, n int) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
@ -108,6 +87,7 @@ func TestClosedConn(t *testing.T) {
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
NewVSettings(&SVSettings{}),
|
||||
testutils.TgAddrs[30],
|
||||
)
|
||||
if err != nil {
|
||||
@ -120,8 +100,13 @@ func TestClosedConn(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
sett := message.NewSettings(&message.SSettings{
|
||||
FWorkSizeBits: conn.GetSettings().GetWorkSizeBits(),
|
||||
FNetworkKey: conn.GetVSettings().GetNetworkKey(),
|
||||
})
|
||||
|
||||
pld := payload.NewPayload(1, []byte("aaa"))
|
||||
msg := message.NewMessage(conn.GetSettings(), pld, 1, 0)
|
||||
msg := message.NewMessage(sett, pld, 1, 0)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@ -163,6 +148,7 @@ func TestInvalidConn(t *testing.T) {
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
NewVSettings(&SVSettings{}),
|
||||
"INVALID_ADDRESS",
|
||||
)
|
||||
if err == nil {
|
||||
@ -191,6 +177,7 @@ func testConn(t *testing.T, pAddr, pNetworkKey string) {
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
NewVSettings(&SVSettings{}),
|
||||
pAddr,
|
||||
)
|
||||
if err != nil {
|
||||
@ -205,10 +192,13 @@ func testConn(t *testing.T, pAddr, pNetworkKey string) {
|
||||
return
|
||||
}
|
||||
|
||||
conn.GetSettings().SetNetworkKey(pNetworkKey)
|
||||
sett := message.NewSettings(&message.SSettings{
|
||||
FWorkSizeBits: conn.GetSettings().GetWorkSizeBits(),
|
||||
FNetworkKey: pNetworkKey,
|
||||
})
|
||||
|
||||
pld := payload.NewPayload(tcHead, []byte(tcBody))
|
||||
msg := message.NewMessage(conn.GetSettings(), pld, 1, 0)
|
||||
msg := message.NewMessage(sett, pld, 1, 0)
|
||||
ctx := context.Background()
|
||||
if err := conn.WriteMessage(ctx, msg); err != nil {
|
||||
t.Error(err)
|
||||
@ -253,10 +243,13 @@ func testNewService(t *testing.T, pAddr, pNetworkKey string) net.Listener {
|
||||
FReadTimeout: time.Minute,
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
NewVSettings(&SVSettings{}),
|
||||
aconn,
|
||||
)
|
||||
|
||||
conn.GetSettings().SetNetworkKey(pNetworkKey)
|
||||
conn.SetVSettings(NewVSettings(&SVSettings{
|
||||
FNetworkKey: pNetworkKey,
|
||||
}))
|
||||
|
||||
readCh := make(chan struct{})
|
||||
go func() { <-readCh }()
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -11,8 +10,6 @@ var (
|
||||
|
||||
type SSettings sSettings
|
||||
type sSettings struct {
|
||||
fMutex sync.RWMutex
|
||||
|
||||
FNetworkKey string
|
||||
FWorkSizeBits uint64
|
||||
FLimitMessageSizeBytes uint64
|
||||
@ -55,20 +52,6 @@ func (p *sSettings) mustNotNull() ISettings {
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *sSettings) GetNetworkKey() string {
|
||||
p.fMutex.RLock()
|
||||
defer p.fMutex.RUnlock()
|
||||
|
||||
return p.FNetworkKey
|
||||
}
|
||||
|
||||
func (p *sSettings) SetNetworkKey(pNetworkKey string) {
|
||||
p.fMutex.Lock()
|
||||
defer p.fMutex.Unlock()
|
||||
|
||||
p.FNetworkKey = pNetworkKey
|
||||
}
|
||||
|
||||
func (p *sSettings) GetWorkSizeBits() uint64 {
|
||||
return p.FWorkSizeBits
|
||||
}
|
||||
|
||||
@ -15,21 +15,24 @@ type IConn interface {
|
||||
GetSettings() ISettings
|
||||
GetSocket() net.Conn
|
||||
|
||||
GetVSettings() IVSettings
|
||||
SetVSettings(IVSettings)
|
||||
|
||||
WriteMessage(context.Context, net_message.IMessage) error
|
||||
ReadMessage(context.Context, chan<- struct{}) (net_message.IMessage, error)
|
||||
}
|
||||
|
||||
type ISettings interface {
|
||||
net_message.ISettings
|
||||
|
||||
GetLimitMessageSizeBytes() uint64
|
||||
GetLimitVoidSizeBytes() uint64
|
||||
GetWorkSizeBits() uint64
|
||||
|
||||
GetDialTimeout() time.Duration
|
||||
GetReadTimeout() time.Duration
|
||||
GetWriteTimeout() time.Duration
|
||||
GetWaitReadTimeout() time.Duration
|
||||
|
||||
// for subsequent inheritance on multiple connections
|
||||
SetNetworkKey(string)
|
||||
}
|
||||
|
||||
type IVSettings interface {
|
||||
GetNetworkKey() string
|
||||
}
|
||||
|
||||
24
pkg/network/conn/v_settings.go
Normal file
24
pkg/network/conn/v_settings.go
Normal file
@ -0,0 +1,24 @@
|
||||
package conn
|
||||
|
||||
var (
|
||||
_ IVSettings = &sVSettings{}
|
||||
)
|
||||
|
||||
type SVSettings sVSettings
|
||||
type sVSettings struct {
|
||||
FNetworkKey string
|
||||
}
|
||||
|
||||
func NewVSettings(pVSett *SVSettings) IVSettings {
|
||||
return (&sVSettings{
|
||||
FNetworkKey: pVSett.FNetworkKey,
|
||||
}).mustNotNull()
|
||||
}
|
||||
|
||||
func (p *sVSettings) mustNotNull() IVSettings {
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *sVSettings) GetNetworkKey() string {
|
||||
return p.FNetworkKey
|
||||
}
|
||||
@ -127,6 +127,7 @@ func newTestConnKeeper(pDuration time.Duration) IConnKeeper {
|
||||
FWriteTimeout: time.Minute,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: testutils.TCCapacity,
|
||||
|
||||
@ -50,7 +50,7 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
conn, err := conn.NewConn(connSettings(), serviceAddress)
|
||||
conn, err := conn.NewConn(connSettings(), vSettings(), serviceAddress)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -60,15 +60,21 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
err = conn.WriteMessage(ctx, message.NewMessage(
|
||||
conn.GetSettings(),
|
||||
payload.NewPayload(
|
||||
serviceHeader,
|
||||
[]byte("hello, world!"),
|
||||
err = conn.WriteMessage(
|
||||
ctx,
|
||||
message.NewMessage(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: conn.GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: conn.GetSettings().GetWorkSizeBits(),
|
||||
}),
|
||||
payload.NewPayload(
|
||||
serviceHeader,
|
||||
[]byte("hello, world!"),
|
||||
),
|
||||
1,
|
||||
0,
|
||||
),
|
||||
1,
|
||||
0,
|
||||
))
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -93,6 +99,7 @@ func newNode(serviceAddress string) network.INode {
|
||||
FWriteTimeout: time.Minute,
|
||||
FReadTimeout: time.Minute,
|
||||
}),
|
||||
vSettings(),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: (1 << 10),
|
||||
@ -101,9 +108,12 @@ func newNode(serviceAddress string) network.INode {
|
||||
)
|
||||
}
|
||||
|
||||
func vSettings() conn.IVSettings {
|
||||
return conn.NewVSettings(&conn.SVSettings{})
|
||||
}
|
||||
|
||||
func connSettings() conn.ISettings {
|
||||
return conn.NewSettings(&conn.SSettings{
|
||||
FWorkSizeBits: 10,
|
||||
FLimitMessageSizeBytes: (1 << 10),
|
||||
FWaitReadTimeout: time.Hour,
|
||||
FDialTimeout: time.Minute,
|
||||
|
||||
@ -41,7 +41,7 @@ func main() {
|
||||
}()
|
||||
time.Sleep(time.Second) // wait
|
||||
|
||||
conn, err := conn.NewConn(connSettings(), serviceAddress)
|
||||
conn, err := conn.NewConn(connSettings(), vSettings(), serviceAddress)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -52,7 +52,10 @@ func main() {
|
||||
}()
|
||||
|
||||
sendMsg := message.NewMessage(
|
||||
conn.GetSettings(),
|
||||
message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: conn.GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: conn.GetSettings().GetWorkSizeBits(),
|
||||
}),
|
||||
payload.NewPayload(serviceHeader, []byte("hello, world!")),
|
||||
1,
|
||||
0,
|
||||
@ -74,15 +77,21 @@ func main() {
|
||||
|
||||
func handler() network.IHandlerF {
|
||||
return func(ctx context.Context, node network.INode, c conn.IConn, msg message.IMessage) error {
|
||||
c.WriteMessage(ctx, message.NewMessage(
|
||||
node.GetSettings().GetConnSettings(),
|
||||
payload.NewPayload(
|
||||
serviceHeader,
|
||||
[]byte(fmt.Sprintf("echo: [%s]", string(msg.GetPayload().GetBody()))),
|
||||
c.WriteMessage(
|
||||
ctx,
|
||||
message.NewMessage(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: node.GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: node.GetSettings().GetConnSettings().GetWorkSizeBits(),
|
||||
}),
|
||||
payload.NewPayload(
|
||||
serviceHeader,
|
||||
[]byte(fmt.Sprintf("echo: [%s]", string(msg.GetPayload().GetBody()))),
|
||||
),
|
||||
1,
|
||||
0,
|
||||
),
|
||||
1,
|
||||
0,
|
||||
))
|
||||
)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -96,6 +105,7 @@ func newNode(serviceAddress string) network.INode {
|
||||
FWriteTimeout: time.Minute,
|
||||
FReadTimeout: time.Minute,
|
||||
}),
|
||||
vSettings(),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: (1 << 10),
|
||||
@ -104,9 +114,12 @@ func newNode(serviceAddress string) network.INode {
|
||||
)
|
||||
}
|
||||
|
||||
func vSettings() conn.IVSettings {
|
||||
return conn.NewVSettings(&conn.SVSettings{})
|
||||
}
|
||||
|
||||
func connSettings() conn.ISettings {
|
||||
return conn.NewSettings(&conn.SSettings{
|
||||
FWorkSizeBits: 10,
|
||||
FLimitMessageSizeBytes: (1 << 10),
|
||||
FWaitReadTimeout: time.Hour,
|
||||
FDialTimeout: time.Minute,
|
||||
|
||||
@ -52,7 +52,10 @@ func main() {
|
||||
}
|
||||
|
||||
msg := message.NewMessage(
|
||||
service2.GetSettings().GetConnSettings(),
|
||||
message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: service2.GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: service2.GetSettings().GetConnSettings().GetWorkSizeBits(),
|
||||
}),
|
||||
payload.NewPayload(
|
||||
serviceHeader,
|
||||
[]byte("0"),
|
||||
@ -80,15 +83,21 @@ func handler(serviceName string) network.IHandlerF {
|
||||
}
|
||||
|
||||
fmt.Printf("service '%s' got '%s#%d'\n", serviceName, val, num)
|
||||
n.BroadcastMessage(ctx, message.NewMessage(
|
||||
n.GetSettings().GetConnSettings(),
|
||||
payload.NewPayload(
|
||||
serviceHeader,
|
||||
[]byte(fmt.Sprintf("%d", num+1)),
|
||||
n.BroadcastMessage(
|
||||
ctx,
|
||||
message.NewMessage(
|
||||
message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: n.GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: n.GetSettings().GetConnSettings().GetWorkSizeBits(),
|
||||
}),
|
||||
payload.NewPayload(
|
||||
serviceHeader,
|
||||
[]byte(fmt.Sprintf("%d", num+1)),
|
||||
),
|
||||
1,
|
||||
0,
|
||||
),
|
||||
1,
|
||||
0,
|
||||
))
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -103,6 +112,7 @@ func newNode(serviceAddress string) network.INode {
|
||||
FWriteTimeout: time.Minute,
|
||||
FReadTimeout: time.Minute,
|
||||
}),
|
||||
vSettings(),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: (1 << 10),
|
||||
@ -111,9 +121,12 @@ func newNode(serviceAddress string) network.INode {
|
||||
)
|
||||
}
|
||||
|
||||
func vSettings() conn.IVSettings {
|
||||
return conn.NewVSettings(&conn.SVSettings{})
|
||||
}
|
||||
|
||||
func connSettings() conn.ISettings {
|
||||
return conn.NewSettings(&conn.SSettings{
|
||||
FWorkSizeBits: 10,
|
||||
FLimitMessageSizeBytes: (1 << 10),
|
||||
FWaitReadTimeout: time.Hour,
|
||||
FDialTimeout: time.Minute,
|
||||
|
||||
@ -129,7 +129,7 @@ func LoadMessage(pSett ISettings, pData interface{}) (IMessage, error) {
|
||||
}
|
||||
|
||||
payloadVoidBytes := pphpBytes[hashIndex:]
|
||||
if len(payloadVoidBytes) < int(payloadLength) {
|
||||
if payloadLength > uint64(len(payloadVoidBytes)) {
|
||||
return nil, ErrInvalidPayloadSize
|
||||
}
|
||||
|
||||
|
||||
@ -115,13 +115,6 @@ func TestMessage(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
msg4 := NewMessage(sett, pld, 1, 0).(*sMessage)
|
||||
msg4.fEncPPHP[0] ^= 1
|
||||
if _, err := LoadMessage(sett, msg3.ToBytes()); err == nil {
|
||||
t.Error("success load with invalid pphp")
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := LoadMessage(sett, struct{}{}); err == nil {
|
||||
t.Error("success load with unknown type of message")
|
||||
return
|
||||
|
||||
@ -23,8 +23,9 @@ var (
|
||||
|
||||
type sNode struct {
|
||||
fMutex sync.Mutex
|
||||
fListener net.Listener
|
||||
fSettings ISettings
|
||||
fVSettings conn.IVSettings
|
||||
fListener net.Listener
|
||||
fCacheSetter cache.ICacheSetter
|
||||
fConnections map[string]conn.IConn
|
||||
fHandleRoutes map[uint64]IHandlerF
|
||||
@ -33,11 +34,16 @@ type sNode struct {
|
||||
// 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, pCacheSetter cache.ICacheSetter) INode {
|
||||
func NewNode(
|
||||
pSettings ISettings,
|
||||
pVSettings conn.IVSettings,
|
||||
pCacheSetter cache.ICacheSetter,
|
||||
) INode {
|
||||
return &sNode{
|
||||
fSettings: pSett,
|
||||
fSettings: pSettings,
|
||||
fVSettings: pVSettings,
|
||||
fCacheSetter: pCacheSetter,
|
||||
fConnections: make(map[string]conn.IConn, pSett.GetMaxConnects()),
|
||||
fConnections: make(map[string]conn.IConn, pSettings.GetMaxConnects()),
|
||||
fHandleRoutes: make(map[uint64]IHandlerF, cHandleRoutesSize),
|
||||
}
|
||||
}
|
||||
@ -47,6 +53,22 @@ func (p *sNode) GetSettings() ISettings {
|
||||
return p.fSettings
|
||||
}
|
||||
|
||||
func (p *sNode) GetVSettings() conn.IVSettings {
|
||||
return p.getVSettings()
|
||||
}
|
||||
|
||||
func (p *sNode) SetVSettings(pVSettings conn.IVSettings) {
|
||||
p.fMutex.Lock()
|
||||
defer p.fMutex.Unlock()
|
||||
|
||||
for id, conn := range p.fConnections {
|
||||
delete(p.fConnections, id)
|
||||
_ = conn.Close()
|
||||
}
|
||||
|
||||
p.fVSettings = pVSettings
|
||||
}
|
||||
|
||||
// Puts the hash of the message in the buffer and sends the message to all connections of the node.
|
||||
func (p *sNode) BroadcastMessage(pCtx context.Context, pMsg message.IMessage) error {
|
||||
connections := p.GetConnections()
|
||||
@ -129,7 +151,7 @@ func (p *sNode) Listen(pCtx context.Context) error {
|
||||
}
|
||||
|
||||
sett := p.fSettings.GetConnSettings()
|
||||
conn := conn.LoadConn(sett, tconn)
|
||||
conn := conn.LoadConn(sett, p.getVSettings(), tconn)
|
||||
address := tconn.RemoteAddr().String()
|
||||
|
||||
p.setConnection(address, conn)
|
||||
@ -190,7 +212,7 @@ func (p *sNode) AddConnection(pCtx context.Context, pAddress string) error {
|
||||
}
|
||||
|
||||
sett := p.fSettings.GetConnSettings()
|
||||
conn, err := conn.NewConn(sett, pAddress)
|
||||
conn, err := conn.NewConn(sett, p.getVSettings(), pAddress)
|
||||
if err != nil {
|
||||
return utils.MergeErrors(ErrAddConnections, err)
|
||||
}
|
||||
@ -284,25 +306,17 @@ func (p *sNode) messageReader(
|
||||
// Returns true if the message was successfully redirected to the handler function
|
||||
// > or if the message already existed in the hash value store.
|
||||
func (p *sNode) handleMessage(pCtx context.Context, pConn conn.IConn, pMsg message.IMessage) bool {
|
||||
// hash of message already in queue
|
||||
if !p.fCacheSetter.Set(pMsg.GetHash(), []byte{}) {
|
||||
return true
|
||||
return true // hash of message already in queue
|
||||
}
|
||||
|
||||
// get function by head
|
||||
pld := pMsg.GetPayload()
|
||||
f, ok := p.getFunction(pld.GetHead())
|
||||
f, ok := p.getFunction(pMsg.GetPayload().GetHead())
|
||||
if !ok || f == nil {
|
||||
// function is not found = protocol error
|
||||
return false
|
||||
return false // function is not found = protocol error
|
||||
}
|
||||
|
||||
if err := f(pCtx, p, pConn, pMsg); err != nil {
|
||||
// function error = protocol error
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
err := f(pCtx, p, pConn, pMsg)
|
||||
return err == nil // function error = protocol error
|
||||
}
|
||||
|
||||
// Checks the current number of connections with the limit.
|
||||
@ -355,3 +369,10 @@ func (p *sNode) getListener() net.Listener {
|
||||
|
||||
return p.fListener
|
||||
}
|
||||
|
||||
func (p *sNode) getVSettings() conn.IVSettings {
|
||||
p.fMutex.Lock()
|
||||
defer p.fMutex.Unlock()
|
||||
|
||||
return p.fVSettings
|
||||
}
|
||||
|
||||
@ -141,7 +141,10 @@ func TestBroadcast(t *testing.T) {
|
||||
headHandle,
|
||||
[]byte(fmt.Sprintf(testutils.TcBodyTemplate, i)),
|
||||
)
|
||||
sett := nodes[0].GetSettings().GetConnSettings()
|
||||
sett := message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: nodes[0].GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: nodes[0].GetSettings().GetConnSettings().GetWorkSizeBits(),
|
||||
})
|
||||
_ = nodes[0].BroadcastMessage(ctx, message.NewMessage(sett, pld, 1, 0))
|
||||
}(i)
|
||||
}
|
||||
@ -261,15 +264,20 @@ func TestNodeConnection(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, c := range node1.GetConnections() {
|
||||
c.Close()
|
||||
}
|
||||
node1.SetVSettings(conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: "set_another_network_key",
|
||||
}))
|
||||
|
||||
if err := node1.DelConnection(testutils.TgAddrs[27]); err == nil {
|
||||
t.Error("success delete already closed connection")
|
||||
return
|
||||
}
|
||||
|
||||
if len(node1.GetConnections()) != 0 {
|
||||
t.Error("set message settings should close all connections")
|
||||
return
|
||||
}
|
||||
|
||||
if err := node2.Close(); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
@ -282,8 +290,23 @@ func TestHandleMessage(t *testing.T) {
|
||||
node := newTestNode("", testutils.TCMaxConnects).(*sNode)
|
||||
defer testFreeNodes([]INode{node})
|
||||
|
||||
sett := node.GetSettings().GetConnSettings()
|
||||
newNetworkKey := "handle_message_network_key"
|
||||
node.SetVSettings(conn.NewVSettings(&conn.SVSettings{
|
||||
FNetworkKey: newNetworkKey,
|
||||
}))
|
||||
|
||||
ctx := context.Background()
|
||||
vsett := node.GetVSettings()
|
||||
|
||||
if vsett.GetNetworkKey() != newNetworkKey {
|
||||
t.Error("incorrect set variable settings")
|
||||
return
|
||||
}
|
||||
|
||||
sett := message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: vsett.GetNetworkKey(),
|
||||
FWorkSizeBits: node.GetSettings().GetConnSettings().GetWorkSizeBits(),
|
||||
})
|
||||
|
||||
node.HandleFunc(1, nil)
|
||||
msg1 := message.NewMessage(sett, payload.NewPayload(1, []byte{1}), 1, 0)
|
||||
@ -340,7 +363,10 @@ func TestContextCancel(t *testing.T) {
|
||||
}
|
||||
|
||||
headHandle := uint64(testutils.TcHead)
|
||||
sett := node2.GetSettings().GetConnSettings()
|
||||
sett := message.NewSettings(&message.SSettings{
|
||||
FNetworkKey: node2.GetVSettings().GetNetworkKey(),
|
||||
FWorkSizeBits: node2.GetSettings().GetConnSettings().GetWorkSizeBits(),
|
||||
})
|
||||
|
||||
go func() {
|
||||
for i := 0; i < 1000; i++ {
|
||||
@ -419,6 +445,7 @@ func newTestNode(pAddr string, pMaxConns uint64) INode {
|
||||
FWriteTimeout: timeout,
|
||||
}),
|
||||
}),
|
||||
conn.NewVSettings(&conn.SVSettings{}),
|
||||
lru.NewLRUCache(
|
||||
lru.NewSettings(&lru.SSettings{
|
||||
FCapacity: testutils.TCCapacity,
|
||||
|
||||
@ -15,14 +15,19 @@ type (
|
||||
|
||||
type INode interface {
|
||||
types.ICloser
|
||||
|
||||
Listen(context.Context) error
|
||||
HandleFunc(uint64, IHandlerF) INode
|
||||
|
||||
SetVSettings(conn.IVSettings)
|
||||
GetVSettings() conn.IVSettings
|
||||
|
||||
GetSettings() ISettings
|
||||
GetConnections() map[string]conn.IConn
|
||||
|
||||
AddConnection(context.Context, string) error
|
||||
DelConnection(string) error
|
||||
|
||||
BroadcastMessage(context.Context, message.IMessage) error
|
||||
}
|
||||
|
||||
|
||||
36128
test/result/coverage.out
36128
test/result/coverage.out
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user