-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.go
64 lines (59 loc) · 1.62 KB
/
broker.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
package main
import (
"context"
"fmt"
"log"
rsocket "github.com/rsocket/rsocket-go"
"github.com/rsocket/rsocket-go/payload"
"github.com/rsocket/rsocket-go/rx"
)
func main() {
const metadataRegister = "register"
bus := rsocket.NewBus()
err := rsocket.Receive().
Acceptor(func(setup payload.SetupPayload, sendingSocket rsocket.EnhancedRSocket) rsocket.RSocket {
// register socket to bus.
metadata, _ := setup.MetadataUTF8()
if metadata == metadataRegister {
merge := struct {
id string
sk rsocket.RSocket
}{setup.DataUTF8(), sendingSocket}
log.Println(setup.DataUTF8())
sendingSocket.OnClose(func() {
log.Println("removing service " + merge.id)
bus.Remove(merge.id, merge.sk)
})
bus.Put(merge.id, merge.sk)
}
// bind responder: redirect to target socket.
return rsocket.NewAbstractSocket(
rsocket.RequestResponse(func(msg payload.Payload) rx.Mono {
id, _ := msg.MetadataUTF8()
log.Println("asking for " + id)
sk, ok := bus.Get(id)
if !ok {
return rx.NewMono(func(ctx context.Context, sink rx.MonoProducer) {
sink.Error(fmt.Errorf("MISSING_SERVICE_SOCKET_%s", id))
})
}
return sk.RequestResponse(msg)
}),
rsocket.RequestStream(func(msg payload.Payload) rx.Flux {
id, _ := msg.MetadataUTF8()
sk, ok := bus.Get(id)
if !ok {
return rx.NewFlux(func(ctx context.Context, producer rx.Producer) {
producer.Error(fmt.Errorf("MISSING_SERVICE_SOCKET_%s", id))
})
}
return sk.RequestStream(msg)
}),
)
}).
Transport("127.0.0.1:8888").
Serve()
if err != nil {
panic(err)
}
}