Skip to content

Commit

Permalink
Add rule and middleware feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alinz committed May 20, 2024
1 parent 2e63008 commit 5565cd0
Show file tree
Hide file tree
Showing 14 changed files with 1,059 additions and 65 deletions.
82 changes: 49 additions & 33 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ const (
type Event struct {
Type EventType
Container *Container
Domain string
Path string
Result chan *Container
Endpoint *Endpoint
Result chan struct {
Container *Container
Endpoint *Endpoint
}
}

type ActionRunner struct {
pingerCallback func()
addCallback func(*Container)
updateCallback func(*Container, string, string)
updateCallback func(*Container, *Endpoint)
removeCallback func(*Container)
getCallback func(string, string) *Container
getCallback func(string, string) (*Container, *Endpoint)

events chan *Event
close chan struct{} // using this to make sure pushing to events stops when Close() is called
Expand All @@ -45,31 +47,33 @@ func (ar *ActionRunner) Add(container *Container) {
ar.push(&Event{Type: addEvent, Container: container})
}

func (ar *ActionRunner) Update(container *Container, domain string, path string) {
ar.push(&Event{Type: updateEvent, Container: container, Domain: domain, Path: path})
func (ar *ActionRunner) Update(container *Container, endpoint *Endpoint) {
ar.push(&Event{Type: updateEvent, Container: container, Endpoint: endpoint})
}

func (ar *ActionRunner) Remove(container *Container) {
ar.push(&Event{Type: removeEvent, Container: container})
}

func (ar *ActionRunner) Get(ctx context.Context, domain, path string) *Container {
func (ar *ActionRunner) Get(ctx context.Context, endpoint *Endpoint) (*Container, *Endpoint) {
evt := &Event{
Type: getEvent,
Domain: domain,
Path: path,
Result: make(chan *Container, 1),
Type: getEvent,
Endpoint: endpoint,
Result: make(chan struct {
Container *Container
Endpoint *Endpoint
}, 1),
}

ar.push(evt)

select {
case container := <-evt.Result:
return container
case r := <-evt.Result:
return r.Container, r.Endpoint
case <-ctx.Done():
return nil
return nil, nil
case <-ar.close:
return nil
return nil, nil
}
}

Expand All @@ -85,7 +89,6 @@ func (ar *ActionRunner) push(event *Event) {

func (ar *ActionRunner) Close() {
close(ar.close)
close(ar.events)
}

func WithPingerCallback(callback func()) func(*ActionRunner) {
Expand All @@ -100,7 +103,7 @@ func WithAddCallback(callback func(*Container)) func(*ActionRunner) {
}
}

func WithUpdateCallback(callback func(*Container, string, string)) func(*ActionRunner) {
func WithUpdateCallback(callback func(*Container, *Endpoint)) func(*ActionRunner) {
return func(ar *ActionRunner) {
ar.updateCallback = callback
}
Expand All @@ -112,7 +115,7 @@ func WithRemoveCallback(callback func(*Container)) func(*ActionRunner) {
}
}

func WithGetCallback(callback func(string, string) *Container) func(*ActionRunner) {
func WithGetCallback(callback func(string, string) (*Container, *Endpoint)) func(*ActionRunner) {
return func(ar *ActionRunner) {
ar.getCallback = callback
}
Expand All @@ -133,20 +136,33 @@ func NewActionRunner(bufferSize int, cbs ...ActionCallback) *ActionRunner {
go func() {
defer slog.Debug("ActionRunner: stopped")

for event := range ar.events {
switch event.Type {
case pingerEvent:
ar.pingerCallback()
case addEvent:
ar.addCallback(event.Container)
case updateEvent:
ar.updateCallback(event.Container, event.Domain, event.Path)
case removeEvent:
ar.removeCallback(event.Container)
case getEvent:
event.Result <- ar.getCallback(event.Domain, event.Path)
default:
continue
for {
select {
case <-ar.close:
return
case event, ok := <-ar.events:
if !ok {
return
}

switch event.Type {
case pingerEvent:
ar.pingerCallback()
case addEvent:
ar.addCallback(event.Container)
case updateEvent:
ar.updateCallback(event.Container, event.Endpoint)
case removeEvent:
ar.removeCallback(event.Container)
case getEvent:
container, endpoint := ar.getCallback(event.Endpoint.Domain, event.Endpoint.Path)
event.Result <- struct {
Container *Container
Endpoint *Endpoint
}{container, endpoint}
default:
continue
}
}
}
}()
Expand Down
12 changes: 12 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package baker

import (
"encoding/json"
"net/netip"
)

Expand All @@ -13,12 +14,23 @@ type Container struct {
type Endpoint struct {
Domain string `json:"domain"`
Path string `json:"path"`
Rules []Rule `json:"rules"`
}

type Rule struct {
Type string `json:"type"`
Args json.RawMessage `json:"args"`
}

type Config struct {
Endpoints []Endpoint `json:"endpoints"`
}

type Service struct {
Containers []*Container
Endpoint *Endpoint
}

type Driver interface {
Add(*Container)
Remove(*Container)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module ella.to/baker
go 1.22.0

require (
github.com/alinz/baker.go v1.2.0
github.com/cespare/xxhash/v2 v2.3.0
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.23.0
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/alinz/baker.go v1.2.0 h1:XHdn58jMGLTupj2+qtUJuN3mImDOC/kABk7iRG+JXfk=
github.com/alinz/baker.go v1.2.0/go.mod h1:W7xTX8eE5v0ddkzDvg3tvCNdjZGjWk5s9S0sPIDuo/c=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
1 change: 1 addition & 0 deletions rule/internal/rate/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This package is copied and modified from https://github.com/go-chi/httprate
16 changes: 16 additions & 0 deletions rule/internal/rate/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package rate

import "context"

var incrementKey = &struct{}{}

func WithIncrement(ctx context.Context, value int) context.Context {
return context.WithValue(ctx, incrementKey, value)
}

func getIncrement(ctx context.Context) int {
if value, ok := ctx.Value(incrementKey).(int); ok {
return value
}
return 1
}
Loading

0 comments on commit 5565cd0

Please sign in to comment.