-
Notifications
You must be signed in to change notification settings - Fork 2
/
emitter.go
88 lines (73 loc) · 2.62 KB
/
emitter.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package event
import (
"context"
"math/rand"
"github.com/klyed/hivesmartchain/event/pubsub"
"github.com/klyed/hivesmartchain/event/query"
"github.com/klyed/hivesmartchain/logging"
"github.com/klyed/hivesmartchain/logging/structure"
"github.com/tendermint/tendermint/libs/service"
hex "github.com/tmthrgd/go-hex"
)
const DefaultEventBufferCapacity = 2 << 10
// TODO: manage the creation, closing, and draining of channels behind subscribe rather than only closing.
// stop one subscriber from blocking everything!
// Emitter has methods for working with events
type Emitter struct {
service.BaseService
pubsubServer *pubsub.Server
logger *logging.Logger
}
// NewEmitter initializes an emitter struct with a pubsubServer
func NewEmitter() *Emitter {
pubsubServer := pubsub.NewServer(pubsub.BufferCapacity(DefaultEventBufferCapacity))
pubsubServer.BaseService = *service.NewBaseService(nil, "Emitter", pubsubServer)
pubsubServer.Start()
return &Emitter{
pubsubServer: pubsubServer,
}
}
// SetLogger attaches a log handler to this emitter
func (em *Emitter) SetLogger(logger *logging.Logger) {
em.logger = logger.With(structure.ComponentKey, "Events")
}
// Shutdown stops the pubsubServer
func (em *Emitter) Shutdown(ctx context.Context) error {
return em.pubsubServer.Stop()
}
// Publish tells the emitter to publish with the given message and tags
func (em *Emitter) Publish(ctx context.Context, message interface{}, tags query.Tagged) error {
if em == nil || em.pubsubServer == nil {
return nil
}
return em.pubsubServer.PublishWithTags(ctx, message, tags)
}
// Subscribe tells the emitter to listen for messages on the given query
func (em *Emitter) Subscribe(ctx context.Context, subscriber string, queryable query.Queryable, bufferSize int) (<-chan interface{}, error) {
qry, err := queryable.Query()
if err != nil {
return nil, err
}
return em.pubsubServer.Subscribe(ctx, subscriber, qry, bufferSize)
}
// Unsubscribe tells the emitter to stop listening for said messages
func (em *Emitter) Unsubscribe(ctx context.Context, subscriber string, queryable query.Queryable) error {
pubsubQuery, err := queryable.Query()
if err != nil {
return nil
}
return em.pubsubServer.Unsubscribe(ctx, subscriber, pubsubQuery)
}
// UnsubscribeAll just stop listening for all messages
func (em *Emitter) UnsubscribeAll(ctx context.Context, subscriber string) error {
return em.pubsubServer.UnsubscribeAll(ctx, subscriber)
}
// ***************
// Helper function
func GenSubID() string {
bs := make([]byte, 32)
rand.Read(bs)
return hex.EncodeUpperToString(bs)
}