Added some helper functions and nonces for the packets
This commit is contained in:
parent
9572989a86
commit
ba2582e851
4
go.mod
4
go.mod
|
@ -1,8 +1,8 @@
|
|||
module git.ailur.dev/ailur/fg-library/v2
|
||||
module git.ailur.dev/ailur/fg-library/v3
|
||||
|
||||
go 1.23.3
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.1.0
|
||||
github.com/go-chi/chi/v5 v5.2.0
|
||||
github.com/google/uuid v1.6.0
|
||||
)
|
||||
|
|
70
main.go
70
main.go
|
@ -2,9 +2,11 @@ package library
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
"io/fs"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -23,6 +25,7 @@ type Service struct {
|
|||
}
|
||||
|
||||
type InterServiceMessage struct {
|
||||
MessageID uuid.UUID `validate:"required"`
|
||||
ServiceID uuid.UUID `validate:"required"`
|
||||
ForServiceID uuid.UUID `validate:"required"`
|
||||
MessageType uint64 `validate:"required"`
|
||||
|
@ -42,11 +45,74 @@ type ServiceInitializationInformation struct {
|
|||
type DBType int
|
||||
|
||||
const (
|
||||
Sqlite DBType = 0
|
||||
Postgres DBType = 1
|
||||
Sqlite DBType = iota
|
||||
Postgres
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
DB *sql.DB
|
||||
DBType DBType
|
||||
}
|
||||
|
||||
type ResponseCode int
|
||||
|
||||
const (
|
||||
Success ResponseCode = iota
|
||||
BadRequest
|
||||
InternalError
|
||||
Unauthorized
|
||||
)
|
||||
|
||||
var buffer = make(map[uuid.UUID]InterServiceMessage)
|
||||
var mutex = sync.Mutex{}
|
||||
var arrived = make(chan uuid.UUID)
|
||||
|
||||
func ISMessageBuffer(inbox <-chan InterServiceMessage) {
|
||||
for {
|
||||
mutex.Lock()
|
||||
msg := <-inbox
|
||||
buffer[msg.MessageID] = msg
|
||||
mutex.Unlock()
|
||||
arrived <- msg.MessageID
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
ErrTimeout = errors.New("timeout")
|
||||
)
|
||||
|
||||
func AwaitISMessage(id uuid.UUID, timeout time.Duration) (InterServiceMessage, error) {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(timeout):
|
||||
return InterServiceMessage{}, ErrTimeout
|
||||
case msgID := <-arrived:
|
||||
if msgID == id {
|
||||
mutex.Lock()
|
||||
msg := buffer[id]
|
||||
delete(buffer, id)
|
||||
mutex.Unlock()
|
||||
return msg, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServiceInitializationInformation) SendISMessage(service Service, forService uuid.UUID, messageType uint64, message any) uuid.UUID {
|
||||
id := uuid.New()
|
||||
msg := InterServiceMessage{
|
||||
MessageID: id,
|
||||
ServiceID: service.ServiceID,
|
||||
ForServiceID: forService,
|
||||
MessageType: messageType,
|
||||
SentAt: time.Now(),
|
||||
Message: message,
|
||||
}
|
||||
s.Outbox <- msg
|
||||
return id
|
||||
}
|
||||
|
||||
func (s *ServiceInitializationInformation) SendAndAwaitISMessage(service Service, forService uuid.UUID, messageType uint64, message any, timeout time.Duration) (InterServiceMessage, error) {
|
||||
id := s.SendISMessage(service, forService, messageType, message)
|
||||
return AwaitISMessage(id, timeout)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue