Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync the fix from NNG upstream #204

Merged
merged 12 commits into from
Oct 10, 2023
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ endif ()

# Expire threads. This runs the timeout handling, and having more of them
# reduces contention on the common locks used for aio expiration.
# The default is to allocate up to max(8, ncpu). The upper limit can be
# overridden here.
set(NNG_MAX_EXPIRE_THREADS 8 CACHE STRING "Upper bound on expire threads, 0 for no limit")
mark_as_advanced(NNG_MAX_EXPIRE_THREADS)
if (NNG_MAX_EXPIRE_THREADS)
Expand Down
2 changes: 1 addition & 1 deletion demo/async/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ client(const char *url, const char *msecstr)
}

if ((rv = nng_sendmsg(sock, msg, 0)) != 0) {
fatal("nng_send", rv);
fatal("nng_sendmsg", rv);
}

if ((rv = nng_recvmsg(sock, &msg, 0)) != 0) {
Expand Down
18 changes: 18 additions & 0 deletions demo/pubsub_forwarder/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This software is supplied under the terms of the MIT License, a
# copy of which should be located in the distribution where this
# file was obtained (LICENSE.txt). A copy of the license may also be
# found online at https://opensource.org/licenses/MIT.

cmake_minimum_required(VERSION 3.10)
project(pubsub_forwarder C)

# Find the nng library
find_package(nng REQUIRED)

# Add the executable target
add_executable(pubsub_forwarder pubsub_forwarder.c)

target_compile_options(pubsub_forwarder PRIVATE -Wall -Wextra -Wpedantic -Werror -O2)

# Link against the nng library
target_link_libraries(pubsub_forwarder PRIVATE nng)
62 changes: 62 additions & 0 deletions demo/pubsub_forwarder/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
= PubSub Forwarder

This is a trivial example of a forwarder/proxy for the pub/sub pattern.

The concept is as follows: the forwarder will listen for connections on
both a front-end port and a back-end port. The front-end will act as a
subscriber so that publishers can publish to it. The back-end will act
as a publisher so that subscribers can subscribe to it. The front-end
then forwards to the back end.

== Compiling

CMake with ninja-build is simplest:

[source, bash]
----
cmake -GNinja -B build
cd build
ninja
----

Or if you prefer a traditional approach,
the following is an example typical of UNIX and similar systems like
Linux and macOS may appeal:

[source, bash]
----
export CPPFLAGS="-I /usr/local/include"
export LDFLAGS="-L /usr/local/lib -lnng"
export CC="cc"
${CC} ${CPPFLAGS} pubsub_forwarder.c -o pubsub_forwarder ${LDFLAGS}
----

== Running

An example setup for running this example would involve the following:

. Step 1: Run this example binary (in the background or a terminal, etc)
. Step 2: In a new terminal, run the following

[source, bash]
----
nngcat --sub --dial "tcp://localhost:3328" --quoted
----

. Step 3: In a second terminal, run the same command again to give us two subscribers

[source, bash]
----
nngcat --sub --dial "tcp://localhost:3328" --quoted
----


. In a third terminal, run the following to publish a counter

[source, bash]
----
for n in $(seq 0 99); do nngcat --pub --dial "tcp://localhost:3327" --data "$n"; done
----



96 changes: 96 additions & 0 deletions demo/pubsub_forwarder/pubsub_forwarder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

//
// Forwarder example based on https://github.com/C-o-r-E/nng_pubsub_proxy
//
// This example shows how to use raw sockets to set up a forwarder or proxy for
// pub/sub.
//
// An example setup for running this example would involve the following:
//
// - Run this example binary (in the background or a terminal, etc)
// - In a new terminal, run
// `nngcat --sub --dial "tcp://localhost:3328" --quoted`
// - In a second terminal, run
// `nngcat --sub --dial "tcp://localhost:3328" --quoted`
// - In a third terminal, run
// `for n in $(seq 0 99);`
// `do nngcat --pub --dial "tcp://localhost:3327" --data "$n";`
// `done`
//
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>

#define PROXY_FRONT_URL "tcp://localhost:3327"
#define PROXY_BACK_URL "tcp://localhost:3328"

void
panic_on_error(int should_panic, const char *format, ...)
{
if (should_panic) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
exit(EXIT_FAILURE);
}
}

int
main()
{
nng_socket sock_front_end = NNG_SOCKET_INITIALIZER;
nng_socket sock_back_end = NNG_SOCKET_INITIALIZER;
int ret = 0;

//
// First we need some nng sockets. Not to be confused with network
// sockets
//
ret = nng_sub0_open_raw(&sock_front_end);
panic_on_error(ret, "Failed to open front end socket\n");

ret = nng_pub0_open_raw(&sock_back_end);
panic_on_error(ret, "Failed to open back end socket\n");

//
// Now we need to set up a listener for each socket so that they have
// addresses
//

nng_listener front_ls = NNG_LISTENER_INITIALIZER;
nng_listener back_ls = NNG_LISTENER_INITIALIZER;

ret = nng_listener_create(&front_ls, sock_front_end, PROXY_FRONT_URL);
panic_on_error(ret, "Failed to create front listener\n");

ret = nng_listener_create(&back_ls, sock_back_end, PROXY_BACK_URL);
panic_on_error(ret, "Failed to create back listener\n");

ret = nng_listener_start(front_ls, 0);
panic_on_error(ret, "Failed to start front listener\n");

ret = nng_listener_start(back_ls, 0);
panic_on_error(ret, "Failed to start back listener\n");

//
// Finally let nng do the forwarding/proxying
//

ret = nng_device(sock_front_end, sock_back_end);
panic_on_error(
ret, "nng_device returned %d: %s\n", ret, nng_strerror(ret));

printf("done");
return 0;
}
22 changes: 10 additions & 12 deletions docs/man/nng_aio_stop.3.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_aio_stop(3)
//
// Copyright 2018 Staysail Systems, Inc. <[email protected]>
// Copyright 2023 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This document is supplied under the terms of the MIT License, a
Expand All @@ -26,18 +26,15 @@ void nng_aio_stop(nng_aio *aio);

The `nng_aio_stop()` function stops the asynchronous I/O operation
associated with _aio_ by aborting with `NNG_ECANCELED`, and then waits
for it to complete or to be completely aborted.
for it to complete or to be completely aborted, and for the any
callback associated with the _aio_ to have completed executing.

If an operation is in progress when this function is called, that operation
is canceled and the callback function is _not_ allowed to run.
Further calls to
xref:nng_aio_schedule.3.adoc[`nng_aio_schedule()`] using this _aio_ will fail with
`NNG_ECLOSED`.

If the callback function is already running when this function is called,
then it is allowed to complete before returning to the caller.

No new operations will be started on this _aio_.

NOTE: Calling this function means that the operation may be aborted without
completing its callback function.
It is safe to call this for an _aio_, even when no operation is currently
pending for it.

TIP: When multiple asynchronous I/O handles are in use and need to be
shut down, it is safest to stop all of them, before deallocating any of
Expand All @@ -55,9 +52,10 @@ None.
== SEE ALSO

[.text-left]
xref:nng_aio_alloc.3.adoc[nng_aio_alloc(3)],
xref:nng_aio_cancel.3.adoc[nng_aio_cancel(3)],
xref:nng_aio_free.3.adoc[nng_aio_free(3)],
xref:nng_aio_schedule.3.adoc[nng_aio_schedule(3)],
xref:nng_aio_wait.3.adoc[nng_aio_wait(3)],
xref:nng_aio_alloc.3.adoc[nng_aio_alloc(3)],
xref:nng_aio.5.adoc[nng_aio(5)],
xref:nng.7.adoc[nng(7)]
2 changes: 1 addition & 1 deletion docs/man/nng_thread_set_name.3supp.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ nng_thread_set_name - set thread name
#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>

void nng_set_thread_name(nng_thread *thread, const char *name);
void nng_thread_set_name(nng_thread *thread, const char *name);
----

== DESCRIPTION
Expand Down
46 changes: 40 additions & 6 deletions etc/pubrefman/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
module go.nanomsg.org/nng/pubrefman

go 1.14
go 1.18

require (
github.com/bytesparadise/libasciidoc v0.3.1-0.20200802124845-5dcda3220c31
github.com/go-git/go-billy/v5 v5.0.0
github.com/go-git/go-git/v5 v5.1.0
github.com/google/uuid v1.1.1
github.com/spf13/jwalterweatherman v1.0.0
github.com/bytesparadise/libasciidoc v0.8.0
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.9.0
github.com/google/uuid v1.3.1
github.com/spf13/jwalterweatherman v1.1.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/alecthomas/chroma/v2 v2.9.1 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/pprof v0.0.0-20230912144702-c363fe2c2ed8 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mna/pigeon v1.1.0 // indirect
github.com/onsi/ginkgo/v2 v2.12.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading
Loading