OLD | NEW |
(Empty) | |
| 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 CompletionQueueAsyncWorker::CompletionQueueAsyncWorker() |
| 62 : Nan::AsyncWorker(NULL) {} |
| 63 |
| 64 CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {} |
| 65 |
| 66 void CompletionQueueAsyncWorker::Execute() { |
| 67 result = |
| 68 grpc_completion_queue_next(queue, gpr_inf_future(GPR_CLOCK_REALTIME), NULL
); |
| 69 if (!result.success) { |
| 70 SetErrorMessage("The async function encountered an error"); |
| 71 } |
| 72 } |
| 73 |
| 74 grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; } |
| 75 |
| 76 void CompletionQueueAsyncWorker::Next() { |
| 77 Nan::HandleScope scope; |
| 78 if (current_threads < max_queue_threads) { |
| 79 current_threads += 1; |
| 80 CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
| 81 Nan::AsyncQueueWorker(worker); |
| 82 } else { |
| 83 waiting_next_calls += 1; |
| 84 } |
| 85 GPR_ASSERT(current_threads <= max_queue_threads); |
| 86 GPR_ASSERT((current_threads == max_queue_threads) || |
| 87 (waiting_next_calls == 0)); |
| 88 } |
| 89 |
| 90 void CompletionQueueAsyncWorker::Init(Local<Object> exports) { |
| 91 Nan::HandleScope scope; |
| 92 current_threads = 0; |
| 93 waiting_next_calls = 0; |
| 94 queue = grpc_completion_queue_create(NULL); |
| 95 } |
| 96 |
| 97 void CompletionQueueAsyncWorker::HandleOKCallback() { |
| 98 Nan::HandleScope scope; |
| 99 if (waiting_next_calls > 0) { |
| 100 waiting_next_calls -= 1; |
| 101 // Old worker removed, new worker added. current_threads += 0 |
| 102 CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
| 103 Nan::AsyncQueueWorker(worker); |
| 104 } else { |
| 105 current_threads -= 1; |
| 106 } |
| 107 GPR_ASSERT(current_threads <= max_queue_threads); |
| 108 GPR_ASSERT((current_threads == max_queue_threads) || |
| 109 (waiting_next_calls == 0)); |
| 110 Nan::Callback *callback = GetTagCallback(result.tag); |
| 111 Local<Value> argv[] = {Nan::Null(), GetTagNodeValue(result.tag)}; |
| 112 callback->Call(2, argv); |
| 113 |
| 114 DestroyTag(result.tag); |
| 115 } |
| 116 |
| 117 void CompletionQueueAsyncWorker::HandleErrorCallback() { |
| 118 if (waiting_next_calls > 0) { |
| 119 waiting_next_calls -= 1; |
| 120 // Old worker removed, new worker added. current_threads += 0 |
| 121 CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
| 122 Nan::AsyncQueueWorker(worker); |
| 123 } else { |
| 124 current_threads -= 1; |
| 125 } |
| 126 GPR_ASSERT(current_threads <= max_queue_threads); |
| 127 GPR_ASSERT((current_threads == max_queue_threads) || |
| 128 (waiting_next_calls == 0)); |
| 129 Nan::HandleScope scope; |
| 130 Nan::Callback *callback = GetTagCallback(result.tag); |
| 131 Local<Value> argv[] = {Nan::Error(ErrorMessage())}; |
| 132 |
| 133 callback->Call(1, argv); |
| 134 |
| 135 DestroyTag(result.tag); |
| 136 } |
| 137 |
| 138 } // namespace node |
| 139 } // namespace grpc |
OLD | NEW |