Skip to content

Commit cab7731

Browse files
Added Thread naming based on remote socket address (#738) (#753)
- Added ThreadNameProvider to set name based on Thread Class and remote socket address - Added RemoteAddressProvider to abstract access to Remote Socket Address - Set Reader Thread name in TransportImpl - Set SFTP PacketReader Thread name in SFTPEngine - Set KeepAlive Thread name in SSHClient Co-authored-by: Jeroen van Erp <[email protected]>
1 parent 50073db commit cab7731

File tree

11 files changed

+104
-13
lines changed

11 files changed

+104
-13
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hierynomus.sshj.common;
17+
18+
import java.net.InetSocketAddress;
19+
20+
public interface RemoteAddressProvider {
21+
/**
22+
* Get Remote Socket Address associated with transport connection
23+
*
24+
* @return Remote Socket Address or null when not connected
25+
*/
26+
InetSocketAddress getRemoteSocketAddress();
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hierynomus.sshj.common;
17+
18+
import java.net.InetSocketAddress;
19+
20+
public class ThreadNameProvider {
21+
private static final String DISCONNECTED = "DISCONNECTED";
22+
23+
/**
24+
* Set Thread Name prefixed with sshj followed by class and remote address when connected
25+
*
26+
* @param thread Class of Thread being named
27+
* @param remoteAddressProvider Remote Address Provider associated with Thread
28+
*/
29+
public static void setThreadName(final Thread thread, final RemoteAddressProvider remoteAddressProvider) {
30+
final InetSocketAddress remoteSocketAddress = remoteAddressProvider.getRemoteSocketAddress();
31+
final String address = remoteSocketAddress == null ? DISCONNECTED : remoteSocketAddress.toString();
32+
final String threadName = String.format("sshj-%s-%s", thread.getClass().getSimpleName(), address);
33+
thread.setName(threadName);
34+
}
35+
}

src/main/java/net/schmizz/keepalive/Heartbeater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Heartbeater
2424
extends KeepAlive {
2525

2626
Heartbeater(ConnectionImpl conn) {
27-
super(conn, "heartbeater");
27+
super(conn, "sshj-Heartbeater");
2828
}
2929

3030
@Override

src/main/java/net/schmizz/keepalive/KeepAliveRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class KeepAliveRunner extends KeepAlive {
3737
new LinkedList<Promise<SSHPacket, ConnectionException>>();
3838

3939
KeepAliveRunner(ConnectionImpl conn) {
40-
super(conn, "keep-alive");
40+
super(conn, "sshj-KeepAliveRunner");
4141
}
4242

4343
synchronized public int getMaxAliveCount() {

src/main/java/net/schmizz/sshj/SSHClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package net.schmizz.sshj;
1717

1818
import net.schmizz.keepalive.KeepAlive;
19+
import com.hierynomus.sshj.common.ThreadNameProvider;
1920
import net.schmizz.sshj.common.*;
2021
import net.schmizz.sshj.connection.Connection;
2122
import net.schmizz.sshj.connection.ConnectionException;
@@ -56,6 +57,7 @@
5657
import java.io.Closeable;
5758
import java.io.File;
5859
import java.io.IOException;
60+
import java.net.InetSocketAddress;
5961
import java.net.ServerSocket;
6062
import java.nio.charset.Charset;
6163
import java.security.KeyPair;
@@ -443,6 +445,16 @@ public Connection getConnection() {
443445
return conn;
444446
}
445447

448+
/**
449+
* Get Remote Socket Address from Transport
450+
*
451+
* @return Remote Socket Address or null when not connected
452+
*/
453+
@Override
454+
public InetSocketAddress getRemoteSocketAddress() {
455+
return trans.getRemoteSocketAddress();
456+
}
457+
446458
/**
447459
* Returns the character set used to communicate with the remote machine for certain strings (like paths).
448460
*
@@ -795,6 +807,7 @@ protected void onConnect()
795807
trans.init(getRemoteHostname(), getRemotePort(), getInputStream(), getOutputStream());
796808
final KeepAlive keepAliveThread = conn.getKeepAlive();
797809
if (keepAliveThread.isEnabled()) {
810+
ThreadNameProvider.setThreadName(conn.getKeepAlive(), trans);
798811
keepAliveThread.start();
799812
}
800813
doKex();

src/main/java/net/schmizz/sshj/connection/channel/direct/SessionFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
package net.schmizz.sshj.connection.channel.direct;
1717

18+
import com.hierynomus.sshj.common.RemoteAddressProvider;
1819
import net.schmizz.sshj.common.SSHException;
1920

2021
/** A factory interface for creating SSH {@link Session session channels}. */
21-
public interface SessionFactory {
22+
public interface SessionFactory extends RemoteAddressProvider {
2223

2324
/**
2425
* Opens a {@code session} channel. The returned {@link Session} instance allows {@link Session#exec(String)
@@ -27,7 +28,7 @@ public interface SessionFactory {
2728
*
2829
* @return the opened {@code session} channel
2930
*
30-
* @throws SSHException
31+
* @throws SSHException Thrown on session initialization failures
3132
* @see Session
3233
*/
3334
Session startSession()

src/main/java/net/schmizz/sshj/sftp/PacketReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public PacketReader(SFTPEngine engine) {
4141
this.engine = engine;
4242
log = engine.getLoggerFactory().getLogger(getClass());
4343
this.in = engine.getSubsystem().getInputStream();
44-
setName("sftp reader");
44+
setName("sshj-PacketReader");
4545
setDaemon(true);
4646
}
4747

src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.schmizz.sshj.sftp;
1717

18+
import com.hierynomus.sshj.common.ThreadNameProvider;
1819
import net.schmizz.concurrent.Promise;
1920
import net.schmizz.sshj.common.IOUtils;
2021
import net.schmizz.sshj.common.LoggerFactory;
@@ -68,6 +69,7 @@ public SFTPEngine(SessionFactory ssh, String pathSep)
6869
sub = session.startSubsystem("sftp");
6970
out = sub.getOutputStream();
7071
reader = new PacketReader(this);
72+
ThreadNameProvider.setThreadName(reader, ssh);
7173
pathHelper = new PathHelper(new PathHelper.Canonicalizer() {
7274
@Override
7375
public String canonicalize(String path)

src/main/java/net/schmizz/sshj/transport/Reader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class Reader
2929
public Reader(TransportImpl trans) {
3030
this.trans = trans;
3131
log = trans.getConfig().getLoggerFactory().getLogger(getClass());
32-
setName("reader");
32+
setName("sshj-Reader");
3333
setDaemon(true);
3434
}
3535

src/main/java/net/schmizz/sshj/transport/Transport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.schmizz.sshj.transport;
1717

18+
import com.hierynomus.sshj.common.RemoteAddressProvider;
1819
import com.hierynomus.sshj.key.KeyAlgorithm;
1920
import net.schmizz.sshj.Config;
2021
import net.schmizz.sshj.Service;
@@ -31,7 +32,7 @@
3132

3233
/** Transport layer of the SSH protocol. */
3334
public interface Transport
34-
extends SSHPacketHandler {
35+
extends SSHPacketHandler, RemoteAddressProvider {
3536

3637
/**
3738
* Sets the host information and the streams to be used by this transport. Identification information is exchanged
@@ -208,7 +209,7 @@ long write(SSHPacket payload)
208209
/**
209210
* Specify a {@code listener} that will be notified upon disconnection.
210211
*
211-
* @param listener
212+
* @param listener Disconnect Listener to be configured
212213
*/
213214
void setDisconnectListener(DisconnectListener listener);
214215

0 commit comments

Comments
 (0)