mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
kafka send the accept message.
This commit is contained in:
parent
de41c1c9d2
commit
7013993c7a
3 changed files with 114 additions and 0 deletions
|
@ -36,10 +36,12 @@ using namespace std;
|
||||||
#include <srs_kernel_balance.hpp>
|
#include <srs_kernel_balance.hpp>
|
||||||
#include <srs_kafka_stack.hpp>
|
#include <srs_kafka_stack.hpp>
|
||||||
#include <srs_core_autofree.hpp>
|
#include <srs_core_autofree.hpp>
|
||||||
|
#include <srs_protocol_json.hpp>
|
||||||
|
|
||||||
#ifdef SRS_AUTO_KAFKA
|
#ifdef SRS_AUTO_KAFKA
|
||||||
|
|
||||||
#define SRS_KAKFA_CYCLE_INTERVAL_MS 3000
|
#define SRS_KAKFA_CYCLE_INTERVAL_MS 3000
|
||||||
|
#define SRS_KAFKA_PRODUCER_AGGREGATE_SIZE 10
|
||||||
|
|
||||||
std::string srs_kafka_metadata_summary(SrsKafkaTopicMetadataResponse* metadata)
|
std::string srs_kafka_metadata_summary(SrsKafkaTopicMetadataResponse* metadata)
|
||||||
{
|
{
|
||||||
|
@ -144,6 +146,33 @@ string SrsKafkaPartition::hostport()
|
||||||
return ep;
|
return ep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SrsKafkaMessageOnClient::SrsKafkaMessageOnClient(SrsKafkaProducer* p, SrsListenerType t, string i)
|
||||||
|
{
|
||||||
|
producer = p;
|
||||||
|
type = t;
|
||||||
|
ip = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsKafkaMessageOnClient::~SrsKafkaMessageOnClient()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsKafkaMessageOnClient::call()
|
||||||
|
{
|
||||||
|
SrsJsonObject* obj = SrsJsonAny::object();
|
||||||
|
|
||||||
|
obj->set("msg", SrsJsonAny::str("accept"));
|
||||||
|
obj->set("type", SrsJsonAny::integer(type));
|
||||||
|
obj->set("ip", SrsJsonAny::str(ip.c_str()));
|
||||||
|
|
||||||
|
return producer->send(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
string SrsKafkaMessageOnClient::to_string()
|
||||||
|
{
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
SrsKafkaProducer::SrsKafkaProducer()
|
SrsKafkaProducer::SrsKafkaProducer()
|
||||||
{
|
{
|
||||||
metadata_ok = false;
|
metadata_ok = false;
|
||||||
|
@ -211,6 +240,41 @@ void SrsKafkaProducer::stop()
|
||||||
worker->stop();
|
worker->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SrsKafkaProducer::on_client(SrsListenerType type, st_netfd_t stfd)
|
||||||
|
{
|
||||||
|
return worker->execute(new SrsKafkaMessageOnClient(this, type, srs_get_peer_ip(st_netfd_fileno(stfd))));
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsKafkaProducer::send(SrsJsonObject* obj)
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
// cache the json object.
|
||||||
|
objects.push_back(obj);
|
||||||
|
|
||||||
|
// too few messages, ignore.
|
||||||
|
if (objects.size() < SRS_KAFKA_PRODUCER_AGGREGATE_SIZE) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// too many messages, warn user.
|
||||||
|
if (objects.size() > SRS_KAFKA_PRODUCER_AGGREGATE_SIZE * 10) {
|
||||||
|
srs_warn("kafka cache too many messages: %d", objects.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// sync with backgound metadata worker.
|
||||||
|
st_mutex_lock(lock);
|
||||||
|
|
||||||
|
// flush message when metadata is ok.
|
||||||
|
if (metadata_ok) {
|
||||||
|
ret = flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
st_mutex_unlock(lock);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int SrsKafkaProducer::cycle()
|
int SrsKafkaProducer::cycle()
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
@ -329,5 +393,12 @@ void SrsKafkaProducer::refresh_metadata()
|
||||||
srs_trace("kafka async refresh metadata in background");
|
srs_trace("kafka async refresh metadata in background");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int SrsKafkaProducer::flush()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
// TODO: FIXME: implements it.
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -35,8 +35,12 @@ class SrsLbRoundRobin;
|
||||||
class SrsAsyncCallWorker;
|
class SrsAsyncCallWorker;
|
||||||
class SrsTcpClient;
|
class SrsTcpClient;
|
||||||
class SrsKafkaClient;
|
class SrsKafkaClient;
|
||||||
|
class SrsJsonObject;
|
||||||
|
class SrsKafkaProducer;
|
||||||
|
|
||||||
#include <srs_app_thread.hpp>
|
#include <srs_app_thread.hpp>
|
||||||
|
#include <srs_app_server.hpp>
|
||||||
|
#include <srs_app_async_call.hpp>
|
||||||
|
|
||||||
#ifdef SRS_AUTO_KAFKA
|
#ifdef SRS_AUTO_KAFKA
|
||||||
|
|
||||||
|
@ -60,6 +64,24 @@ public:
|
||||||
virtual std::string hostport();
|
virtual std::string hostport();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the following is all types of kafka messages.
|
||||||
|
*/
|
||||||
|
struct SrsKafkaMessageOnClient : public ISrsAsyncCallTask
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SrsKafkaProducer* producer;
|
||||||
|
SrsListenerType type;
|
||||||
|
std::string ip;
|
||||||
|
public:
|
||||||
|
SrsKafkaMessageOnClient(SrsKafkaProducer* p, SrsListenerType t, std::string i);
|
||||||
|
virtual ~SrsKafkaMessageOnClient();
|
||||||
|
// interface ISrsAsyncCallTask
|
||||||
|
public:
|
||||||
|
virtual int call();
|
||||||
|
virtual std::string to_string();
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the kafka producer used to save log to kafka cluster.
|
* the kafka producer used to save log to kafka cluster.
|
||||||
*/
|
*/
|
||||||
|
@ -73,6 +95,7 @@ private:
|
||||||
st_cond_t metadata_expired;
|
st_cond_t metadata_expired;
|
||||||
public:
|
public:
|
||||||
std::vector<SrsKafkaPartition*> partitions;
|
std::vector<SrsKafkaPartition*> partitions;
|
||||||
|
std::vector<SrsJsonObject*> objects;
|
||||||
private:
|
private:
|
||||||
SrsLbRoundRobin* lb;
|
SrsLbRoundRobin* lb;
|
||||||
SrsAsyncCallWorker* worker;
|
SrsAsyncCallWorker* worker;
|
||||||
|
@ -85,6 +108,17 @@ public:
|
||||||
virtual int initialize();
|
virtual int initialize();
|
||||||
virtual int start();
|
virtual int start();
|
||||||
virtual void stop();
|
virtual void stop();
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* when got any client connect to SRS, notify kafka.
|
||||||
|
*/
|
||||||
|
virtual int on_client(SrsListenerType type, st_netfd_t stfd);
|
||||||
|
/**
|
||||||
|
* send json object to kafka cluster.
|
||||||
|
* the producer will aggregate message and send in kafka message set.
|
||||||
|
* @param obj the json object; user must never free it again.
|
||||||
|
*/
|
||||||
|
virtual int send(SrsJsonObject* obj);
|
||||||
// interface ISrsReusableThreadHandler
|
// interface ISrsReusableThreadHandler
|
||||||
public:
|
public:
|
||||||
virtual int cycle();
|
virtual int cycle();
|
||||||
|
@ -95,6 +129,7 @@ private:
|
||||||
virtual int request_metadata();
|
virtual int request_metadata();
|
||||||
// set the metadata to invalid and refresh it.
|
// set the metadata to invalid and refresh it.
|
||||||
virtual void refresh_metadata();
|
virtual void refresh_metadata();
|
||||||
|
virtual int flush();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1289,6 +1289,14 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
|
||||||
}
|
}
|
||||||
srs_assert(conn);
|
srs_assert(conn);
|
||||||
|
|
||||||
|
#ifdef SRS_AUTO_KAFKA
|
||||||
|
// notify kafka cluster.
|
||||||
|
if ((ret = kafka->on_client(type, client_stfd)) != ERROR_SUCCESS) {
|
||||||
|
srs_error("kafka handler on_client failed. ret=%d", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// directly enqueue, the cycle thread will remove the client.
|
// directly enqueue, the cycle thread will remove the client.
|
||||||
conns.push_back(conn);
|
conns.push_back(conn);
|
||||||
srs_verbose("add conn to vector.");
|
srs_verbose("add conn to vector.");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue