blob: efc611b8b78e80c80673a32ae8012f8ecc9ce492 [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
murgatroid99749666e2015-01-12 18:25:58 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
murgatroid99e5061512015-01-12 18:14:35 -080034#include <node.h>
35#include <nan.h>
36
37#include "grpc/grpc.h"
murgatroid9977659062015-02-11 09:26:25 -080038#include "grpc/support/log.h"
murgatroid99e5061512015-01-12 18:14:35 -080039#include "grpc/support/time.h"
40#include "completion_queue_async_worker.h"
murgatroid99016bb502015-02-09 15:55:10 -080041#include "call.h"
murgatroid99e5061512015-01-12 18:14:35 -080042
43namespace grpc {
44namespace node {
45
murgatroid997ab95fb2015-02-02 16:50:07 -080046const int max_queue_threads = 2;
47
murgatroid99e5061512015-01-12 18:14:35 -080048using v8::Function;
49using v8::Handle;
50using v8::Object;
51using v8::Persistent;
52using v8::Value;
53
54grpc_completion_queue *CompletionQueueAsyncWorker::queue;
55
murgatroid99e339f6f2015-09-23 11:38:39 -070056// Invariants: current_threads <= max_queue_threads
57// (current_threads == max_queue_threads) || (waiting_next_calls == 0)
58
murgatroid997ab95fb2015-02-02 16:50:07 -080059int CompletionQueueAsyncWorker::current_threads;
60int CompletionQueueAsyncWorker::waiting_next_calls;
61
Craig Tillere8e304e2015-01-13 14:41:29 -080062CompletionQueueAsyncWorker::CompletionQueueAsyncWorker()
63 : NanAsyncWorker(NULL) {}
murgatroid99e5061512015-01-12 18:14:35 -080064
Craig Tillere8e304e2015-01-13 14:41:29 -080065CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {}
murgatroid99e5061512015-01-12 18:14:35 -080066
67void CompletionQueueAsyncWorker::Execute() {
Craig Tiller354398f2015-07-13 09:16:03 -070068 result =
Nicolas "Pixel" Noble8e6bab52015-08-07 01:40:49 +020069 grpc_completion_queue_next(queue, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
Craig Tiller64be9f72015-05-04 14:53:51 -070070 if (!result.success) {
murgatroid99937ac9b2015-08-13 11:00:13 -070071 SetErrorMessage("The async function encountered an error");
murgatroid99d4d67ad2015-02-09 10:43:21 -080072 }
murgatroid99e5061512015-01-12 18:14:35 -080073}
74
Craig Tillere8e304e2015-01-13 14:41:29 -080075grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; }
murgatroid99e5061512015-01-12 18:14:35 -080076
77void CompletionQueueAsyncWorker::Next() {
78 NanScope();
murgatroid997ab95fb2015-02-02 16:50:07 -080079 if (current_threads < max_queue_threads) {
murgatroid99e339f6f2015-09-23 11:38:39 -070080 current_threads += 1;
murgatroid997ab95fb2015-02-02 16:50:07 -080081 CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
82 NanAsyncQueueWorker(worker);
83 } else {
84 waiting_next_calls += 1;
85 }
murgatroid99e339f6f2015-09-23 11:38:39 -070086 GPR_ASSERT(current_threads <= max_queue_threads);
87 GPR_ASSERT((current_threads == max_queue_threads) ||
88 (waiting_next_calls == 0));
murgatroid99e5061512015-01-12 18:14:35 -080089}
90
91void CompletionQueueAsyncWorker::Init(Handle<Object> exports) {
92 NanScope();
murgatroid997ab95fb2015-02-02 16:50:07 -080093 current_threads = 0;
94 waiting_next_calls = 0;
Nicolas "Pixel" Noble8e6bab52015-08-07 01:40:49 +020095 queue = grpc_completion_queue_create(NULL);
murgatroid99e5061512015-01-12 18:14:35 -080096}
97
98void CompletionQueueAsyncWorker::HandleOKCallback() {
99 NanScope();
murgatroid997ab95fb2015-02-02 16:50:07 -0800100 if (waiting_next_calls > 0) {
101 waiting_next_calls -= 1;
murgatroid99e339f6f2015-09-23 11:38:39 -0700102 // Old worker removed, new worker added. current_threads += 0
murgatroid997ab95fb2015-02-02 16:50:07 -0800103 CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
104 NanAsyncQueueWorker(worker);
105 } else {
106 current_threads -= 1;
107 }
murgatroid99e339f6f2015-09-23 11:38:39 -0700108 GPR_ASSERT(current_threads <= max_queue_threads);
109 GPR_ASSERT((current_threads == max_queue_threads) ||
110 (waiting_next_calls == 0));
Craig Tiller64be9f72015-05-04 14:53:51 -0700111 NanCallback *callback = GetTagCallback(result.tag);
112 Handle<Value> argv[] = {NanNull(), GetTagNodeValue(result.tag)};
murgatroid991578c6a2015-02-11 16:19:55 -0800113 callback->Call(2, argv);
murgatroid99e5061512015-01-12 18:14:35 -0800114
Craig Tiller64be9f72015-05-04 14:53:51 -0700115 DestroyTag(result.tag);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800116}
murgatroid99e5061512015-01-12 18:14:35 -0800117
murgatroid99d4d67ad2015-02-09 10:43:21 -0800118void CompletionQueueAsyncWorker::HandleErrorCallback() {
119 NanScope();
murgatroid99e339f6f2015-09-23 11:38:39 -0700120 if (waiting_next_calls > 0) {
121 waiting_next_calls -= 1;
122 // Old worker removed, new worker added. current_threads += 0
123 CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
124 NanAsyncQueueWorker(worker);
125 } else {
126 current_threads -= 1;
127 }
128 GPR_ASSERT(current_threads <= max_queue_threads);
129 GPR_ASSERT((current_threads == max_queue_threads) ||
130 (waiting_next_calls == 0));
Craig Tiller64be9f72015-05-04 14:53:51 -0700131 NanCallback *callback = GetTagCallback(result.tag);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800132 Handle<Value> argv[] = {NanError(ErrorMessage())};
133
murgatroid991578c6a2015-02-11 16:19:55 -0800134 callback->Call(1, argv);
murgatroid9977659062015-02-11 09:26:25 -0800135
Craig Tiller64be9f72015-05-04 14:53:51 -0700136 DestroyTag(result.tag);
murgatroid99e5061512015-01-12 18:14:35 -0800137}
138
139} // namespace node
140} // namespace grpc