Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit 95a41a3

Browse files
Use a multithreaded python server to serve RPMs
SimpleHTTPServer is single threaded and so a long download will cause liveness checks to fail. This is a more complicated variant that runs multiple threads.
1 parent c06625a commit 95a41a3

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

pkg/steps/rpm_server.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,46 @@ func (s *rpmServerStep) Run(ctx context.Context, dry bool) error {
9393
Name: RPMRepoName,
9494
Image: imageReference,
9595
ImagePullPolicy: coreapi.PullAlways,
96-
Command: []string{"/bin/python"},
97-
Args: []string{"-m", "SimpleHTTPServer", "8080"},
98-
WorkingDir: RPMServeLocation,
96+
97+
// SimpleHTTPServer is too simple - it can't handle threading. Use a threaded implementation
98+
// that binds multiple simple servers to the same port.
99+
Command: []string{"/bin/bash", "-c"},
100+
Args: []string{
101+
`
102+
#!/bin/bash
103+
cat <<END >>/tmp/serve.py
104+
import time, threading, socket, SocketServer, BaseHTTPServer, SimpleHTTPServer
105+
106+
# Create socket
107+
addr = ('', 8080)
108+
sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
109+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
110+
sock.bind(addr)
111+
sock.listen(5)
112+
113+
# Launch multiple listeners as threads
114+
class Thread(threading.Thread):
115+
def __init__(self, i):
116+
threading.Thread.__init__(self)
117+
self.i = i
118+
self.daemon = True
119+
self.start()
120+
def run(self):
121+
httpd = BaseHTTPServer.HTTPServer(addr, SimpleHTTPServer.SimpleHTTPRequestHandler, False)
122+
123+
# Prevent the HTTP server from re-binding every handler.
124+
# https://stackoverflow.com/questions/46210672/
125+
httpd.socket = sock
126+
httpd.server_bind = self.server_close = lambda self: None
127+
128+
httpd.serve_forever()
129+
[Thread(i) for i in range(100)]
130+
time.sleep(9e9)
131+
END
132+
python /tmp/serve.py
133+
`,
134+
},
135+
WorkingDir: RPMServeLocation,
99136
Ports: []coreapi.ContainerPort{{
100137
ContainerPort: 8080,
101138
Protocol: coreapi.ProtocolTCP,

0 commit comments

Comments
 (0)