Skip to content

Commit

Permalink
Merge pull request #116 from rekby/devel
Browse files Browse the repository at this point in the history
multigo test and compile
  • Loading branch information
rekby authored Mar 5, 2020
2 parents 5af1965 + f070b2c commit bfd70d0
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 20 deletions.
15 changes: 14 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ addons:

go:
- "1.10"
- "1.14"

env:
global:
- COVERALLS_PARALLEL=true

services:
- docker
Expand All @@ -29,8 +34,10 @@ before_deploy:
- BUILD_TIME=$(TZ=UTC date --rfc-3339=seconds)
- go get github.com/mitchellh/gox
- mkdir -p output
- GO_VERSION=$(go version)
- echo ${GO_VERSION}
- OS_ARCH_BUILDS="darwin/amd64 linux/386 linux/amd64 linux/arm freebsd/386 freebsd/amd64 freebsd/arm windows/386 windows/amd64"
- GO111MODULE=on CGO_ENABLED=0 gox --mod=vendor -osarch "$OS_ARCH_BUILDS" --ldflags "-X \"main.VERSION=$TRAVIS_TAG+build-$TRAVIS_BUILD_NUMBER, Build time $BUILD_TIME, commit $TRAVIS_COMMIT\"" --output="output/lets-proxy_{{.OS}}_{{.Arch}}" -verbose --rebuild ./cmd/
- GO111MODULE=on CGO_ENABLED=0 gox --mod=vendor -osarch "$OS_ARCH_BUILDS" --ldflags "-X \"main.VERSION=$TRAVIS_TAG+build-$TRAVIS_BUILD_NUMBER, Build time $BUILD_TIME, commit $TRAVIS_COMMIT, $GO_VERSION\"" --output="output/lets-proxy_{{.OS}}_{{.Arch}}" -verbose --rebuild ./cmd/
- bash tests/make_archives.sh

deploy:
Expand All @@ -42,3 +49,9 @@ deploy:
api_key: $GITHUB_TOKEN
file_glob: true
file: output/*

notifications:
webhooks:
urls:
- https://coveralls.io/webhook?repo_token=$COVERALLS_TOKEN
if: env(tags) IS blank
7 changes: 7 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func startMetrics(ctx context.Context, r prometheus.Gatherer, config config.Conf
return nil
}

//nolint:funlen
func startProgram(config *configType) {
logger := initLogger(config.Log)
ctx := zc.WithLogger(context.Background(), logger)
Expand Down Expand Up @@ -148,6 +149,12 @@ func startProgram(config *configType) {
err = config.Proxy.Apply(ctx, p)
log.InfoFatal(logger, err, "Apply proxy config")

go func() {
<-ctx.Done()
err := p.Close()
log.DebugError(logger, err, "Stop proxy")
}()

err = p.Start()
var effectiveError = err
if effectiveError == http.ErrServerClosed {
Expand Down
41 changes: 39 additions & 2 deletions internal/acme_client_manager/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync"
"time"

"golang.org/x/xerrors"

"go.uber.org/zap"

"github.com/rekby/lets-proxy2/internal/log"
Expand All @@ -23,23 +25,30 @@ import (
const rsaKeyLength = 2048
const renewAccountInterval = time.Hour * 24

var errClosed = xerrors.Errorf("acmeManager already closed")

type AcmeManager struct {
IgnoreCacheLoad bool
DirectoryURL string
AgreeFunction func(tosurl string) bool
RenewAccountInterval time.Duration

ctx context.Context
cache cache.Bytes
ctx context.Context
ctxCancel context.CancelFunc
ctxAutorenewCompleted context.Context
cache cache.Bytes

mu sync.Mutex
client *acme.Client
account *acme.Account
closed bool
}

func New(ctx context.Context, cache cache.Bytes) *AcmeManager {
ctx, ctxCancel := context.WithCancel(ctx)
return &AcmeManager{
ctx: ctx,
ctxCancel: ctxCancel,
cache: cache,
AgreeFunction: acme.AcceptTOS,
RenewAccountInterval: renewAccountInterval,
Expand All @@ -51,6 +60,23 @@ type acmeManagerState struct {
AcmeAccount *acme.Account
}

func (m *AcmeManager) Close() error {
m.mu.Lock()
alreadyClosed := m.closed
ctxAutorenewCompleted := m.ctxAutorenewCompleted
m.closed = true
m.mu.Unlock()

if alreadyClosed {
return xerrors.Errorf("close: %w", errClosed)
}
m.ctxCancel()
if ctxAutorenewCompleted != nil {
<-ctxAutorenewCompleted.Done()
}
return nil
}

func (m *AcmeManager) GetClient(ctx context.Context) (*acme.Client, error) {
if ctx.Err() != nil {
return nil, errors.New("acme manager context closed")
Expand All @@ -59,6 +85,10 @@ func (m *AcmeManager) GetClient(ctx context.Context) (*acme.Client, error) {
m.mu.Lock()
defer m.mu.Unlock()

if m.closed {
return nil, xerrors.Errorf("GetClient: %w", errClosed)
}

if m.client != nil {
return m.client, nil
}
Expand Down Expand Up @@ -98,6 +128,13 @@ func (m *AcmeManager) GetClient(ctx context.Context) (*acme.Client, error) {
}

func (m *AcmeManager) accountRenew() {
ctx, ctxCancel := context.WithCancel(m.ctx)
defer ctxCancel()

m.mu.Lock()
m.ctxAutorenewCompleted = ctx
m.mu.Unlock()

ticker := time.NewTicker(m.RenewAccountInterval)
ctxDone := m.ctx.Done()

Expand Down
1 change: 1 addition & 0 deletions internal/acme_client_manager/client_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestClientManagerGetFromCache(t *testing.T) {
var err error

manager := New(ctx, c)
defer th.Close(manager)

state := acmeManagerState{
AcmeAccount: &acme.Account{},
Expand Down
28 changes: 13 additions & 15 deletions internal/proxy/http-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type HTTPProxy struct {
Director Director // modify requests to backend.
HTTPTransport http.RoundTripper

ctx context.Context
logger *zap.Logger
listener net.Listener
httpReverseProxy httputil.ReverseProxy
IdleTimeout time.Duration
httpServer http.Server
}

func NewHTTPProxy(ctx context.Context, listener net.Listener) *HTTPProxy {
Expand All @@ -44,13 +45,17 @@ func NewHTTPProxy(ctx context.Context, listener net.Listener) *HTTPProxy {
Director: NewDirectorSameIP(defaultHTTPPort),
GetContext: getContext,
listener: listener,
ctx: ctx,
logger: zc.L(ctx),
httpServer: http.Server{},
}
res.httpReverseProxy.Director = res.director

return res
}

func (p *HTTPProxy) Close() error {
return p.httpServer.Close()
}

// Start - finish initialization of proxy and start handling request.
// It is sync method, always return with non nil error: if handle stopped by context or if error on start handling.
// Any public fields must not change after Start called
Expand All @@ -65,18 +70,11 @@ func (p *HTTPProxy) Start() error {
p.httpReverseProxy.ServeHTTP(writer, request)
}
})
httpServer := http.Server{}
httpServer.Handler = mux
httpServer.IdleTimeout = p.IdleTimeout

go func() {
<-p.ctx.Done()
err := httpServer.Close()
log.DebugErrorCtx(p.ctx, err, "Http builtin reverse proxy stop because context canceled")
}()

zc.L(p.ctx).Info("Http builtin reverse proxy start")
err := httpServer.Serve(p.listener)
p.httpServer.Handler = mux
p.httpServer.IdleTimeout = p.IdleTimeout

p.logger.Info("Http builtin reverse proxy start")
err := p.httpServer.Serve(p.listener)
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/proxy/http-proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestNewHttpProxy(t *testing.T) {

td.FailureIsFatal()
listener, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)})
defer th.Close(listener)
td.CmpNoError(err)

prefix := "http://" + listener.Addr().String()
Expand Down Expand Up @@ -114,6 +115,8 @@ func TestNewHttpProxy(t *testing.T) {
})

proxy := NewHTTPProxy(ctx, listener)
defer th.Close(proxy)

proxy.GetContext = proxyTest.GetContext
proxy.Director = directorMock
proxy.HandleHTTPValidation = proxyTest.HandleHTTPValidation
Expand Down
5 changes: 5 additions & 0 deletions internal/th/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package th

import (
"context"
"io"

"go.uber.org/zap/zaptest"

Expand Down Expand Up @@ -30,3 +31,7 @@ func NoLog(ctx context.Context) context.Context {
func Logger(t zaptest.TestingT) *zap.Logger {
return zaptest.NewLogger(t, zaptest.WrapOptions(zap.Development()))
}

func Close(closer io.Closer) {
_ = closer.Close()
}
4 changes: 2 additions & 2 deletions tests/make_archives.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ for FILE in `ls`; do
if expr match "$FILE" '^lets-proxy_windows' > /dev/null; then
mv "$FILE" lets-proxy.exe
unix2dos -n config_default.toml_ config_default.toml
zip "${FILE%.exe}.zip" lets-proxy.exe README.txt config_default.toml
zip "${FILE%.exe}-go${TRAVIS_GO_VERSION}.zip" lets-proxy.exe README.txt config_default.toml
rm config_default.toml
rm lets-proxy.exe
else
mv "$FILE" lets-proxy
cp config_default.toml_ config_default.toml
tar -zcvf "${FILE%.exe}.tar.gz" lets-proxy README.md config_default.toml
tar -zcvf "${FILE%.exe}-go${TRAVIS_GO_VERSION}.tar.gz" lets-proxy README.md config_default.toml
rm config_default.toml
rm lets-proxy
fi
Expand Down

0 comments on commit bfd70d0

Please sign in to comment.