This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
modified event transmission method to gRPC request stream #36
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -16,6 +16,10 @@ func (c *Client) ReceivePayload() <-chan event.Payload { | |
return c.payloadChan | ||
} | ||
|
||
func (c *Client) SetEvents(events []string) { | ||
c.events = events | ||
} | ||
|
||
func NewClient(events []string) Client { | ||
return Client{ | ||
events: events, | ||
|
@@ -59,6 +63,20 @@ func (cm *ClientManager) RemoveClient(client Client) { | |
close(client.payloadChan) | ||
} | ||
|
||
func (cm *ClientManager) DeleteEvents(client *Client) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint]
|
||
for _, e := range client.events { | ||
clients, ok := cm.clientsTable[e] | ||
if !ok { | ||
continue | ||
} | ||
clients.mu.Lock() | ||
delete(clients.clients, client.payloadChan) | ||
clients.mu.Unlock() | ||
|
||
delete(cm.clientsTable, e) | ||
} | ||
} | ||
|
||
const eventSeparator = ":" | ||
|
||
func (cm *ClientManager) createEvents(request string) []string { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package server | ||
|
||
import ( | ||
"io" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
|
@@ -15,6 +16,7 @@ import ( | |
"github.com/openfresh/plasma/protobuf" | ||
"github.com/openfresh/plasma/pubsub" | ||
"github.com/pkg/errors" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
|
@@ -61,25 +63,34 @@ func NewGRPCServer(opt Option) (*GRPCServer, error) { | |
return gs, nil | ||
} | ||
|
||
type refreshEvents struct { | ||
client *manager.Client | ||
events []string | ||
} | ||
|
||
type StreamServer struct { | ||
clientManager *manager.ClientManager | ||
newClients chan manager.Client | ||
removeClients chan manager.Client | ||
payloads chan event.Payload | ||
pubsub pubsub.PubSuber | ||
accessLogger *zap.Logger | ||
errorLogger *zap.Logger | ||
clientManager *manager.ClientManager | ||
newClients chan manager.Client | ||
removeClients chan manager.Client | ||
payloads chan event.Payload | ||
resfreshEvents chan refreshEvents | ||
errChan chan error | ||
pubsub pubsub.PubSuber | ||
accessLogger *zap.Logger | ||
errorLogger *zap.Logger | ||
} | ||
|
||
func NewStreamServer(opt Option) *StreamServer { | ||
ss := &StreamServer{ | ||
clientManager: manager.NewClientManager(), | ||
newClients: make(chan manager.Client), | ||
removeClients: make(chan manager.Client), | ||
payloads: make(chan event.Payload), | ||
pubsub: opt.PubSuber, | ||
accessLogger: opt.AccessLogger, | ||
errorLogger: opt.ErrorLogger, | ||
clientManager: manager.NewClientManager(), | ||
newClients: make(chan manager.Client), | ||
removeClients: make(chan manager.Client), | ||
payloads: make(chan event.Payload), | ||
errChan: make(chan error), | ||
resfreshEvents: make(chan refreshEvents), | ||
pubsub: opt.PubSuber, | ||
accessLogger: opt.AccessLogger, | ||
errorLogger: opt.ErrorLogger, | ||
} | ||
ss.pubsub.Subscribe(func(payload event.Payload) { | ||
ss.payloads <- payload | ||
|
@@ -101,35 +112,65 @@ func (ss *StreamServer) Run() { | |
metrics.DecConnection() | ||
case payload := <-ss.payloads: | ||
ss.clientManager.SendPayload(payload) | ||
case re := <-ss.resfreshEvents: | ||
ss.clientManager.DeleteEvents(re.client) | ||
re.client.SetEvents(re.events) | ||
ss.clientManager.AddClient(*re.client) | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (ss *StreamServer) Events(request *proto.Request, es proto.StreamService_EventsServer) error { | ||
ss.accessLogger.Info("gRPC", | ||
zap.Array("request-events", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error { | ||
for _, e := range request.Events { | ||
enc.AppendString(e.Type) | ||
func (ss *StreamServer) Events(es proto.StreamService_EventsServer) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint]
|
||
client := manager.NewClient([]string{}) | ||
ss.newClients <- client | ||
go func() { | ||
for { | ||
request, err := es.Recv() | ||
if err == io.EOF { | ||
<-es.Context().Done() | ||
return | ||
} | ||
return nil | ||
})), | ||
) | ||
if request == nil || request.Events == nil { | ||
return errors.New("request can't be nil") | ||
} | ||
|
||
l := len(request.Events) | ||
events := make([]string, l) | ||
for i := 0; i < l; i++ { | ||
events[i] = request.Events[i].Type | ||
} | ||
if err != nil { | ||
if grpc.Code(err) != codes.Canceled { | ||
ss.errChan <- errors.Wrap(err, "Recv error") | ||
return | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint]
|
||
<-es.Context().Done() | ||
return | ||
} | ||
} | ||
|
||
client := manager.NewClient(events) | ||
ss.newClients <- client | ||
ss.accessLogger.Info("gRPC", | ||
zap.Array("request-events", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error { | ||
for _, e := range request.Events { | ||
enc.AppendString(e.Type) | ||
} | ||
return nil | ||
})), | ||
) | ||
if request.Events == nil { | ||
ss.errChan <- errors.New("event can't be nil") | ||
return | ||
} | ||
|
||
l := len(request.Events) | ||
events := make([]string, l) | ||
for i := 0; i < l; i++ { | ||
events[i] = request.Events[i].Type | ||
} | ||
ss.resfreshEvents <- refreshEvents{ | ||
client: &client, | ||
events: events, | ||
} | ||
} | ||
}() | ||
|
||
for { | ||
select { | ||
case err := <-ss.errChan: | ||
return err | ||
case pl, open := <-client.ReceivePayload(): | ||
if !open { | ||
return nil | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[golint]