Browse Source

Read stats off for any new user or worker generated

master
Con Kolivas 9 years ago
parent
commit
4151ddb4cd
  1. 75
      src/stratifier.c

75
src/stratifier.c

@ -2783,8 +2783,6 @@ static void reconnect_client_id(sdata_t *sdata, const int64_t client_id)
/* API commands */ /* API commands */
static user_instance_t *get_user(sdata_t *sdata, const char *username);
static json_t *userinfo(const user_instance_t *user) static json_t *userinfo(const user_instance_t *user)
{ {
json_t *val; json_t *val;
@ -2903,8 +2901,6 @@ out:
_Close(sockd); _Close(sockd);
} }
static worker_instance_t *get_worker(sdata_t *sdata, user_instance_t *user, const char *workername);
static json_t *workerinfo(const user_instance_t *user, const worker_instance_t *worker) static json_t *workerinfo(const user_instance_t *user, const worker_instance_t *worker)
{ {
json_t *val; json_t *val;
@ -4001,20 +3997,33 @@ static user_instance_t *__create_user(sdata_t *sdata, const char *username)
return user; return user;
} }
/* Find user by username or create one if it doesn't already exist */ /* Find user by username or create one if it doesn't already exist */
static user_instance_t *get_user(sdata_t *sdata, const char *username) static user_instance_t *get_create_user(ckpool_t *ckp, sdata_t *sdata, const char *username, bool *new_user)
{ {
user_instance_t *user; user_instance_t *user;
ck_wlock(&sdata->instance_lock); ck_wlock(&sdata->instance_lock);
HASH_FIND_STR(sdata->user_instances, username, user); HASH_FIND_STR(sdata->user_instances, username, user);
if (unlikely(!user)) if (unlikely(!user)) {
user = __create_user(sdata, username); user = __create_user(sdata, username);
*new_user = true;
}
ck_wunlock(&sdata->instance_lock); ck_wunlock(&sdata->instance_lock);
if (CKP_STANDALONE(ckp) && *new_user)
read_userstats(ckp, user);
return user; return user;
} }
static user_instance_t *get_user(sdata_t *sdata, const char *username)
{
bool dummy;
return get_create_user(sdata->ckp, sdata, username, &dummy);
}
static worker_instance_t *__create_worker(user_instance_t *user, const char *workername) static worker_instance_t *__create_worker(user_instance_t *user, const char *workername)
{ {
worker_instance_t *worker = ckzalloc(sizeof(worker_instance_t)); worker_instance_t *worker = ckzalloc(sizeof(worker_instance_t));
@ -4041,19 +4050,32 @@ static worker_instance_t *__get_worker(user_instance_t *user, const char *worker
/* Find worker amongst a user's workers by workername or create one if it /* Find worker amongst a user's workers by workername or create one if it
* doesn't yet exist. */ * doesn't yet exist. */
static worker_instance_t *get_worker(sdata_t *sdata, user_instance_t *user, const char *workername) static worker_instance_t *get_create_worker(ckpool_t *ckp, sdata_t *sdata, user_instance_t *user,
const char *workername, bool *new_worker)
{ {
worker_instance_t *worker; worker_instance_t *worker;
ck_wlock(&sdata->instance_lock); ck_wlock(&sdata->instance_lock);
worker = __get_worker(user, workername); worker = __get_worker(user, workername);
if (!worker) if (!worker) {
worker = __create_worker(user, workername); worker = __create_worker(user, workername);
*new_worker = true;
}
ck_wunlock(&sdata->instance_lock); ck_wunlock(&sdata->instance_lock);
if (CKP_STANDALONE(ckp) && *new_worker)
read_workerstats(ckp, worker);
return worker; return worker;
} }
static worker_instance_t *get_worker(sdata_t *sdata, user_instance_t *user, const char *workername)
{
bool dummy;
return get_create_worker(sdata->ckp, sdata, user, workername, &dummy);
}
/* This simply strips off the first part of the workername and matches it to a /* This simply strips off the first part of the workername and matches it to a
* user or creates a new one. Needs to be entered with client holding a ref * user or creates a new one. Needs to be entered with client holding a ref
* count. */ * count. */
@ -4063,6 +4085,7 @@ static user_instance_t *generate_user(ckpool_t *ckp, stratum_instance_t *client,
char *base_username = strdupa(workername), *username; char *base_username = strdupa(workername), *username;
bool new_user = false, new_worker = false; bool new_user = false, new_worker = false;
sdata_t *sdata = ckp->data; sdata_t *sdata = ckp->data;
worker_instance_t *worker;
user_instance_t *user; user_instance_t *user;
int len; int len;
@ -4073,30 +4096,18 @@ static user_instance_t *generate_user(ckpool_t *ckp, stratum_instance_t *client,
if (unlikely(len > 127)) if (unlikely(len > 127))
username[127] = '\0'; username[127] = '\0';
ck_wlock(&sdata->instance_lock); user = get_create_user(ckp, sdata, username, &new_user);
HASH_FIND_STR(sdata->user_instances, username, user); worker = get_create_worker(ckp, sdata, user, workername, &new_worker);
if (!user) {
/* New user instance. Secondary user id will be NULL */
user = __create_user(sdata, username);
new_user = true;
}
client->user_instance = user;
client->worker_instance = __get_worker(user, workername);
/* Create one worker instance for combined data from workers of the /* Create one worker instance for combined data from workers of the
* same name */ * same name */
if (!client->worker_instance) { ck_wlock(&sdata->instance_lock);
client->worker_instance = __create_worker(user, workername); client->user_instance = user;
new_worker = true; client->worker_instance = worker;
}
DL_APPEND(user->clients, client); DL_APPEND(user->clients, client);
__inc_worker(sdata,user); __inc_worker(sdata,user);
ck_wunlock(&sdata->instance_lock); ck_wunlock(&sdata->instance_lock);
if (CKP_STANDALONE(ckp) && new_user)
read_userstats(ckp, user);
if (CKP_STANDALONE(ckp) && new_worker)
read_workerstats(ckp, client->worker_instance);
if (new_user && !ckp->proxy) { if (new_user && !ckp->proxy) {
/* Is this a btc address based username? */ /* Is this a btc address based username? */
if (len > 26 && len < 35) if (len > 26 && len < 35)
@ -5472,17 +5483,7 @@ static user_instance_t *generate_remote_user(ckpool_t *ckp, const char *workerna
if (unlikely(len > 127)) if (unlikely(len > 127))
username[127] = '\0'; username[127] = '\0';
ck_wlock(&sdata->instance_lock); user = get_create_user(ckp, sdata, username, &new_user);
HASH_FIND_STR(sdata->user_instances, username, user);
if (!user) {
/* New user instance. Secondary user id will be NULL */
user = __create_user(sdata, username);
new_user = true;
}
ck_wunlock(&sdata->instance_lock);
if (CKP_STANDALONE(ckp) && new_user)
read_userstats(ckp, user);
if (new_user && !ckp->proxy) { if (new_user && !ckp->proxy) {
/* Is this a btc address based username? */ /* Is this a btc address based username? */

Loading…
Cancel
Save