-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update membrane to use new worker pools.
- Loading branch information
Showing
5 changed files
with
58 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,31 @@ | ||
package grpc | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/google/uuid" | ||
pb "github.com/nitric-dev/membrane/interfaces/nitric/v1" | ||
"github.com/nitric-dev/membrane/sdk" | ||
"github.com/nitric-dev/membrane/worker" | ||
) | ||
|
||
type FaasServer struct { | ||
pb.UnimplementedFaasServer | ||
eventPlugin sdk.EventService | ||
|
||
srv pb.Faas_TriggerStreamServer | ||
|
||
// The function is ready to go | ||
FunctionReady chan bool | ||
|
||
// Each trigger will get a channel back to wait for its response from the function | ||
// triggerQueue map[string]*pb.TriggerRequest | ||
// Add a write lock for triggers | ||
responseQueue sync.Map | ||
// Add a read lock for responses | ||
// srv pb.Faas_TriggerStreamServer | ||
pool *worker.FaasWorkerPool | ||
} | ||
|
||
// Push a trigger onto the queue | ||
func (s *FaasServer) PushTrigger(request *pb.TriggerRequest) (chan *pb.TriggerResponse, error) { | ||
|
||
// Make a channel for this trigger and push it onto the response queue under its id | ||
// Generate an ID for this request response pair | ||
ID := uuid.New().String() | ||
message := &pb.Message{ | ||
Id: ID, | ||
Content: &pb.Message_TriggerRequest{ | ||
TriggerRequest: request, | ||
}, | ||
} | ||
|
||
err := s.srv.Send(message) | ||
|
||
if err != nil { | ||
// There was an error enqueuing the message | ||
return nil, err | ||
// Starts a new stream | ||
// A reference to this stream will be passed on to a new worker instance | ||
// This represents a new server that is ready to begin processing | ||
func (s *FaasServer) TriggerStream(srv pb.Faas_TriggerStreamServer) error { | ||
if err := s.pool.AddWorker(srv); err != nil { | ||
// return an error here... | ||
// TODO: Return proper grpc error with status here... | ||
return err | ||
} | ||
|
||
// Get a lock on the response queue | ||
returnChan := make(chan *pb.TriggerResponse) | ||
s.responseQueue.Store(ID, returnChan) | ||
|
||
return returnChan, nil | ||
return nil | ||
} | ||
|
||
// Recieve messages from the function | ||
func (s *FaasServer) recieveMessages(errch chan error) { | ||
for { | ||
var msg *pb.Message | ||
|
||
err := s.srv.RecvMsg(msg) | ||
|
||
if err != nil { | ||
// exit | ||
errch <- err | ||
break | ||
} else { | ||
|
||
} | ||
|
||
// Load the the response channel and delete its map key reference | ||
if val, ok := s.responseQueue.LoadAndDelete(msg.GetId()); ok { | ||
// For now assume this is a trigger response... | ||
response := msg.GetTriggerResponse() | ||
rChan := val.(chan *pb.TriggerResponse) | ||
// Write the response the the waiting recipient | ||
rChan <- response | ||
} else { | ||
errch <- fmt.Errorf("Fatal: FaaS server in base state exiting!!!") | ||
} | ||
func NewFaasServer(workerPool *worker.FaasWorkerPool) *FaasServer { | ||
return &FaasServer{ | ||
pool: workerPool, | ||
} | ||
} | ||
|
||
// Start the stream | ||
func (s *FaasServer) TriggerStream(srv pb.Faas_TriggerStreamServer) error { | ||
s.srv = srv | ||
|
||
errch := make(chan error) | ||
go s.recieveMessages(errch) | ||
|
||
return <-errch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package worker | ||
|
||
import "github.com/nitric-dev/membrane/handler" | ||
|
||
type WorkerPool interface { | ||
// WaitForActiveWorkers - A blocking method | ||
WaitForActiveWorkers(timeout int) error | ||
GetTriggerHandler() (handler.TriggerHandler, error) | ||
} |