-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter_main.go
96 lines (76 loc) · 3 KB
/
adapter_main.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
93
94
95
96
// The package nsqAdapter provides a thin wrapper around bitly's nsq-services to
// simplify usage of nsq's asynchronous message queues for midsized projects.
//
// Basically, you define a new nsq-adapter struct and call the following methods:
// - Subscribe() to subscribe to a specific topic and handle incoming messages
// - Publish() to send a message to a specific topic
// - Request() to send a request to a specific topic and wait for a response
// - RespondTo() to send a response message to a request
//
// Please take a look at the example and the samples in the testing directory for
// an idea about the usage of the package.
package nsqAdapter
import (
"encoding/json"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/bitly/go-nsq"
)
// NewNsqAdapter will create a new nsq-adapter using the given address to connect
// to a nsqlookupd-service and use the default configuration for connections
func New(serviceName string, nsqlookupHttpAddress string) *NsqAdapter {
return NewWithCustomConfig(serviceName, nsqlookupHttpAddress, nsq.NewConfig())
}
// NewWithCustomConfig will create a new nsq-adapter with a custom nsq-configuration
func NewWithCustomConfig(serviceName string, nsqlookupHttpAddress string, config *nsq.Config) *NsqAdapter {
// initialize a new adapter
queue := NsqAdapter{
Name: serviceName,
nsqlookupAddress: nsqlookupHttpAddress,
consumers: make(map[string]*nsq.Consumer),
requests: make(map[string]chan Message),
config: config,
}
return &queue
}
// New Message will create a new message struct to send to nsq
func (queue *NsqAdapter) NewMessage(topic string, messageType string, payload interface{}) *Message {
// create a new Message
message := Message{}
// set a unique id for our message
message.Id = uuid.NewUUID().String()
// set the originating service
message.From = queue.Name
// set the message to send the data to
message.To = topic
// define the time until we need the response
message.StartTime = time.Now().String()
// set the payload
message.Payload, _ = json.Marshal(payload)
// set the type
message.MessageType = messageType
return &message
}
// Handle will start handling all incoming messages with the given function in a separate go routine
func (queue *NsqAdapter) Handle(topic string, channel string, handleFunction func(message Message)) {
// start handling the topic immediately in it's own routine
go func() {
// create a channel that will receive message from
// a topic we would like to subscribe to
messageChan := make(chan Message)
// subscribe to all messages posted to the fetch process
queue.Subscribe(topic, channel, messageChan)
// handle all incoming requests for fetching data
for {
// wait for incoming messages
message := <-messageChan
// start processing our data
go handleFunction(message)
}
}()
}
// Process will simply block execution so the requests to the specified topics can be processed
func (queue *NsqAdapter) Process() {
block := make(chan struct{})
<-block
}