memcache/server.c
author Tero Marttila <terom@fixme.fi>
Thu, 28 Aug 2008 03:12:11 +0300
changeset 46 8a832c0e01ee
parent 43 e5b714190dee
child 48 1c67f512779b
permissions -rw-r--r--
bugfixed, enum->string mappings, test does requests
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include <stdlib.h>
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include <assert.h>
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include "server.h"
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include "connection.h"
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include "request.h"
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
#include "../memcache.h"
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
#include "../common.h"
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
struct memcache_server *memcache_server_alloc (struct config_endpoint *endpoint, int max_connections) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    struct memcache_server *server = NULL;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    if ((server = calloc(1, sizeof(*server))) == NULL)
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        ERROR("calloc");
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    // store the vars
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    server->endpoint = endpoint;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    server->max_connections = max_connections;
46
8a832c0e01ee bugfixed, enum->string mappings, test does requests
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    20
8a832c0e01ee bugfixed, enum->string mappings, test does requests
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    21
    // init lists
8a832c0e01ee bugfixed, enum->string mappings, test does requests
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    22
    LIST_INIT(&server->conn_list);
8a832c0e01ee bugfixed, enum->string mappings, test does requests
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    23
    TAILQ_INIT(&server->req_queue);
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    // grow connpool so as to have a connection ready
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    if (memcache_server_grow_connpool(server))
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        ERROR("error opening initial connection");
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    // success
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    return server;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
error:
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    free(server);
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    return NULL;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
}
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
int memcache_server_grow_connpool (struct memcache_server *server) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
   struct memcache_conn *conn;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
   int count;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
   // count connections
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
   for (count = 0, conn = server->conn_list.lh_first; conn != NULL; conn = conn->connlist_node.le_next, count++) ;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
   // room for more?
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
   if (count < server->max_connections) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        // create a new one
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        if ((conn = memcache_conn_open(server)) == NULL)
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
            return -1;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        // enlist it
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        LIST_INSERT_HEAD(&server->conn_list, conn, connlist_node);
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        // the connection will call memcache_server_coon_ready once it's ready for use...
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
   }
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
   // success
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
   return 0;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
}
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
int memcache_server_add_req (struct memcache_server *server, struct memcache_req *req) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    struct memcache_conn *conn;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    // look for an idle connection
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    for (conn = server->conn_list.lh_first; conn != NULL; conn = conn->connlist_node.le_next) {
39
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    66
        if (memcache_conn_is_available(conn)) {
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
            // we found an idle connection
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
            break;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        }
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    }
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    if (conn != NULL) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        // we found an available connection
43
e5b714190dee the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
    74
        // if the request fails, then we will know via conn_dead
e5b714190dee the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
    75
        memcache_conn_do_req(conn, req);
e5b714190dee the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
    76
e5b714190dee the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
    77
        return 0;
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    } else {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
        // enqueue the request until a connection is available
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        // XXX: queue size limits
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
        TAILQ_INSERT_TAIL(&server->req_queue, req, reqqueue_node);
42
0e503189af2f more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    84
        
0e503189af2f more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    85
        // notify the req
0e503189af2f more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    86
        memcache_req_queued(req);
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
        return 0;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    }
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
}
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
void memcache_server_conn_ready (struct memcache_server *server, struct memcache_conn *conn) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    assert(server == conn->server);
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    // do we have any queued requests waiting?
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    struct memcache_req *req = server->req_queue.tqh_first;
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    if (req) {
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
        // remove it from the queue and execute it
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
        TAILQ_REMOVE(&server->req_queue, req, reqqueue_node);
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
        
43
e5b714190dee the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   102
        // this will take care of any error handling by itself
e5b714190dee the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   103
        memcache_conn_do_req(conn, req);
38
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    }
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
}
9894df13b779 added the beginnings of the memcache client module (only up to connect() yet)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
39
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   107
void memcache_server_conn_dead (struct memcache_server *server, struct memcache_conn *conn) {
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   108
    assert(server == conn->server);
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   109
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   110
    // XXX: reconnect/error out requests?
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   111
    
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   112
    // remove it from the list
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   113
    LIST_REMOVE(conn, connlist_node);
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   114
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   115
    // free it
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   116
    memcache_conn_free(conn);
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   117
}
0e21a65074a6 memcache connect error handling and req queuein
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   118