-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver_grpc.go
54 lines (45 loc) · 1.45 KB
/
observer_grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package observer
import (
"context"
"log"
"github.com/senzing-garage/go-observing/observerpb"
)
// ----------------------------------------------------------------------------
// Types
// ----------------------------------------------------------------------------
// GrpcObserver sends the observed message to a Grpc server.
type GrpcObserver struct {
GrpcClient observerpb.ObserverClient
ID string
}
// ----------------------------------------------------------------------------
// Interface methods
// ----------------------------------------------------------------------------
/*
The GetObserverID method returns the unique identifier of the observer.
Use by the subject to manage the list of Observers.
Input
- ctx: A context to control lifecycle.
*/
func (observer *GrpcObserver) GetObserverID(ctx context.Context) string {
_ = ctx
return observer.ID
}
/*
The UpdateObserver method processes the message sent by the Subject.
The subject invokes UpdateObserver as a goroutine.
Input
- ctx: A context to control lifecycle.
- message: The string to propagate to all registered Observers.
*/
func (observer *GrpcObserver) UpdateObserver(ctx context.Context, message string) {
if observer.GrpcClient != nil {
request := observerpb.UpdateObserverRequest{
Message: message,
}
_, err := observer.GrpcClient.UpdateObserver(ctx, &request)
if err != nil {
log.Printf("Observer: %s; Message: %s; Error: %v\n", observer.ID, message, err)
}
}
}