Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a throttling function that by default is disable, and can be cust… #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions panics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

"strings"

"github.com/tokopedia/panics/svc/throttle"

"github.com/eapache/go-resiliency/breaker"
"github.com/julienschmidt/httprouter"
"github.com/nsqio/go-nsq"
Expand All @@ -34,6 +36,7 @@ var (
capturedBadDeployment bool
customMessage string

checkThrottle bool
// circuitbreaker
cb *breaker.Breaker
)
Expand All @@ -52,8 +55,13 @@ type Options struct {
Tags Tags
CustomMessage string
DontLetMeDie bool

MaxSendMessage int // maximum message being send to channel
RetrySendMessageAfter int // time in second
FlagThrottle bool
}

//SetOptions setting up general function
func SetOptions(o *Options) {
filepath = o.Filepath
slackWebhookURL = o.SlackWebhookURL
Expand All @@ -73,13 +81,21 @@ func SetOptions(o *Options) {
if o.DontLetMeDie {
cb = nil
}

//add new flag function
if o.FlagThrottle {
checkThrottle = true
throttle.Setup(o.MaxSendMessage, o.RetrySendMessageAfter)
}

CaptureBadDeployment()
}

func init() {
env = os.Getenv("TKPENV")
// circuitbreaker to let apps died when got too many panics
cb = breaker.New(3, 2, time.Minute*1)

}

// CaptureHandler handle panic on http handler.
Expand Down Expand Up @@ -232,6 +248,13 @@ func recovery(r interface{}) error {
}

func publishError(errs error, reqBody []byte, withStackTrace bool) {
//do nothing if system exeding threshold that was being put on in
if checkThrottle {
if !throttle.AllowedSend() {

}
}

var text string
var snip string
var buffer bytes.Buffer
Expand Down Expand Up @@ -274,6 +297,12 @@ func publishError(errs error, reqBody []byte, withStackTrace bool) {
}

func postToSlack(text, snip string) {
//do nothing if system exeding threshold that was being put on in
if checkThrottle {
if !throttle.AllowedSend() {
return
}
}
payload := map[string]interface{}{
"text": text,
//Enable slack to parse mention @<someone>
Expand Down
11 changes: 11 additions & 0 deletions svc/throttle/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package throttle

//AllowedSend verifying send message
func AllowedSend() bool {
if counter > maxSendMessage {
return false
}

counter++
return true
}
24 changes: 24 additions & 0 deletions svc/throttle/throttle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package throttle

import (
"time"
)

//Setup do initial setup
func Setup(max, retryAfter int) {
maxSendMessage = defaultMaxSendMessage
if max != 0 {
maxSendMessage = max
}

go func() {
waitTime := defaultRetrySendMessage
if retryAfter != 0 {
waitTime = retryAfter
}
for {
time.Sleep(time.Duration(waitTime) * time.Second)
sendMessageCounter = 0
}
}()
}
14 changes: 14 additions & 0 deletions svc/throttle/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package throttle

var (
sendMessageCounter int
maxSendMessage int
counter int
)

type throttle struct{}

const (
defaultRetrySendMessage = 600 // 10 minutes
defaultMaxSendMessage = 10
)