Subject: | Feature Request: Allow bind to specific IP address |
It would be nice to be able to bind to a specific IP address, rather than all of them. Providing a patch which implements this proposal.
Subject: | specific-ip.patch |
commit a140d615ae5b545256c93eb7e0556cbe550c73b2
Author: Graham Ollis <plicease@cpan.org>
Date: Thu Jun 16 10:30:48 2016 -0400
Add option to bind to a specific IP
diff --git a/gearmand b/gearmand
index 106c6bf..f3cc1ac 100755
--- a/gearmand
+++ b/gearmand
@@ -27,6 +27,10 @@ for running under daemontools/supervise).
Set the port number, defaults to 7003.
+=item --host hostname / -h hostname
+
+Bind to the given hostname or IP address.
+
=item --pidfile=/some/dir/gearmand.pid
Write a pidfile when starting up
@@ -121,11 +125,13 @@ my (
$accept,
$wakeup,
$wakeup_delay,
+ $conf_host,
);
my $conf_port = 7003;
Getopt::Long::GetOptions(
'd|daemonize' => \$daemonize,
+ 'h|host=s' => \$conf_host,
'p|port=i' => \$conf_port,
'debug=i' => \$DEBUG,
'pidfile=s' => \$opt_pidfile,
@@ -147,7 +153,7 @@ my $server = Gearman::Server->new(
wakeup => $wakeup,
wakeup_delay => $wakeup_delay,
);
-my $ssock = $server->create_listening_sock($conf_port, accept_per_loop => $accept);
+my $ssock = $server->create_listening_sock($conf_port, accept_per_loop => $accept, local_addr => $conf_host);
if ($opt_pidfile) {
open my $fh, '>', $opt_pidfile or die "Could not open $opt_pidfile: $!";
diff --git a/lib/Gearman/Server.pm b/lib/Gearman/Server.pm
index 9f64c65..d2e6b64 100644
--- a/lib/Gearman/Server.pm
+++ b/lib/Gearman/Server.pm
@@ -120,9 +120,19 @@ sub debug {
=head2 create_listening_sock
- $server_object->create_listening_sock( $portnum )
+ $server_object->create_listening_sock( $portnum, \%options )
-Add a TCP port listener for incoming Gearman worker and client connections.
+Add a TCP port listener for incoming Gearman worker and client connections. Options:
+
+=over 4
+
+=item accept_per_loop
+
+=item local_addr
+
+Bind socket to only this address.
+
+=back
=cut
@@ -130,6 +140,7 @@ sub create_listening_sock {
my ($self, $portnum, %opts) = @_;
my $accept_per_loop = delete $opts{accept_per_loop};
+ my $local_addr = delete $opts{local_addr};
warn "Extra options passed into create_listening_sock: " . join(', ', keys %opts) . "\n"
if keys %opts;
@@ -139,7 +150,8 @@ sub create_listening_sock {
Proto => IPPROTO_TCP,
Blocking => 0,
Reuse => 1,
- Listen => 1024 )
+ Listen => 1024,
+ ($local_addr ? (LocalAddr => $local_addr) : ()) )
or die "Error creating socket: $@\n";
my $listeners = $self->{listeners};