refactor foundry lib structure; add transport classes for db,http,websocket; add action interface instead of multiple function that accepts action classes; add changing foundry status on websocket responses
This commit is contained in:
71
internal/foundry/transport/transport.go
Normal file
71
internal/foundry/transport/transport.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/db"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/requests"
|
||||
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type FoundryTransport struct {
|
||||
WsConn *websocket.Conn
|
||||
CurrWsId int
|
||||
ChanMutex sync.Mutex
|
||||
ReadChan types.ReadChannels
|
||||
ExchangeChan types.ExchangeChannels
|
||||
|
||||
Http requests.FoundryHttpRequest
|
||||
IsAuth bool
|
||||
|
||||
Models *db.Models
|
||||
IsDbInit bool
|
||||
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewFoundryTransport(dbConn *sql.DB, logger *slog.Logger, httpConfig *requests.FoundryHttpRequest) *FoundryTransport {
|
||||
return &FoundryTransport{
|
||||
CurrWsId: 0,
|
||||
ExchangeChan: types.ExchangeChannels{
|
||||
Msgs: make(map[int]chan []byte),
|
||||
ProgressMsg: map[string]chan struct{}{},
|
||||
},
|
||||
|
||||
Http: *httpConfig,
|
||||
IsAuth: false,
|
||||
|
||||
Models: db.NewModels(dbConn),
|
||||
IsDbInit: false,
|
||||
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *FoundryTransport) CloseMsgChannel(id int, timeout time.Duration) bool {
|
||||
time.Sleep(timeout)
|
||||
|
||||
t.ChanMutex.Lock()
|
||||
defer t.ChanMutex.Unlock()
|
||||
|
||||
ok := true
|
||||
if _, ok = t.ExchangeChan.Msgs[id]; !ok {
|
||||
return false
|
||||
}
|
||||
select {
|
||||
case _, ok = <-t.ExchangeChan.Msgs[id]:
|
||||
if ok {
|
||||
close(t.ExchangeChan.Msgs[id])
|
||||
delete(t.ExchangeChan.Msgs, id)
|
||||
}
|
||||
default:
|
||||
close(t.ExchangeChan.Msgs[id])
|
||||
delete(t.ExchangeChan.Msgs, id)
|
||||
}
|
||||
|
||||
return ok
|
||||
}
|
||||
Reference in New Issue
Block a user