Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * 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 :
34 : #include <node.h>
35 : #include <nan.h>
36 :
37 : #include "grpc/grpc.h"
38 : #include "grpc/support/log.h"
39 : #include "grpc/support/time.h"
40 : #include "completion_queue_async_worker.h"
41 : #include "call.h"
42 :
43 : namespace grpc {
44 : namespace node {
45 :
46 : const int max_queue_threads = 2;
47 :
48 : using v8::Function;
49 : using v8::Local;
50 : using v8::Object;
51 : using v8::Value;
52 :
53 : grpc_completion_queue *CompletionQueueAsyncWorker::queue;
54 :
55 : // Invariants: current_threads <= max_queue_threads
56 : // (current_threads == max_queue_threads) || (waiting_next_calls == 0)
57 :
58 : int CompletionQueueAsyncWorker::current_threads;
59 : int CompletionQueueAsyncWorker::waiting_next_calls;
60 :
61 787 : CompletionQueueAsyncWorker::CompletionQueueAsyncWorker()
62 787 : : Nan::AsyncWorker(NULL) {}
63 :
64 1574 : CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {}
65 :
66 787 : void CompletionQueueAsyncWorker::Execute() {
67 : result =
68 787 : grpc_completion_queue_next(queue, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
69 787 : if (!result.success) {
70 36 : SetErrorMessage("The async function encountered an error");
71 : }
72 787 : }
73 :
74 393 : grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; }
75 :
76 787 : void CompletionQueueAsyncWorker::Next() {
77 : Nan::HandleScope scope;
78 787 : if (current_threads < max_queue_threads) {
79 158 : current_threads += 1;
80 158 : CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
81 : Nan::AsyncQueueWorker(worker);
82 : } else {
83 629 : waiting_next_calls += 1;
84 : }
85 787 : GPR_ASSERT(current_threads <= max_queue_threads);
86 787 : GPR_ASSERT((current_threads == max_queue_threads) ||
87 : (waiting_next_calls == 0));
88 787 : }
89 :
90 1 : void CompletionQueueAsyncWorker::Init(Local<Object> exports) {
91 : Nan::HandleScope scope;
92 1 : current_threads = 0;
93 1 : waiting_next_calls = 0;
94 1 : queue = grpc_completion_queue_create(NULL);
95 1 : }
96 :
97 751 : void CompletionQueueAsyncWorker::HandleOKCallback() {
98 : Nan::HandleScope scope;
99 751 : if (waiting_next_calls > 0) {
100 623 : waiting_next_calls -= 1;
101 : // Old worker removed, new worker added. current_threads += 0
102 623 : CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
103 : Nan::AsyncQueueWorker(worker);
104 : } else {
105 128 : current_threads -= 1;
106 : }
107 751 : GPR_ASSERT(current_threads <= max_queue_threads);
108 751 : GPR_ASSERT((current_threads == max_queue_threads) ||
109 : (waiting_next_calls == 0));
110 751 : Nan::Callback *callback = GetTagCallback(result.tag);
111 751 : Local<Value> argv[] = {Nan::Null(), GetTagNodeValue(result.tag)};
112 : callback->Call(2, argv);
113 :
114 751 : DestroyTag(result.tag);
115 751 : }
116 :
117 36 : void CompletionQueueAsyncWorker::HandleErrorCallback() {
118 36 : if (waiting_next_calls > 0) {
119 6 : waiting_next_calls -= 1;
120 : // Old worker removed, new worker added. current_threads += 0
121 6 : CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
122 : Nan::AsyncQueueWorker(worker);
123 : } else {
124 30 : current_threads -= 1;
125 : }
126 36 : GPR_ASSERT(current_threads <= max_queue_threads);
127 36 : GPR_ASSERT((current_threads == max_queue_threads) ||
128 : (waiting_next_calls == 0));
129 : Nan::HandleScope scope;
130 36 : Nan::Callback *callback = GetTagCallback(result.tag);
131 72 : Local<Value> argv[] = {Nan::Error(ErrorMessage())};
132 :
133 : callback->Call(1, argv);
134 :
135 36 : DestroyTag(result.tag);
136 36 : }
137 :
138 : } // namespace node
139 : } // namespace grpc
|