-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
59 lines (47 loc) · 1.03 KB
/
service.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
package svcb
import (
"fmt"
"sync"
as "github.com/godevsig/adaptiveservice"
)
// Request is the request from clients.
// Return *Reply
type Request struct {
Input string
}
// Handle handles msg.
func (req *Request) Handle(stream as.ContextStream) (reply any) {
svc := stream.GetContext().(*service)
return &Reply{Output: fmt.Sprintf("%s %s service b", req.Input, svc.info)}
}
var knownMsgs = []as.KnownMessage{
(*Request)(nil),
}
// Reply is the reply for Request to clients
type Reply struct {
Output string
}
type service struct {
sync.RWMutex
info string
}
func (svc *service) onNewStream(ctx as.Context) {
ctx.SetContext(svc)
}
// RunService runs the service.
func RunService(opts []as.Option) error {
s := as.NewServer(opts...).SetPublisher("example")
svc := &service{info: "Hello"}
if err := s.Publish("serviceB",
knownMsgs,
as.OnNewStreamFunc(svc.onNewStream),
); err != nil {
return err
}
// ctrl+c to exit
return s.Serve()
}
func init() {
as.RegisterType((*Request)(nil))
as.RegisterType((*Reply)(nil))
}