Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Apr 7, 2024
1 parent f198aa0 commit 6991d26
Show file tree
Hide file tree
Showing 38 changed files with 17,722 additions and 20,650 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Update `cmd/hidden_lake/applications/messenger`: delete share_enabled option, requestID
- Update `examples/anon_messenger/group`: change group chat to use one private key
- Update `go-peer`: all packages with custom errors now have errors.go file
- Update `pkg`: delete wrapper package

### BUG FIXES

Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
6. Append errors (as traffic/database) for all interface methods
7. Append Batch's to database
8. Custom Unmarshal with fields
9. golint: "wrapcheck,cyclop,funlen,gocognit"

### Tests

Expand Down
2 changes: 1 addition & 1 deletion cmd/hidden_lake/_test/result/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25,406 changes: 12,074 additions & 13,332 deletions cmd/hidden_lake/_test/result/coverage.out

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions cmd/hidden_lake/helpers/traffic/internal/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,3 @@ type ISettings interface {
GetPath() string
GetMessagesCapacity() uint64
}

type IDBWrapper interface {
types.ICloser

Get() IDatabase
Set(IDatabase) IDBWrapper
}
57 changes: 0 additions & 57 deletions cmd/hidden_lake/helpers/traffic/internal/database/wrapper.go

This file was deleted.

19 changes: 9 additions & 10 deletions cmd/hidden_lake/helpers/traffic/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func testNetworkMessageSettings() net_message.ISettings {
})
}

func testAllRun(addr string) (*http.Server, context.CancelFunc, database.IDBWrapper, hlt_client.IClient) {
func testAllRun(addr string) (*http.Server, context.CancelFunc, database.IDatabase, hlt_client.IClient) {
db, err := database.NewDatabase(
database.NewSettings(&database.SSettings{
FPath: fmt.Sprintf(databaseTemplate, addr),
Expand All @@ -60,8 +60,7 @@ func testAllRun(addr string) (*http.Server, context.CancelFunc, database.IDBWrap
return nil, nil, nil, nil
}

wDB := database.NewDBWrapper().Set(db)
srv, _, cancel := testRunService(wDB, addr, "")
srv, _, cancel := testRunService(db, addr, "")

hltClient := hlt_client.NewClient(
hlt_client.NewBuilder(),
Expand All @@ -73,18 +72,18 @@ func testAllRun(addr string) (*http.Server, context.CancelFunc, database.IDBWrap
)

time.Sleep(200 * time.Millisecond)
return srv, cancel, wDB, hltClient
return srv, cancel, db, hltClient
}

func testAllFree(addr string, srv *http.Server, cancel context.CancelFunc, wDB database.IDBWrapper) {
func testAllFree(addr string, srv *http.Server, cancel context.CancelFunc, db database.IDatabase) {
defer func() {
os.RemoveAll(fmt.Sprintf(databaseTemplate, addr))
}()
cancel()
_ = closer.CloseAll([]types.ICloser{srv, wDB})
_ = closer.CloseAll([]types.ICloser{srv, db})
}

func testRunService(wDB database.IDBWrapper, addr string, addrNode string) (*http.Server, connkeeper.IConnKeeper, context.CancelFunc) {
func testRunService(db database.IDatabase, addr string, addrNode string) (*http.Server, connkeeper.IConnKeeper, context.CancelFunc) {
mux := http.NewServeMux()

connKeeperSettings := &connkeeper.SSettings{
Expand Down Expand Up @@ -156,9 +155,9 @@ func testRunService(wDB database.IDBWrapper, addr string, addrNode string) (*htt
)

mux.HandleFunc(pkg_settings.CHandleIndexPath, HandleIndexAPI(logger))
mux.HandleFunc(pkg_settings.CHandleStoragePointerPath, HandlePointerAPI(wDB, logger))
mux.HandleFunc(pkg_settings.CHandleStorageHashesPath, HandleHashesAPI(wDB, logger))
mux.HandleFunc(pkg_settings.CHandleNetworkMessagePath, HandleMessageAPI(ctx, cfg, wDB, logger, logger, node))
mux.HandleFunc(pkg_settings.CHandleStoragePointerPath, HandlePointerAPI(db, logger))
mux.HandleFunc(pkg_settings.CHandleStorageHashesPath, HandleHashesAPI(db, logger))
mux.HandleFunc(pkg_settings.CHandleNetworkMessagePath, HandleMessageAPI(ctx, cfg, db, logger, logger, node))
mux.HandleFunc(pkg_settings.CHandleConfigSettings, HandleConfigSettingsAPI(cfg, logger))

srv := &http.Server{
Expand Down
11 changes: 2 additions & 9 deletions cmd/hidden_lake/helpers/traffic/internal/handler/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/number571/go-peer/pkg/logger"
)

func HandleHashesAPI(pDBWrapper database.IDBWrapper, pLogger logger.ILogger) http.HandlerFunc {
func HandleHashesAPI(pDatabase database.IDatabase, pLogger logger.ILogger) http.HandlerFunc {
return func(pW http.ResponseWriter, pR *http.Request) {
logBuilder := http_logger.NewLogBuilder(hlt_settings.CServiceName, pR)

Expand All @@ -22,13 +22,6 @@ func HandleHashesAPI(pDBWrapper database.IDBWrapper, pLogger logger.ILogger) htt
return
}

database := pDBWrapper.Get()
if database == nil {
pLogger.PushErro(logBuilder.WithMessage("get_database"))
_ = api.Response(pW, http.StatusInternalServerError, "failed: get database")
return
}

query := pR.URL.Query()
id, err := strconv.Atoi(query.Get("id"))
if err != nil {
Expand All @@ -37,7 +30,7 @@ func HandleHashesAPI(pDBWrapper database.IDBWrapper, pLogger logger.ILogger) htt
return
}

hash, err := database.Hash(uint64(id))
hash, err := pDatabase.Hash(uint64(id))
if err != nil {
pLogger.PushWarn(logBuilder.WithMessage("get_hashes"))
_ = api.Response(pW, http.StatusNotAcceptable, "failed: load size from DB")
Expand Down
13 changes: 3 additions & 10 deletions cmd/hidden_lake/helpers/traffic/internal/handler/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
net_message "github.com/number571/go-peer/pkg/network/message"
)

func HandleMessageAPI(pCtx context.Context, pCfg config.IConfig, pDBWrapper database.IDBWrapper, pHTTPLogger, pAnonLogger logger.ILogger, pNode network.INode) http.HandlerFunc {
tcpHandler := HandleServiceTCP(pCfg, pDBWrapper, pAnonLogger)
func HandleMessageAPI(pCtx context.Context, pCfg config.IConfig, pDatabase database.IDatabase, pHTTPLogger, pAnonLogger logger.ILogger, pNode network.INode) http.HandlerFunc {
tcpHandler := HandleServiceTCP(pCfg, pDatabase, pAnonLogger)

return func(pW http.ResponseWriter, pR *http.Request) {
logBuilder := http_logger.NewLogBuilder(hlt_settings.CServiceName, pR)
Expand All @@ -29,13 +29,6 @@ func HandleMessageAPI(pCtx context.Context, pCfg config.IConfig, pDBWrapper data
return
}

database := pDBWrapper.Get()
if database == nil {
pHTTPLogger.PushErro(logBuilder.WithMessage("get_database"))
_ = api.Response(pW, http.StatusInternalServerError, "failed: get database")
return
}

switch pR.Method {
case http.MethodGet:
query := pR.URL.Query()
Expand All @@ -47,7 +40,7 @@ func HandleMessageAPI(pCtx context.Context, pCfg config.IConfig, pDBWrapper data
return
}

msg, err := database.Load(hash)
msg, err := pDatabase.Load(hash)
if err != nil {
pHTTPLogger.PushWarn(logBuilder.WithMessage("load_hash"))
_ = api.Response(pW, http.StatusNotFound, "failed: load message")
Expand Down
11 changes: 2 additions & 9 deletions cmd/hidden_lake/helpers/traffic/internal/handler/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/number571/go-peer/pkg/logger"
)

func HandlePointerAPI(pDBWrapper database.IDBWrapper, pLogger logger.ILogger) http.HandlerFunc {
func HandlePointerAPI(pDatabase database.IDatabase, pLogger logger.ILogger) http.HandlerFunc {
return func(pW http.ResponseWriter, pR *http.Request) {
logBuilder := http_logger.NewLogBuilder(hlt_settings.CServiceName, pR)

Expand All @@ -21,14 +21,7 @@ func HandlePointerAPI(pDBWrapper database.IDBWrapper, pLogger logger.ILogger) ht
return
}

database := pDBWrapper.Get()
if database == nil {
pLogger.PushErro(logBuilder.WithMessage("get_database"))
_ = api.Response(pW, http.StatusInternalServerError, "failed: get database")
return
}

pLogger.PushInfo(logBuilder.WithMessage(http_logger.CLogSuccess))
_ = api.Response(pW, http.StatusOK, strconv.FormatUint(database.Pointer(), 10))
_ = api.Response(pW, http.StatusOK, strconv.FormatUint(pDatabase.Pointer(), 10))
}
}
11 changes: 2 additions & 9 deletions cmd/hidden_lake/helpers/traffic/internal/handler/service_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
hlt_settings "github.com/number571/go-peer/cmd/hidden_lake/helpers/traffic/pkg/settings"
)

func HandleServiceTCP(pCfg config.IConfig, pDBWrapper database.IDBWrapper, pLogger logger.ILogger) network.IHandlerF {
func HandleServiceTCP(pCfg config.IConfig, pDatabase database.IDatabase, pLogger logger.ILogger) network.IHandlerF {
httpClient := &http.Client{Timeout: time.Minute}

return func(pCtx context.Context, pNode network.INode, pConn conn.IConn, pNetMsg net_message.IMessage) error {
Expand All @@ -40,15 +40,8 @@ func HandleServiceTCP(pCfg config.IConfig, pDBWrapper database.IDBWrapper, pLogg
return utils.MergeErrors(ErrLoadMessage, err)
}

// check database
hltDB := pDBWrapper.Get()
if hltDB == nil {
pLogger.PushErro(logBuilder.WithType(anon_logger.CLogErroDatabaseGet))
return ErrDatabaseNull
}

// check message from in database queue
if err := hltDB.Push(pNetMsg); err != nil {
if err := pDatabase.Push(pNetMsg); err != nil {
if errors.Is(err, database.ErrMessageIsExist) {
pLogger.PushInfo(logBuilder.WithType(anon_logger.CLogInfoExist))
return nil
Expand Down
17 changes: 7 additions & 10 deletions cmd/hidden_lake/helpers/traffic/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type sApp struct {

fPathTo string
fConfig config.IConfig
fDBWrapper database.IDBWrapper
fNode network.INode
fDatabase database.IDatabase
fConnKeeper connkeeper.IConnKeeper

fAnonLogger logger.ILogger
Expand All @@ -55,15 +55,9 @@ func NewApp(
stdfLogger = std_logger.NewStdLogger(pCfg.GetLogging(), std_logger.GetLogFunc())
)

dbWrapper := database.NewDBWrapper()
node := initNode(pCfg, dbWrapper, anonLogger)

return &sApp{
fState: state.NewBoolState(),
fConfig: pCfg,
fDBWrapper: dbWrapper,
fNode: node,
fConnKeeper: initConnKeeper(pCfg, node),
fPathTo: pPathTo,
fAnonLogger: anonLogger,
fHTTPLogger: httpLogger,
Expand Down Expand Up @@ -109,8 +103,11 @@ func (p *sApp) enable(pCtx context.Context) state.IStateF {
return utils.MergeErrors(ErrInitDB, err)
}

p.initServiceHTTP(pCtx)
p.initNetworkNode(p.fDatabase)
p.initConnKeeper(p.fNode)

p.initServicePPROF()
p.initServiceHTTP(pCtx)

p.fStdfLogger.PushInfo(hlt_settings.CServiceName + " is running...")
return nil
Expand All @@ -130,8 +127,8 @@ func (p *sApp) stop() error {
err := closer.CloseAll([]types.ICloser{
p.fServiceHTTP,
p.fServicePPROF,
p.fDBWrapper,
p.fConnKeeper.GetNetworkNode(),
p.fDatabase,
p.fNode,
})
if err != nil {
return utils.MergeErrors(ErrClose, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package app

import (
"github.com/number571/go-peer/cmd/hidden_lake/helpers/traffic/internal/config"
"github.com/number571/go-peer/pkg/network"
"github.com/number571/go-peer/pkg/network/connkeeper"

hls_settings "github.com/number571/go-peer/cmd/hidden_lake/service/pkg/settings"
)

func initConnKeeper(pCfg config.IConfig, pNode network.INode) connkeeper.IConnKeeper {
return connkeeper.NewConnKeeper(
func (p *sApp) initConnKeeper(pNode network.INode) {
p.fConnKeeper = connkeeper.NewConnKeeper(
connkeeper.NewSettings(&connkeeper.SSettings{
FConnections: func() []string { return pCfg.GetConnections() },
FConnections: func() []string { return p.fConfig.GetConnections() },
FDuration: hls_settings.CConnKeeperDuration,
}),
pNode,
Expand Down
2 changes: 1 addition & 1 deletion cmd/hidden_lake/helpers/traffic/pkg/app/init_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (p *sApp) initDatabase() error {
return fmt.Errorf("init database: %w", err)
}

p.fDBWrapper.Set(db)
p.fDatabase = db
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@ package app
import (
"time"

"github.com/number571/go-peer/cmd/hidden_lake/helpers/traffic/internal/config"
"github.com/number571/go-peer/cmd/hidden_lake/helpers/traffic/internal/database"
"github.com/number571/go-peer/cmd/hidden_lake/helpers/traffic/internal/handler"
"github.com/number571/go-peer/pkg/cache/lru"
"github.com/number571/go-peer/pkg/logger"
"github.com/number571/go-peer/pkg/network"
"github.com/number571/go-peer/pkg/network/conn"

hls_settings "github.com/number571/go-peer/cmd/hidden_lake/service/pkg/settings"
)

func initNode(pCfg config.IConfig, pDBWrapper database.IDBWrapper, pLogger logger.ILogger) network.INode {
cfgSettings := pCfg.GetSettings()
func (p *sApp) initNetworkNode(pDatabase database.IDatabase) {
cfgSettings := p.fConfig.GetSettings()
queueDuration := time.Duration(cfgSettings.GetQueuePeriodMS()) * time.Millisecond

// queue_period_ms in HLT can be = 0 (as only-storage mode)
if queueDuration == 0 {
queueDuration = 1
}

return network.NewNode(
p.fNode = network.NewNode(
network.NewSettings(&network.SSettings{
FAddress: pCfg.GetAddress().GetTCP(),
FAddress: p.fConfig.GetAddress().GetTCP(),
FMaxConnects: hls_settings.CNetworkMaxConns,
FReadTimeout: queueDuration,
FWriteTimeout: queueDuration,
Expand All @@ -49,6 +47,6 @@ func initNode(pCfg config.IConfig, pDBWrapper database.IDBWrapper, pLogger logge
),
).HandleFunc(
hls_settings.CNetworkMask,
handler.HandleServiceTCP(pCfg, pDBWrapper, pLogger),
handler.HandleServiceTCP(p.fConfig, pDatabase, p.fAnonLogger),
)
}
6 changes: 3 additions & 3 deletions cmd/hidden_lake/helpers/traffic/pkg/app/init_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func (p *sApp) initServiceHTTP(pCtx context.Context) {
mux := http.NewServeMux()

mux.HandleFunc(pkg_settings.CHandleIndexPath, handler.HandleIndexAPI(p.fHTTPLogger))
mux.HandleFunc(pkg_settings.CHandleStoragePointerPath, handler.HandlePointerAPI(p.fDBWrapper, p.fHTTPLogger))
mux.HandleFunc(pkg_settings.CHandleStorageHashesPath, handler.HandleHashesAPI(p.fDBWrapper, p.fHTTPLogger))
mux.HandleFunc(pkg_settings.CHandleNetworkMessagePath, handler.HandleMessageAPI(pCtx, p.fConfig, p.fDBWrapper, p.fHTTPLogger, p.fAnonLogger, p.fNode))
mux.HandleFunc(pkg_settings.CHandleStoragePointerPath, handler.HandlePointerAPI(p.fDatabase, p.fHTTPLogger))
mux.HandleFunc(pkg_settings.CHandleStorageHashesPath, handler.HandleHashesAPI(p.fDatabase, p.fHTTPLogger))
mux.HandleFunc(pkg_settings.CHandleNetworkMessagePath, handler.HandleMessageAPI(pCtx, p.fConfig, p.fDatabase, p.fHTTPLogger, p.fAnonLogger, p.fNode))
mux.HandleFunc(pkg_settings.CHandleConfigSettings, handler.HandleConfigSettingsAPI(p.fConfig, p.fHTTPLogger))

p.fServiceHTTP = &http.Server{
Expand Down
Loading

0 comments on commit 6991d26

Please sign in to comment.