-
Notifications
You must be signed in to change notification settings - Fork 46
/
rsocket.go
82 lines (71 loc) · 2.44 KB
/
rsocket.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
package rsocket
import (
"github.com/rsocket/rsocket-go/internal/socket"
"github.com/rsocket/rsocket-go/payload"
"github.com/rsocket/rsocket-go/rx"
"github.com/rsocket/rsocket-go/rx/flux"
"github.com/rsocket/rsocket-go/rx/mono"
)
type (
// ServerAcceptor is alias for server accepter.
ServerAcceptor = func(setup payload.SetupPayload, sendingSocket CloseableRSocket) RSocket
// RSocket is a contract providing different interaction models for RSocket protocol.
RSocket interface {
// FireAndForget is a single one-way message.
FireAndForget(msg payload.Payload)
// MetadataPush sends asynchronous Metadata frame.
MetadataPush(msg payload.Payload)
// RequestResponse request single response.
RequestResponse(msg payload.Payload) mono.Mono
// RequestStream request a completable stream.
RequestStream(msg payload.Payload) flux.Flux
// RequestChannel request a completable stream in both directions.
RequestChannel(msgs rx.Publisher) flux.Flux
}
// CloseableRSocket is a RSocket which support more events.
CloseableRSocket interface {
socket.Closeable
RSocket
}
// OptAbstractSocket is option for abstract socket.
OptAbstractSocket func(*socket.AbstractRSocket)
)
// NewAbstractSocket returns an abstract implementation of RSocket.
// You can specify the actual implementation of any request.
func NewAbstractSocket(opts ...OptAbstractSocket) RSocket {
sk := &socket.AbstractRSocket{}
for _, fn := range opts {
fn(sk)
}
return sk
}
// MetadataPush register request handler for MetadataPush.
func MetadataPush(fn func(msg payload.Payload)) OptAbstractSocket {
return func(socket *socket.AbstractRSocket) {
socket.MP = fn
}
}
// FireAndForget register request handler for FireAndForget.
func FireAndForget(fn func(msg payload.Payload)) OptAbstractSocket {
return func(opts *socket.AbstractRSocket) {
opts.FF = fn
}
}
// RequestResponse register request handler for RequestResponse.
func RequestResponse(fn func(msg payload.Payload) mono.Mono) OptAbstractSocket {
return func(opts *socket.AbstractRSocket) {
opts.RR = fn
}
}
// RequestStream register request handler for RequestStream.
func RequestStream(fn func(msg payload.Payload) flux.Flux) OptAbstractSocket {
return func(opts *socket.AbstractRSocket) {
opts.RS = fn
}
}
// RequestChannel register request handler for RequestChannel.
func RequestChannel(fn func(msgs rx.Publisher) flux.Flux) OptAbstractSocket {
return func(opts *socket.AbstractRSocket) {
opts.RC = fn
}
}