npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nice-grpc-server-middleware-terminator

v2.0.11

Published

Server middleware for nice-grpc to terminate long-running calls on shutdown

Downloads

10,752

Readme

nice-grpc-server-middleware-terminator npm version

Server middleware for nice-grpc that makes it possible to prevent long-running calls from blocking server graceful shutdown.

When server.shutdown() is called, the server stops accepting new calls, but the returned promise only resolves once all inflight requests finish. If you have a long-running call like an infinite stream, the shutdown will block until the client cancels the call. With this middleware, service implementation methods can alter this behavior, so that on shutdown the call would be aborted and clients would receive gRPC error UNAVAILABLE: Server shutting down.

Installation

npm install nice-grpc-server-middleware-terminator

Usage

Consider the following service definition with a streaming method:

service ExampleService {
  rpc ExampleMethod(ExampleRequest)
    returns (stream ExampleResponse) {};
}

In this example implementation we emit a response every second until aborted:

import {ServiceImplementation, CallContext} from 'nice-grpc';
import {TerminatorContext} from 'nice-grpc-server-middleware-terminator';
import {delay} from 'abort-controller-x';
import {
  ExampleServiceDefinition,
  ExampleRequest,
  ExampleResponse,
  DeepPartial,
} from './compiled_proto/example';

const exampleServiceImpl: ServiceImplementation<
  typeof ExampleServiceDefinition,
  TerminatorContext
> = {
  async *exampleMethod(
    request: ExampleRequest,
    context: CallContext & TerminatorContext,
  ): AsyncIterable<DeepPartial<ExampleResponse>> {
    // When `terminatorMiddleware.terminate()` is called, `context.signal` will
    // be aborted. Note that the method is still responsible for aborting all
    // the work once `context.signal` is aborted.
    context.abortOnTerminate();

    while (true) {
      await delay(context.signal, 1000);

      yield {
        /* ... */
      };
    }
  },
};

Attach the middleware to the server and terminate it before shutdown:

import {createServer} from 'nice-grpc';
import {TerminatorMiddleware} from 'nice-grpc-server-middleware-terminator';
import {ExampleServiceDefinition} from './compiled_proto/example';

const terminatorMiddleware = TerminatorMiddleware();

const server = createServer().use(terminatorMiddleware);
server.add(ExampleServiceDefinition, exampleServiceImpl);
await server.listen('0.0.0.0:8080');

// ... terminate middleware before shutdown:

terminatorMiddleware.terminate();
await server.shutdown();