forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first draft for event system - includes example Signed-off-by: jkoberg <[email protected]> * add event middleware Signed-off-by: jkoberg <[email protected]> * events: distinguish grantee userid and groupid Signed-off-by: Jörn Friedrich Dreyer <[email protected]> * seperate consumer from publisher Signed-off-by: jkoberg <[email protected]> * code review suggestions Signed-off-by: jkoberg <[email protected]> * simplify example Signed-off-by: jkoberg <[email protected]> * add changelog Signed-off-by: jkoberg <[email protected]> * make nats server configurable Signed-off-by: jkoberg <[email protected]> * add license headers Signed-off-by: jkoberg <[email protected]> * cheat the linter Signed-off-by: jkoberg <[email protected]> Co-authored-by: Jörn Friedrich Dreyer <[email protected]>
- Loading branch information
Showing
13 changed files
with
995 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Enhancement: introduce events | ||
|
||
This will introduce events into the system. Events are a simple way to bring information from | ||
one service to another. Read `pkg/events/example` and subfolders for more information | ||
|
||
https://github.com/cs3org/reva/pull/2522 | ||
|
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,37 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package eventsmiddleware | ||
|
||
import ( | ||
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" | ||
"github.com/cs3org/reva/pkg/events" | ||
) | ||
|
||
// ShareCreated converts response to event | ||
func ShareCreated(r *collaboration.CreateShareResponse) events.ShareCreated { | ||
e := events.ShareCreated{ | ||
Sharer: r.Share.Creator, | ||
GranteeUserID: r.Share.GetGrantee().GetUserId(), | ||
GranteeGroupID: r.Share.GetGrantee().GetGroupId(), | ||
ItemID: r.Share.ResourceId, | ||
CTime: r.Share.Ctime, | ||
} | ||
|
||
return e | ||
} |
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,95 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package eventsmiddleware | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go-micro.dev/v4/util/log" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/asim/go-micro/plugins/events/nats/v4" | ||
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" | ||
"github.com/cs3org/reva/pkg/events" | ||
"github.com/cs3org/reva/pkg/events/server" | ||
"github.com/cs3org/reva/pkg/rgrpc" | ||
) | ||
|
||
const ( | ||
defaultPriority = 200 | ||
) | ||
|
||
func init() { | ||
rgrpc.RegisterUnaryInterceptor("eventsmiddleware", NewUnary) | ||
} | ||
|
||
// NewUnary returns a new unary interceptor that emits events when needed | ||
// no lint because of the switch statement that should be extendable | ||
//nolint:gocritic | ||
func NewUnary(m map[string]interface{}) (grpc.UnaryServerInterceptor, int, error) { | ||
publisher, err := publisherFromConfig(m) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
res, err := handler(ctx, req) | ||
if err != nil { | ||
return res, err | ||
} | ||
|
||
var ev interface{} | ||
switch v := res.(type) { | ||
case *collaboration.CreateShareResponse: | ||
ev = ShareCreated(v) | ||
} | ||
|
||
if ev != nil { | ||
if err := events.Publish(publisher, ev); err != nil { | ||
log.Error(err) | ||
} | ||
} | ||
|
||
return res, nil | ||
} | ||
return interceptor, defaultPriority, nil | ||
} | ||
|
||
// NewStream returns a new server stream interceptor | ||
// that creates the application context. | ||
func NewStream() grpc.StreamServerInterceptor { | ||
interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | ||
// TODO: Use ss.RecvMsg() and ss.SendMsg() to send events from a stream | ||
return handler(srv, ss) | ||
} | ||
return interceptor | ||
} | ||
|
||
func publisherFromConfig(m map[string]interface{}) (events.Publisher, error) { | ||
typ := m["type"].(string) | ||
switch typ { | ||
default: | ||
return nil, fmt.Errorf("stream type '%s' not supported", typ) | ||
case "nats": | ||
address := m["address"].(string) | ||
cid := m["clusterID"].(string) | ||
return server.NewNatsStream(nats.Address(address), nats.ClusterID(cid)) | ||
} | ||
} |
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,107 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package events | ||
|
||
import ( | ||
"log" | ||
"reflect" | ||
|
||
"go-micro.dev/v4/events" | ||
) | ||
|
||
var ( | ||
// MainQueueName is the name of the main queue | ||
// All events will go through here as they are forwarded to the consumer via the | ||
// group name | ||
// TODO: "fan-out" so not all events go through the same queue? requires investigation | ||
MainQueueName = "main-queue" | ||
|
||
// MetadatakeyEventType is the key used for the eventtype in the metadata map of the event | ||
MetadatakeyEventType = "eventtype" | ||
) | ||
|
||
type ( | ||
// Unmarshaller is the interface events need to fulfill | ||
Unmarshaller interface { | ||
Unmarshal([]byte) (interface{}, error) | ||
} | ||
|
||
// Publisher is the interface publishers need to fulfill | ||
Publisher interface { | ||
Publish(string, interface{}, ...events.PublishOption) error | ||
} | ||
|
||
// Consumer is the interface consumer need to fulfill | ||
Consumer interface { | ||
Consume(string, ...events.ConsumeOption) (<-chan events.Event, error) | ||
} | ||
|
||
// Stream is the interface common to Publisher and Consumer | ||
Stream interface { | ||
Publish(string, interface{}, ...events.PublishOption) error | ||
Consume(string, ...events.ConsumeOption) (<-chan events.Event, error) | ||
} | ||
) | ||
|
||
// Consume returns a channel that will get all events that match the given evs | ||
// group defines the service type: One group will get exactly one copy of a event that is emitted | ||
// NOTE: uses reflect on initialization | ||
func Consume(s Consumer, group string, evs ...Unmarshaller) (<-chan interface{}, error) { | ||
c, err := s.Consume(MainQueueName, events.WithGroup(group)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
registeredEvents := map[string]Unmarshaller{} | ||
for _, e := range evs { | ||
typ := reflect.TypeOf(e) | ||
registeredEvents[typ.String()] = e | ||
} | ||
|
||
outchan := make(chan interface{}) | ||
go func() { | ||
for { | ||
e := <-c | ||
et := e.Metadata[MetadatakeyEventType] | ||
ev, ok := registeredEvents[et] | ||
if !ok { | ||
log.Printf("not registered: %s", et) | ||
continue | ||
} | ||
|
||
event, err := ev.Unmarshal(e.Payload) | ||
if err != nil { | ||
log.Printf("can't unmarshal event %v", err) | ||
continue | ||
} | ||
|
||
outchan <- event | ||
} | ||
}() | ||
return outchan, nil | ||
} | ||
|
||
// Publish publishes the ev to the MainQueue from where it is distributed to all subscribers | ||
// NOTE: needs to use reflect on runtime | ||
func Publish(s Publisher, ev interface{}) error { | ||
evName := reflect.TypeOf(ev).String() | ||
return s.Publish(MainQueueName, ev, events.WithMetadata(map[string]string{ | ||
MetadatakeyEventType: evName, | ||
})) | ||
} |
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,60 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
// Package consumer contains an example implementation of an event consumer | ||
package consumer | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/cs3org/reva/pkg/events" | ||
) | ||
|
||
// Example consumes events from the queue | ||
func Example(c events.Consumer) { | ||
// Step 1 - which group does the consumer belong to? | ||
// each group will get each event that is emitted, but only one member of the group will get it. | ||
group := "test-consumer" | ||
|
||
// Step 2 - which events does the consumer listen too? | ||
evs := []events.Unmarshaller{ | ||
// for example created shares | ||
events.ShareCreated{}, | ||
} | ||
|
||
// Step 3 - create event channel | ||
evChan, err := events.Consume(c, group, evs...) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Step 4 - listen to events | ||
for { | ||
event := <-evChan | ||
|
||
// best to use type switch to differentiate events | ||
switch v := event.(type) { | ||
case events.ShareCreated: | ||
fmt.Printf("%s) Share created: %+v\n", group, v) | ||
default: | ||
fmt.Printf("%s) Unregistered event: %+v\n", group, v) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.