Skip to content

Clean up and fix port forwarding example. #309

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

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/portforward/PortForward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private static async Task Main(string[] args)
private async static Task Forward(IKubernetes client, V1Pod pod) {
// Note this is single-threaded, it won't handle concurrent requests well...
var webSocket = await client.WebSocketNamespacedPodPortForwardAsync(pod.Metadata.Name, "default", new int[] {80}, "v4.channel.k8s.io");
var demux = new StreamDemuxer(webSocket);
var demux = new StreamDemuxer(webSocket, StreamType.PortForward);
demux.Start();

var stream = demux.GetStream((byte?)0, (byte?)0);
Expand All @@ -37,6 +37,7 @@ private async static Task Forward(IKubernetes client, V1Pod pod) {

Socket handler = null;

// Note this will only accept a single connection
var accept = Task.Run(() => {
while (true) {
handler = listener.Accept();
Expand All @@ -61,6 +62,10 @@ private async static Task Forward(IKubernetes client, V1Pod pod) {

await accept;
await copy;
if (handler != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (handler != null) {
handler?.Close();

handler.Close();
}
listener.Close();
}
}
}