Skip to content

Commit 8201e4c

Browse files
authored
Merge pull request groupcache#5 from Tochemey/main
feat: add TLS support
2 parents 3b42ce3 + ae94224 commit 8201e4c

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*~
22
.idea/
33
.DS_Store
4+
vendor

transport/http_transport.go

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ type HttpTransportOptions struct {
9898
// defaults to http.DefaultClient
9999
Client *http.Client
100100

101-
// Scheme (Optional) is either `http` or `https`. Should always be 'http' as
102-
// 'https' is not currently supported. `Scheme` is reserved here for future use.
103-
// defaults to `http`
101+
// Scheme (Optional) is either `http` or `https`. `Scheme` is reserved here for future use.
102+
// defaults to `http` when TLSConfig is not set.
104103
Scheme string
105104

106105
// BasePath (Optional) specifies the HTTP path that will serve groupcache requests.
@@ -110,15 +109,19 @@ type HttpTransportOptions struct {
110109
// Logger
111110
Logger Logger
112111

113-
// TODO: Support for TLS
112+
// TLS support.
113+
TLSConfig *tls.Config
114114
}
115115

116+
// HttpTransport defines the HTTP transport
116117
type HttpTransport struct {
117-
opts HttpTransportOptions
118-
instance GroupCacheInstance
119-
wg sync.WaitGroup
120-
listener net.Listener
121-
server *http.Server
118+
opts HttpTransportOptions
119+
instance GroupCacheInstance
120+
wg sync.WaitGroup
121+
listener net.Listener
122+
server *http.Server
123+
tlsListener net.Listener
124+
tlsServer *http.Server
122125
}
123126

124127
// NewHttpTransport returns a new HttpTransport instance based on the provided HttpTransportOptions.
@@ -151,6 +154,11 @@ func NewHttpTransport(opts HttpTransportOptions) *HttpTransport {
151154
opts.Logger = slog.Default()
152155
}
153156

157+
// override the Scheme that is set to ensure it is https
158+
if opts.TLSConfig != nil {
159+
opts.Scheme = "https"
160+
}
161+
154162
return &HttpTransport{
155163
opts: opts,
156164
}
@@ -177,36 +185,61 @@ func (t *HttpTransport) ListenAndServe(ctx context.Context, address string) erro
177185
return fmt.Errorf("while starting HTTP listener: %w", err)
178186
}
179187

180-
t.server = &http.Server{
181-
Handler: mux,
188+
if t.opts.TLSConfig != nil {
189+
t.tlsListener = tls.NewListener(t.listener, t.opts.TLSConfig)
190+
t.tlsServer = &http.Server{
191+
Handler: mux,
192+
}
193+
} else {
194+
t.server = &http.Server{
195+
Handler: mux,
196+
}
182197
}
183198

184199
t.wg.Add(1)
185200
go func() {
186201
t.opts.Logger.Info(fmt.Sprintf("Listening on %s ....", address))
187-
if err := t.server.Serve(t.listener); err != nil {
188-
if !errors.Is(err, http.ErrServerClosed) {
189-
t.opts.Logger.Error("while starting HTTP server", "err", err)
202+
if t.tlsServer != nil {
203+
if err := t.tlsServer.Serve(t.tlsListener); err != nil {
204+
if !errors.Is(err, http.ErrServerClosed) {
205+
t.opts.Logger.Error("while starting HTTPs server", "err", err)
206+
}
207+
}
208+
} else {
209+
if err := t.server.Serve(t.listener); err != nil {
210+
if !errors.Is(err, http.ErrServerClosed) {
211+
t.opts.Logger.Error("while starting HTTP server", "err", err)
212+
}
190213
}
191214
}
192215
t.wg.Done()
193216
}()
194217

195218
// Ensure server is accepting connections before returning
196-
return waitForConnect(ctx, t.listener.Addr().String(), nil)
219+
return waitForConnect(ctx, t.listener.Addr().String(), t.opts.TLSConfig)
197220
}
198221

199222
// Shutdown shuts down the server started when calling ListenAndServe()
200223
func (t *HttpTransport) Shutdown(ctx context.Context) error {
201-
if err := t.server.Shutdown(ctx); err != nil {
202-
return err
224+
if t.tlsServer != nil {
225+
if err := t.tlsServer.Shutdown(ctx); err != nil {
226+
return err
227+
}
228+
} else {
229+
if err := t.server.Shutdown(ctx); err != nil {
230+
return err
231+
}
203232
}
233+
204234
t.wg.Wait()
205235
return nil
206236
}
207237

208238
// ListenAddress returns the address the server is listening on after calling ListenAndServe().
209239
func (t *HttpTransport) ListenAddress() string {
240+
if t.tlsListener != nil {
241+
return t.tlsListener.Addr().String()
242+
}
210243
return t.listener.Addr().String()
211244
}
212245

@@ -469,7 +502,16 @@ func waitForConnect(ctx context.Context, address string, cfg *tls.Config) error
469502
errs = append(errs, ctx.Err().Error())
470503
return errors.New(strings.Join(errs, "\n"))
471504
}
472-
time.Sleep(time.Millisecond * 100)
505+
wait(time.Millisecond * 100)
473506
continue
474507
}
475508
}
509+
510+
func wait(duration time.Duration) {
511+
stopCh := make(chan struct{}, 1)
512+
timer := time.AfterFunc(duration, func() {
513+
stopCh <- struct{}{}
514+
})
515+
<-stopCh
516+
timer.Stop()
517+
}

0 commit comments

Comments
 (0)