forked from lastbackend/rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.go
92 lines (69 loc) · 2.08 KB
/
rpc.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
89
90
91
92
// Copyright 2016 Last.Backend. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
/*
Package lastbackend/prc implements an amqp queue based messaging system.
Like the standard AMQP application rpc.Router matches incoming messages against
a list of registered routes and upstream and calls a handler for the route or upstream
that matches the route name or upstream or other conditions. The main features are:
* Messages routing can be matched based received router, upstream.
* You can send many types of messages, with or without confirmation.
Each app automatically join in a group by its name, you can set uuid of registered app to
provide better delivery.
You should start with your app registering:
func main() {
r := rpc.Register()
r.SetHandler("handler", SomeHandler)
}
Setup handler and upstream examples:
r := rpc.Register()
r.SetHandler("handler", SomeHandler)
r.SetUpstream("upstream", SomeUpstream)
Handlers and Upstreams examples:
// SomeHandler definition
func SomeHandler(s rpc.Sender, message []byte) error {
}
// SomeUpstream definition
func SomeUpstream(s rpc.Sender, r rpc.Recipient, message []byte) error {
}
*/
package rpc
// Register application in RPC
func Register(name string, uuid string, token string) (*RPC, error) {
var rpc RPC
rpc.name = name
rpc.uuid = uuid
rpc.token = token
rpc.connect = make(chan bool)
rpc.reconnect = make(chan bool)
rpc.connected = make(chan bool)
rpc.limit = 1
rpc.done = make(chan error)
rpc.error = make(chan error)
rpc.handlers = make(map[string]Handler)
rpc.upstreams = make(map[string]Upstream)
return &rpc, nil
}
// SetURI - set URI connection
func (r *RPC) SetURI(uri string) {
r.uri = uri
}
func (r *RPC) SetLimit(limit int) {
r.limit = limit
}
// Start listening for incoming messages
func (r *RPC) Listen() {
go r.listen()
r.online = true
r.connect <- true
}
func (r *RPC) Connected() chan bool {
return r.connected
}
func (r *RPC) Cleanup() {
r.cleanup()
}
func (r *RPC) Shutdown() {
r.online = false
r.shutdown()
}