MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / clusterSendPublish

Function clusterSendPublish

src/cluster.cpp:2762–2802  ·  view source on GitHub ↗

Send a PUBLISH message. * * If link is NULL, then the message is broadcasted to the whole cluster. */

Source from the content-addressed store, hash-verified

2760 *
2761 * If link is NULL, then the message is broadcasted to the whole cluster. */
2762void clusterSendPublish(clusterLink *link, robj *channel, robj *message) {
2763 unsigned char *payload;
2764 clusterMsg buf[1];
2765 clusterMsg *hdr = (clusterMsg*) buf;
2766 uint32_t totlen;
2767 uint32_t channel_len, message_len;
2768
2769 channel = getDecodedObject(channel);
2770 message = getDecodedObject(message);
2771 channel_len = sdslen(szFromObj(channel));
2772 message_len = sdslen(szFromObj(message));
2773
2774 clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_PUBLISH);
2775 totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
2776 totlen += sizeof(clusterMsgDataPublish) - 8 + channel_len + message_len;
2777
2778 hdr->data.publish.msg.channel_len = htonl(channel_len);
2779 hdr->data.publish.msg.message_len = htonl(message_len);
2780 hdr->totlen = htonl(totlen);
2781
2782 /* Try to use the local buffer if possible */
2783 if (totlen < sizeof(buf)) {
2784 payload = (unsigned char*)buf;
2785 } else {
2786 payload = (unsigned char*)zmalloc(totlen, MALLOC_LOCAL);
2787 memcpy(payload,hdr,sizeof(*hdr));
2788 hdr = (clusterMsg*) payload;
2789 }
2790 memcpy(hdr->data.publish.msg.bulk_data,ptrFromObj(channel),sdslen(szFromObj(channel)));
2791 memcpy(hdr->data.publish.msg.bulk_data+sdslen(szFromObj(channel)),
2792 ptrFromObj(message),sdslen(szFromObj(message)));
2793
2794 if (link)
2795 clusterSendMessage(link,payload,totlen);
2796 else
2797 clusterBroadcastMessage(payload,totlen);
2798
2799 decrRefCount(channel);
2800 decrRefCount(message);
2801 if (payload != (unsigned char*)buf) zfree(payload);
2802}
2803
2804/* Send a FAIL message to all the nodes we are able to contact.
2805 * The FAIL message is sent when we detect that a node is failing

Callers 1

clusterPropagatePublishFunction · 0.85

Calls 10

getDecodedObjectFunction · 0.85
sdslenFunction · 0.85
szFromObjFunction · 0.85
clusterBuildMessageHdrFunction · 0.85
zmallocFunction · 0.85
ptrFromObjFunction · 0.85
clusterSendMessageFunction · 0.85
clusterBroadcastMessageFunction · 0.85
decrRefCountFunction · 0.85
zfreeFunction · 0.85

Tested by

no test coverage detected