Skip to content

Commit

Permalink
feat: add configurator implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fangwentong committed Apr 28, 2024
1 parent 2866189 commit a3e2bc2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 64 deletions.
40 changes: 6 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,23 @@ package main

import (
"github.com/fangwentong/gogctuner"
"sync/atomic"
)

func main() {
configurator := newConfigurator()
configurator := gogctuner.NewGcConfigurator()

// Integrate with your dynamic config implementation here:
// conf := readFromYourConfigCenter("some_config_key")
// configurator.Set(conf)
// registerConfigUpdateCallback("some_config_key", func(conf *gogctuner.Config) {
// configurator.Set(conf)
// })
conf := readFromYourConfigCenter("some_config_key")
configurator.SetConfig(conf)
registerConfigUpdateCallback("some_config_key", func(conf gogctuner.Config) {
configurator.SetConfig(conf)
})

gogctuner.EnableGCTuner(
gogctuner.WithConfigurator(configurator),
)
}

func newConfigurator() *exampleDynamicConfigurator {
return &exampleDynamicConfigurator{
updateCh: make(chan interface{}, 1),
}
}

type exampleDynamicConfigurator struct {
conf atomic.Value
updateCh chan interface{}
}

func (e *exampleDynamicConfigurator) GetConfig() (gogctuner.Config, error) {
val, _ := e.conf.Load().(gogctuner.Config)
return val, nil
}

func (e *exampleDynamicConfigurator) Updates() <-chan interface{} {
return e.updateCh
}

func (e *exampleDynamicConfigurator) Set(conf gogctuner.Config) {
e.conf.Store(conf)
select {
case e.updateCh <- struct{}{}: // Notify the config change
default:
}
}
```

### Reference
Expand Down
37 changes: 37 additions & 0 deletions configurator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gogctuner

import (
"log"
"sync/atomic"
)

func NewGcConfigurator() *gcConfigurator {
return &gcConfigurator{
configUpdateCh: make(chan interface{}, 1),
}
}

type gcConfigurator struct {
config atomic.Value
configUpdateCh chan interface{}
}

func (g *gcConfigurator) GetConfig() (Config, error) {
c, _ := g.config.Load().(Config)
return c, nil
}

func (g *gcConfigurator) Updates() <-chan interface{} {
return g.configUpdateCh
}

func (g *gcConfigurator) SetConfig(value Config) {
g.config.Store(value)

select {
case g.configUpdateCh <- struct{}{}:
log.Printf("gctuner: gc config update event dispatched, config: %v", value)
default:
log.Printf("gctuner: gc config update event is dropped due to previous task not done, config: %v", value)
}
}
31 changes: 1 addition & 30 deletions gc_tunner_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBenchGcTuner(t *testing.T) {
enableProfiler := true
defaultRounds := doBench("default", t, enableProfiler)
// enable adaptive gc
configurator := newConfigSupplier()
configurator := NewGcConfigurator()
configurator.SetConfig(Config{MaxRAMPercentage: 10})

_ = EnableGCTuner(WithConfigurator(configurator))
Expand Down Expand Up @@ -110,32 +110,3 @@ func gcInfoPrinter(f *ref) {
log.Printf("gc triggered at %v, %d GCs, pause %v", stats.LastGC, stats.NumGC, stats.Pause[0])
runtime.SetFinalizer(f, gcInfoPrinter)
}

func newConfigSupplier() *configSupplier {
supplier := &configSupplier{
ch: make(chan interface{}),
}
return supplier
}

type configSupplier struct {
config atomic.Value
ch chan interface{}
}

func (c *configSupplier) Updates() <-chan interface{} {
return c.ch
}

func (c *configSupplier) GetConfig() (Config, error) {
config, _ := c.config.Load().(Config)
return config, nil
}

func (c *configSupplier) SetConfig(config Config) {
c.config.Store(config)
select {
case c.ch <- struct{}{}: // notify the config change
default:
}
}

0 comments on commit a3e2bc2

Please sign in to comment.