Skip to content

Commit

Permalink
make disable/enable of server possible and abandon marks if server is…
Browse files Browse the repository at this point in the history
… full
  • Loading branch information
forrestjgq committed Apr 29, 2022
1 parent fe6128c commit e2b9b90
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
13 changes: 11 additions & 2 deletions cmd/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/forrestjgq/gomark"
"github.com/forrestjgq/gomark/internal/gm"
"math/rand"
"sync"
"time"
Expand All @@ -16,7 +17,7 @@ func main() {

// adder
go func() {
adder := gomark.NewAdder("test")
adder := gomark.NewAdder("test_adder")
for i := 0; i < 10000; i++ {
adder.Mark(rand.Int31n(100))
time.Sleep(100 * time.Millisecond)
Expand All @@ -27,7 +28,7 @@ func main() {

// latency recorder
go func() {
lr := gomark.NewLatencyRecorder("test")
lr := gomark.NewLatencyRecorder("test_latency")
for i := 0; i < 10000; i++ {
lr.Mark(rand.Int31n(100))
time.Sleep(100 * time.Millisecond)
Expand All @@ -36,6 +37,14 @@ func main() {
wg.Done()
}()

go func() {
time.Sleep(time.Second * 10)
gm.DisableServer()
time.Sleep(time.Second * 10)
gm.EnableServer()

}()

// wait and see monitor on http://127.0.0.1:7777/vars
wg.Wait()
}
20 changes: 18 additions & 2 deletions internal/gm/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"
"unicode"

"github.com/forrestjgq/gomark/internal/util"
"github.com/forrestjgq/glog"
"github.com/forrestjgq/gomark/internal/util"
)

const (
Expand All @@ -28,6 +28,7 @@ type sampler interface {
type disposer func()

type server struct {
disabled bool // set to true to disable all marks
seq Identity
smp Identity
stubc chan stub
Expand All @@ -48,6 +49,7 @@ var srv *server

func init() {
srv = &server{
disabled: false,
stubc: make(chan stub, sizeOfQ),
callc: make(chan func()),
all: make(map[Identity]Variable),
Expand Down Expand Up @@ -325,8 +327,22 @@ func Dump(dumper Dumper, option *DumpOption) (int, error) {
return ret, err
}

func EnableServer() {
srv.disabled = false
}

func DisableServer() {
srv.disabled = true
}

func ServerEnabled() bool {
return srv.disabled
}

func PushStub(s stub) {
srv.stubc <- s
if !srv.disabled && len(srv.stubc) < sizeOfQ-1 {
srv.stubc <- s
}
}

func AddSampler(s sampler) disposer {
Expand Down

0 comments on commit e2b9b90

Please sign in to comment.