events: refactor event distribution
In the course of setting out to add filters and address some cleanup, it was found that we had a few problems in the events subsystem that needed addressing before moving forward. The biggest change was to move to the more standard terminology of publish and subscribe. We make this terminology change across the Go interface and the GRPC API, making the behavior more familier. The previous system was very context-oriented, which is no longer required. With this, we've removed a large amount of dead and unneeded code. Event transactions, context storage and the concept of `Poster` is gone. This has been replaced in most places with a `Publisher`, which matches the actual usage throughout the codebase, removing the need for helpers. There are still some questions around the way events are handled in the shim. Right now, we've preserved some of the existing bugs which may require more extensive changes to resolve correctly. Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
@@ -89,17 +89,16 @@ func (c *local) Update(ctx context.Context, in *shimapi.UpdateTaskRequest, opts
|
||||
return c.s.Update(ctx, in)
|
||||
}
|
||||
|
||||
type poster interface {
|
||||
Post(ctx context.Context, in *events.PostEventRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error)
|
||||
type publisher interface {
|
||||
Publish(ctx context.Context, in *events.PublishRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error)
|
||||
}
|
||||
|
||||
type localEventsClient struct {
|
||||
emitter evt.Poster
|
||||
forwarder evt.Forwarder
|
||||
}
|
||||
|
||||
func (l *localEventsClient) Post(ctx context.Context, r *events.PostEventRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error) {
|
||||
ctx = evt.WithTopic(ctx, r.Envelope.Topic)
|
||||
if err := l.emitter.Post(ctx, r.Envelope); err != nil {
|
||||
func (l *localEventsClient) Publish(ctx context.Context, r *events.PublishRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error) {
|
||||
if err := l.forwarder.Forward(ctx, r.Envelope); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return empty, nil
|
||||
|
||||
@@ -37,7 +37,7 @@ func NewService(path, namespace, address string) (*Service, error) {
|
||||
return nil, fmt.Errorf("shim namespace cannot be empty")
|
||||
}
|
||||
context := namespaces.WithNamespace(context.Background(), namespace)
|
||||
var client poster
|
||||
var client publisher
|
||||
if address != "" {
|
||||
conn, err := connect(address, dialer)
|
||||
if err != nil {
|
||||
@@ -46,7 +46,7 @@ func NewService(path, namespace, address string) (*Service, error) {
|
||||
client = events.NewEventsClient(conn)
|
||||
} else {
|
||||
client = &localEventsClient{
|
||||
emitter: evt.GetPoster(context),
|
||||
forwarder: evt.NewExchange(),
|
||||
}
|
||||
}
|
||||
s := &Service{
|
||||
@@ -379,16 +379,18 @@ func (s *Service) getContainerPids(ctx context.Context, id string) ([]uint32, er
|
||||
return pids, nil
|
||||
}
|
||||
|
||||
func (s *Service) forward(client poster) {
|
||||
func (s *Service) forward(client publisher) {
|
||||
for e := range s.events {
|
||||
a, err := typeurl.MarshalAny(e)
|
||||
if err != nil {
|
||||
log.G(s.context).WithError(err).Error("marshal event")
|
||||
continue
|
||||
}
|
||||
if _, err := client.Post(s.context, &events.PostEventRequest{
|
||||
|
||||
if _, err := client.Publish(s.context, &events.PublishRequest{
|
||||
Envelope: &events.Envelope{
|
||||
Timestamp: time.Now(),
|
||||
Namespace: s.namespace,
|
||||
Timestamp: time.Now().UTC(),
|
||||
Topic: getTopic(e),
|
||||
Event: a,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user