From ba2582e8514b25264605f1366abb8543a106feab Mon Sep 17 00:00:00 2001 From: arzumify Date: Tue, 31 Dec 2024 15:53:45 +0000 Subject: [PATCH] Added some helper functions and nonces for the packets --- go.mod | 4 ++-- main.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 51c1c79..b0419c0 100644 --- a/go.mod +++ b/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 ) diff --git a/main.go b/main.go index 3457ef1..98236e0 100644 --- a/main.go +++ b/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) +}