Skip to content

Commit fb4cc9f

Browse files
authored
Merge pull request #166 from SenseUnit/proper_shutdown
Tidy shutdown
2 parents 0a4a46b + 4d8445c commit fb4cc9f

File tree

12 files changed

+86
-31
lines changed

12 files changed

+86
-31
lines changed

auth/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package auth
33
import (
44
"context"
55
"errors"
6+
"io"
67
"net/http"
78
"net/url"
89
"strings"
@@ -12,7 +13,7 @@ import (
1213

1314
type Auth interface {
1415
Validate(ctx context.Context, wr http.ResponseWriter, req *http.Request) (string, bool)
15-
Stop()
16+
io.Closer
1617
}
1718

1819
func NewAuth(paramstr string, logger *clog.CondLogger) (Auth, error) {

auth/basic.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ func (auth *BasicAuth) Validate(ctx context.Context, wr http.ResponseWriter, req
173173
return requireBasicAuth(ctx, wr, req, auth.hiddenDomain, auth.next)
174174
}
175175

176-
func (auth *BasicAuth) Stop() {
176+
func (auth *BasicAuth) Close() error {
177+
var err error
177178
auth.stopOnce.Do(func() {
178179
if auth.next != nil {
179-
auth.next.Stop()
180+
err = auth.next.Close()
180181
}
181182
close(auth.stopChan)
182183
})
184+
return err
183185
}

auth/cert.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
clog "github.com/SenseUnit/dumbproxy/log"
19+
"github.com/hashicorp/go-multierror"
1920

2021
us "github.com/Snawoot/uniqueslice"
2122
)
@@ -108,16 +109,22 @@ func (auth *CertAuth) Validate(ctx context.Context, wr http.ResponseWriter, req
108109
), true
109110
}
110111

111-
func (auth *CertAuth) Stop() {
112+
func (auth *CertAuth) Close() error {
113+
var err error
112114
auth.stopOnce.Do(func() {
113115
if auth.next != nil {
114-
auth.next.Stop()
116+
if closeErr := auth.next.Close(); closeErr != nil {
117+
err = multierror.Append(err, closeErr)
118+
}
115119
}
116120
if auth.reject != nil {
117-
auth.reject.Stop()
121+
if closeErr := auth.reject.Close(); closeErr != nil {
122+
err = multierror.Append(err, closeErr)
123+
}
118124
}
119125
close(auth.stopChan)
120126
})
127+
return err
121128
}
122129

123130
func (auth *CertAuth) reload() error {

auth/hmac.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ func (auth *HMACAuth) Validate(ctx context.Context, wr http.ResponseWriter, req
143143
return requireBasicAuth(ctx, wr, req, auth.hiddenDomain, auth.next)
144144
}
145145

146-
func (auth *HMACAuth) Stop() {
146+
func (auth *HMACAuth) Close() error {
147+
var err error
147148
auth.stopOnce.Do(func() {
148149
if auth.next != nil {
149-
auth.next.Stop()
150+
err = auth.next.Close()
150151
}
151152
})
153+
return err
152154
}
153155

154156
func CalculateHMACSignature(secret []byte, username string, expire int64) []byte {

auth/noauth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ func (_ NoAuth) Validate(_ context.Context, _ http.ResponseWriter, _ *http.Reque
1111
return "", true
1212
}
1313

14-
func (_ NoAuth) Stop() {}
14+
func (_ NoAuth) Close() error {
15+
return nil
16+
}

auth/redis.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
clog "github.com/SenseUnit/dumbproxy/log"
16+
"github.com/hashicorp/go-multierror"
1617

1718
"github.com/redis/go-redis/v9"
1819
)
@@ -134,11 +135,17 @@ func (auth *RedisAuth) Validate(ctx context.Context, wr http.ResponseWriter, req
134135
return requireBasicAuth(ctx, wr, req, auth.hiddenDomain, auth.next)
135136
}
136137

137-
func (auth *RedisAuth) Stop() {
138+
func (auth *RedisAuth) Close() error {
139+
var err error
138140
auth.stopOnce.Do(func() {
139141
if auth.next != nil {
140-
auth.next.Stop()
142+
if closeErr := auth.next.Close(); closeErr != nil {
143+
err = multierror.Append(err, closeErr)
144+
}
145+
}
146+
if closeErr := auth.r.Close(); closeErr != nil {
147+
err = multierror.Append(err, closeErr)
141148
}
142-
auth.r.Close()
143149
})
150+
return err
144151
}

auth/rejecthttp.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ func (_ *RejectHTTPAuth) Valid(_, _, _ string) bool {
5656
return false
5757
}
5858

59-
func (_ *RejectHTTPAuth) Stop() {}
59+
func (_ *RejectHTTPAuth) Close() error {
60+
return nil
61+
}

auth/rejectstatic.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,6 @@ func (_ *StaticRejectAuth) Valid(_, _, _ string) bool {
105105
return false
106106
}
107107

108-
func (_ *StaticRejectAuth) Stop() {}
108+
func (_ *StaticRejectAuth) Close() error {
109+
return nil
110+
}

certcache/cryptobox.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/cipher"
66
cryptorand "crypto/rand"
77
"errors"
8+
"io"
89

910
"golang.org/x/crypto/acme/autocert"
1011
"golang.org/x/crypto/chacha20poly1305"
@@ -64,3 +65,10 @@ func (c *EncryptedCache) Put(ctx context.Context, key string, data []byte) error
6465
func (c *EncryptedCache) Delete(ctx context.Context, key string) error {
6566
return c.next.Delete(ctx, key)
6667
}
68+
69+
func (c *EncryptedCache) Close() error {
70+
if cacheCloser, ok := c.next.(io.Closer); ok {
71+
return cacheCloser.Close()
72+
}
73+
return nil
74+
}

certcache/local.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package certcache
22

33
import (
44
"context"
5+
"io"
56
"sync"
67
"time"
78

@@ -16,10 +17,9 @@ type certCacheValue struct {
1617
}
1718

1819
type LocalCertCache struct {
19-
cache *ttlcache.Cache[certCacheKey, certCacheValue]
20-
next autocert.Cache
21-
startOnce sync.Once
22-
stopOnce sync.Once
20+
cache *ttlcache.Cache[certCacheKey, certCacheValue]
21+
next autocert.Cache
22+
stopOnce sync.Once
2323
}
2424

2525
func NewLocalCertCache(next autocert.Cache, ttl, timeout time.Duration) *LocalCertCache {
@@ -41,6 +41,7 @@ func NewLocalCertCache(next autocert.Cache, ttl, timeout time.Duration) *LocalCe
4141
nil),
4242
),
4343
)
44+
go cache.Start()
4445
return &LocalCertCache{
4546
cache: cache,
4647
next: next,
@@ -62,14 +63,15 @@ func (cc *LocalCertCache) Delete(ctx context.Context, key string) error {
6263
return cc.next.Delete(ctx, key)
6364
}
6465

65-
func (cc *LocalCertCache) Start() {
66-
cc.startOnce.Do(func() {
67-
go cc.cache.Start()
66+
func (cc *LocalCertCache) Close() error {
67+
var err error
68+
cc.stopOnce.Do(func() {
69+
cc.cache.Stop()
70+
if cacheCloser, ok := cc.next.(io.Closer); ok {
71+
err = cacheCloser.Close()
72+
}
6873
})
69-
}
70-
71-
func (cc *LocalCertCache) Stop() {
72-
cc.stopOnce.Do(cc.cache.Stop)
74+
return err
7375
}
7476

7577
var _ autocert.Cache = new(LocalCertCache)

0 commit comments

Comments
 (0)