forked from nsqio/libnsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
167 lines (134 loc) · 4.73 KB
/
reader.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "nsq.h"
#include "utlist.h"
#include "http.h"
#ifdef DEBUG
#define _DEBUG(...) fprintf(stdout, __VA_ARGS__)
#else
#define _DEBUG(...) do {;} while (0)
#endif
static void nsq_reader_connect_cb(struct NSQDConnection *conn, void *arg)
{
struct NSQReader *rdr = (struct NSQReader *)arg;
_DEBUG("%s: %p\n", __FUNCTION__, rdr);
if (rdr->connect_callback) {
rdr->connect_callback(rdr, conn);
}
// subscribe
buffer_reset(conn->command_buf);
nsq_subscribe(conn->command_buf, rdr->topic, rdr->channel);
buffered_socket_write_buffer(conn->bs, conn->command_buf);
// send initial RDY
buffer_reset(conn->command_buf);
nsq_ready(conn->command_buf, rdr->max_in_flight);
buffered_socket_write_buffer(conn->bs, conn->command_buf);
}
static void nsq_reader_msg_cb(struct NSQDConnection *conn, struct NSQMessage *msg, void *arg)
{
struct NSQReader *rdr = (struct NSQReader *)arg;
_DEBUG("%s: %p %p\n", __FUNCTION__, msg, rdr);
if (rdr->msg_callback) {
msg->id[sizeof(msg->id)-1] = '\0';
rdr->msg_callback(rdr, conn, msg, rdr->ctx);
}
}
static void nsq_reader_close_cb(struct NSQDConnection *conn, void *arg)
{
struct NSQReader *rdr = (struct NSQReader *)arg;
_DEBUG("%s: %p\n", __FUNCTION__, rdr);
if (rdr->close_callback) {
rdr->close_callback(rdr, conn);
}
LL_DELETE(rdr->conns, conn);
free_nsqd_connection(conn);
}
void nsq_lookupd_request_cb(struct HttpRequest *req, struct HttpResponse *resp, void *arg);
static void nsq_reader_lookupd_poll_cb(EV_P_ struct ev_timer *w, int revents)
{
struct NSQReader *rdr = (struct NSQReader *)w->data;
struct NSQLookupdEndpoint *nsqlookupd_endpoint;
struct HttpRequest *req;
int i, idx, count = 0;
char buf[256];
LL_FOREACH(rdr->lookupd, nsqlookupd_endpoint) {
count++;
}
idx = rand() % count;
_DEBUG("%s: rdr %p (chose %d)\n", __FUNCTION__, rdr, idx);
i = 0;
LL_FOREACH(rdr->lookupd, nsqlookupd_endpoint) {
if (i++ == idx) {
sprintf(buf, "http://%s:%d/lookup?topic=%s", nsqlookupd_endpoint->address,
nsqlookupd_endpoint->port, rdr->topic);
req = new_http_request(buf, nsq_lookupd_request_cb, rdr);
http_client_get((struct HttpClient *)rdr->httpc, req);
break;
}
}
ev_timer_again(rdr->loop, &rdr->lookupd_poll_timer);
}
struct NSQReader *new_nsq_reader(struct ev_loop *loop, const char *topic, const char *channel, void *ctx,
void (*connect_callback)(struct NSQReader *rdr, struct NSQDConnection *conn),
void (*close_callback)(struct NSQReader *rdr, struct NSQDConnection *conn),
void (*msg_callback)(struct NSQReader *rdr, struct NSQDConnection *conn, struct NSQMessage *msg, void *ctx))
{
struct NSQReader *rdr;
rdr = (struct NSQReader *)malloc(sizeof(struct NSQReader));
rdr->topic = strdup(topic);
rdr->channel = strdup(channel);
rdr->max_in_flight = 1;
rdr->connect_callback = connect_callback;
rdr->close_callback = close_callback;
rdr->msg_callback = msg_callback;
rdr->ctx = ctx;
rdr->conns = NULL;
rdr->lookupd = NULL;
rdr->loop = loop;
rdr->httpc = new_http_client(rdr->loop);
// TODO: configurable interval
ev_timer_init(&rdr->lookupd_poll_timer, nsq_reader_lookupd_poll_cb, 0., 5.);
rdr->lookupd_poll_timer.data = rdr;
ev_timer_again(rdr->loop, &rdr->lookupd_poll_timer);
return rdr;
}
void free_nsq_reader(struct NSQReader *rdr)
{
struct NSQDConnection *conn;
struct NSQLookupdEndpoint *nsqlookupd_endpoint;
if (rdr) {
// TODO: this should probably trigger disconnections and then keep
// trying to clean up until everything upstream is finished
LL_FOREACH(rdr->conns, conn) {
nsqd_connection_disconnect(conn);
}
LL_FOREACH(rdr->lookupd, nsqlookupd_endpoint) {
free_nsqlookupd_endpoint(nsqlookupd_endpoint);
}
free(rdr->topic);
free(rdr->channel);
free(rdr);
}
}
int nsq_reader_add_nsqlookupd_endpoint(struct NSQReader *rdr, const char *address, int port)
{
struct NSQLookupdEndpoint *nsqlookupd_endpoint;
nsqlookupd_endpoint = new_nsqlookupd_endpoint(address, port);
LL_APPEND(rdr->lookupd, nsqlookupd_endpoint);
return 1;
}
int nsq_reader_connect_to_nsqd(struct NSQReader *rdr, const char *address, int port)
{
struct NSQDConnection *conn;
int rc;
conn = new_nsqd_connection(rdr->loop, address, port,
nsq_reader_connect_cb, nsq_reader_close_cb, nsq_reader_msg_cb, rdr);
rc = nsqd_connection_connect(conn);
if (rc > 0) {
LL_APPEND(rdr->conns, conn);
}
return rc;
}
void nsq_run(struct ev_loop *loop)
{
srand(time(NULL));
ev_loop(loop, 0);
}