-
-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathtransport.go
More file actions
99 lines (79 loc) · 3.3 KB
/
Copy pathtransport.go
File metadata and controls
99 lines (79 loc) · 3.3 KB
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
89
90
91
92
93
94
95
96
97
98
99
package mercure
import (
"context"
"errors"
"fmt"
)
// EarliestLastEventID is the reserved value representing the earliest available event id.
const EarliestLastEventID = "earliest"
// Transport provides methods to dispatch and persist updates.
type Transport interface {
// Dispatch dispatches an update to all subscribers.
//
// It trusts u to be well-formed. A caller that builds u from untrusted
// input (e.g. a publisher request) and dispatches it directly instead of
// through Hub.Publish MUST call u.Validate first and reject the update on
// error, otherwise a CR, LF, or NUL in ID or Type can inject arbitrary SSE
// fields into subscribers' streams (CWE-93). Hub-internal updates such as
// subscription events are trusted and skip Validate (they use reserved
// topics that Validate rejects by design).
Dispatch(ctx context.Context, u *Update) error
// AddSubscriber adds a new subscriber to the transport.
AddSubscriber(ctx context.Context, s *LocalSubscriber) error
// RemoveSubscriber removes a subscriber from the transport.
RemoveSubscriber(ctx context.Context, s *LocalSubscriber) error
// Close closes the Transport.
Close(ctx context.Context) error
}
// TransportSubscribers provides a method to retrieve the list of active subscribers.
type TransportSubscribers interface {
// GetSubscribers gets the last event ID and the list of active subscribers at this time.
GetSubscribers(ctx context.Context) (string, []*Subscriber, error)
}
// TransportTopicMatcherStore provides a method to pass the TopicMatcherStore to the transport.
type TransportTopicMatcherStore interface {
SetTopicMatcherStore(store *TopicMatcherStore)
}
// TransportHealthChecker may be implemented by transports that support health checking.
// Transports that do not implement this interface are assumed to always be healthy.
type TransportHealthChecker interface {
// Ready reports whether the transport can currently serve traffic.
// Returns nil if healthy, or an error describing the problem.
// This is typically used for readiness probes (e.g. Kubernetes).
Ready(ctx context.Context) error
// Live reports whether the transport is fundamentally operational.
// Returns nil if alive, or an error if the transport has been unhealthy
// for an extended period and should be restarted.
// This is typically used for liveness probes (e.g. Kubernetes).
Live(ctx context.Context) error
}
// ErrClosedTransport is returned by the Transport's Dispatch and AddSubscriber methods after a call to Close.
var ErrClosedTransport = errors.New("hub: read/write on closed Transport")
// TransportError is returned when the Transport's DSN is invalid.
type TransportError struct {
dsn string
msg string
err error
}
func (e *TransportError) Error() string {
if e.msg == "" {
if e.err == nil {
return fmt.Sprintf("%q: invalid transport", e.dsn)
}
return fmt.Sprintf("%q: invalid transport: %s", e.dsn, e.err)
}
if e.err == nil {
return fmt.Sprintf("%q: invalid transport: %s", e.dsn, e.msg)
}
return fmt.Sprintf("%q: %s: invalid transport: %s", e.dsn, e.msg, e.err)
}
func (e *TransportError) Unwrap() error {
return e.err
}
func getSubscribers(sl *SubscriberList) (subscribers []*Subscriber) {
sl.Walk(0, func(s *LocalSubscriber) bool {
subscribers = append(subscribers, &s.Subscriber)
return true
})
return subscribers
}