Skip to content

Commit

Permalink
v0.5-rc2: added reload cooldown, fixed auto reload, updated API
Browse files Browse the repository at this point in the history
  • Loading branch information
yusing committed Sep 16, 2024
1 parent 996b418 commit c0ebd9f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 45 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ repush:
git reset --soft HEAD^
git add -A
git commit -m "repush"
git push gitlab dev --force
git push gitlab dev --force

rapid-crash:
sudo docker run --restart=always --name test_crash debian:bookworm-slim /bin/cat &&\
sleep 3 &&\
sudo docker rm -f test_crash
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ A [lightweight](docs/benchmark_result.md), easy-to-use, and efficient reverse pr
- Auto configuration for docker contaienrs
- Auto hot-reload on container state / config file changes
- Support HTTP(s), TCP and UDP
- Support HTTP(s) round robin load balancing
- Web UI for configuration and monitoring (See [screenshots](screeenshots))
- Written in **[Go](https://go.dev)**

Expand Down Expand Up @@ -110,14 +109,16 @@ See [providers.example.yml](providers.example.yml) for examples

## Build it yourself

1. Install / Upgrade [go (>=1.22)](https://go.dev/doc/install) and `make` if not already
1. Clone the repository `git clone https://github.com/yusing/go-proxy --depth=1`

2. Clear cache if you have built this before (go < 1.22) with `go clean -cache`
2. Install / Upgrade [go (>=1.22)](https://go.dev/doc/install) and `make` if not already

3. get dependencies with `make get`
3. Clear cache if you have built this before (go < 1.22) with `go clean -cache`

4. build binary with `make build`
4. get dependencies with `make get`

5. start your container with `make up` (docker) or `bin/go-proxy` (binary)
5. build binary with `make build`

6. start your container with `make up` (docker) or `bin/go-proxy` (binary)

[🔼Back to top](#table-of-content)
54 changes: 42 additions & 12 deletions src/proxy/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"path"
"time"

"github.com/sirupsen/logrus"
E "github.com/yusing/go-proxy/error"
Expand All @@ -30,6 +31,8 @@ type Provider struct {
watcherCancel context.CancelFunc

l *logrus.Entry

cooldownCh chan struct{}
}

type ProviderType string
Expand All @@ -45,9 +48,10 @@ func newProvider(name string, t ProviderType) *Provider {
t: t,
routes: R.NewRoutes(),
reloadReqCh: make(chan struct{}, 1),
cooldownCh: make(chan struct{}, 1),
}
p.l = logrus.WithField("provider", p)

go p.processReloadRequests()
return p
}
func NewFileProvider(filename string) *Provider {
Expand Down Expand Up @@ -100,7 +104,8 @@ func (p *Provider) StartAllRoutes() E.NestedError {
nStarted++
}
})
p.l.Infof("%d routes started, %d failed", nStarted, nFailed)

p.l.Debugf("%d routes started, %d failed", nStarted, nFailed)
return errors.Build()
}

Expand All @@ -120,16 +125,17 @@ func (p *Provider) StopAllRoutes() E.NestedError {
nStopped++
}
})
p.l.Infof("%d routes stopped, %d failed", nStopped, nFailed)
p.l.Debugf("%d routes stopped, %d failed", nStopped, nFailed)
return errors.Build()
}

func (p *Provider) ReloadRoutes() {
defer p.l.Info("routes reloaded")

p.StopAllRoutes()
p.loadRoutes()
p.StartAllRoutes()
select {
case p.reloadReqCh <- struct{}{}:
// Successfully sent reload request
default:
// Reload request already in progress, ignore this request
}
}

func (p *Provider) GetCurrentRoutes() *R.Routes {
Expand All @@ -142,15 +148,14 @@ func (p *Provider) watchEvents() {

for {
select {
case <-p.reloadReqCh: // block until last reload is done
p.ReloadRoutes()
continue // ignore events once after reload
case <-p.watcherCtx.Done():
return
case event, ok := <-events:
if !ok {
return
}
l.Info(event)
p.reloadReqCh <- struct{}{}
p.ReloadRoutes()
case err, ok := <-errs:
if !ok {
return
Expand All @@ -163,6 +168,29 @@ func (p *Provider) watchEvents() {
}
}

func (p *Provider) processReloadRequests() {
for range p.reloadReqCh {
// prevent busy loop caused by a container
// repeating crashing and restarting
select {
case p.cooldownCh <- struct{}{}:
p.l.Info("Starting to reload routes")

p.StopAllRoutes()
p.loadRoutes()
p.StartAllRoutes()

p.l.Info("Routes reloaded")

go func() {
time.Sleep(reloadCooldown)
<-p.cooldownCh
}()
default:
}
}
}

func (p *Provider) loadRoutes() E.NestedError {
entries, err := p.GetProxyEntries()

Expand All @@ -183,3 +211,5 @@ func (p *Provider) loadRoutes() E.NestedError {
})
return errors.Build()
}

const reloadCooldown = 300 * time.Millisecond
9 changes: 4 additions & 5 deletions src/route/http_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (

type (
HTTPRoute struct {
Alias PT.Alias `json:"alias"`

TargetURL URL
PathPatterns PT.PathPatterns
Alias PT.Alias `json:"alias"`
TargetURL *URL `json:"target_url"`
PathPatterns PT.PathPatterns `json:"path_patterns"`

mux *http.ServeMux
handler *P.ReverseProxy
Expand Down Expand Up @@ -53,7 +52,7 @@ func NewHTTPRoute(entry *P.Entry) (*HTTPRoute, E.NestedError) {
if !ok {
r = &HTTPRoute{
Alias: entry.Alias,
TargetURL: URL(*entry.URL),
TargetURL: (*URL)(entry.URL),
PathPatterns: entry.PathPatterns,
handler: rp,
}
Expand Down
3 changes: 2 additions & 1 deletion src/watcher/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"path"

"github.com/yusing/go-proxy/common"
E "github.com/yusing/go-proxy/error"
)

Expand All @@ -22,4 +23,4 @@ func (f *fileWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.Nested
return fwHelper.Add(ctx, f)
}

var fwHelper = newFileWatcherHelper()
var fwHelper = newFileWatcherHelper(common.ConfigBasePath)
33 changes: 14 additions & 19 deletions src/watcher/file_watcher_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/yusing/go-proxy/common"
E "github.com/yusing/go-proxy/error"
)

Expand All @@ -26,14 +25,12 @@ type fileWatcherStream struct {
errCh chan E.NestedError
}

func newFileWatcherHelper() *fileWatcherHelper {
func newFileWatcherHelper(dirPath string) *fileWatcherHelper {
w, err := fsnotify.NewWatcher()
if err != nil {
logrus.Panicf("unable to create fs watcher: %s", err)
}
// watch config path for all changes
err = w.Add(common.ConfigBasePath)
if err != nil {
if err = w.Add(dirPath); err != nil {
logrus.Panicf("unable to create fs watcher: %s", err)
}
helper := &fileWatcherHelper{
Expand All @@ -60,26 +57,24 @@ func (h *fileWatcherHelper) Add(ctx context.Context, w *fileWatcher) (<-chan Eve
errCh: make(chan E.NestedError),
}
go func() {
select {
case <-ctx.Done():
h.Remove(w)
return
case <-s.stopped:
return
for {
select {
case <-ctx.Done():
s.stopped <- struct{}{}
case <-s.stopped:
h.mu.Lock()
defer h.mu.Unlock()
close(s.eventCh)
close(s.errCh)
delete(h.m, w.filename)
return
}
}
}()
h.m[w.filename] = s
return s.eventCh, s.errCh
}

func (h *fileWatcherHelper) Remove(w *fileWatcher) {
h.mu.Lock()
defer h.mu.Unlock()

h.m[w.filename].stopped <- struct{}{}
delete(h.m, w.filename)
}

func (h *fileWatcherHelper) start() {
defer h.wg.Done()

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.0-rc1
0.5.0-rc2

0 comments on commit c0ebd9f

Please sign in to comment.