<commit_before>package gentle

import (
	"errors"
	"github.com/afex/hystrix-go/hystrix"
	log15 "gopkg.in/inconshreveable/log15.v2"
	"time"
)

// Rate limiting pattern is used to limit the speed of a series of Handle().
type RateLimitedHandler struct {
	Name    string
	Log     log15.Logger
	handler Handler
	limiter RateLimit
}

func NewRateLimitedHandler(name string, handler Handler, limiter RateLimit) *RateLimitedHandler {
	return &RateLimitedHandler{
		Name:    name,
		Log:     Log.New("mixin", "handler_rate", "name", name),
		handler: handler,
		limiter: limiter,
	}
}

// Handle() is blocked when the limit is exceeded.
func (r *RateLimitedHandler) Handle(msg Message) (Message, error) {
	r.Log.Debug("[Handler] Handle() ...", "msg_in", msg.Id())
	r.limiter.Wait(1, 0)
	msg_out, err := r.handler.Handle(msg)
	if err != nil {
		r.Log.Error("[Handler] Handle() err", "msg_in", msg.Id(), "err", err)
		return nil, err
	}
	r.Log.Debug("[Handler] Handle() ok", "msg_in", msg.Id(), "msg_out", msg_out.Id())
	return msg, nil
}

// RetryHandler takes an Handler. When Handler.Handle() encounters an error,
// RetryHandler back off for some time and then retries.
type RetryHandler struct {
	Name     string
	Log      log15.Logger
	handler  Handler
	backoffs []time.Duration
}

func NewRetryHandler(name string, handler Handler, backoffs []time.Duration) *RetryHandler {
	if len(backoffs) == 0 {
		Log.Warn("NewRetryHandler() len(backoffs) == 0")
	}
	return &RetryHandler{
		Name:     name,
		Log:      Log.New("mixin", "handler_retry", "name", name),
		handler:  handler,
		backoffs: backoffs,
	}
}

func (r *RetryHandler) Handle(msg Message) (Message, error) {
	begin := time.Now()<commit_msg>RatedLimitedHandler Handle() returns msg_out<commit_after>package gentle

import (
	"errors"
	"github.com/afex/hystrix-go/hystrix"
	log15 "gopkg.in/inconshreveable/log15.v2"
	"time"
)

// Rate limiting pattern is used to limit the speed of a series of Handle().
type RateLimitedHandler struct {
	Name    string
	Log     log15.Logger
	handler Handler
	limiter RateLimit
}

func NewRateLimitedHandler(name string, handler Handler, limiter RateLimit) *RateLimitedHandler {
	return &RateLimitedHandler{
		Name:    name,
		Log:     Log.New("mixin", "handler_rate", "name", name),
		handler: handler,
		limiter: limiter,
	}
}

// Handle() is blocked when the limit is exceeded.
func (r *RateLimitedHandler) Handle(msg Message) (Message, error) {
	r.Log.Debug("[Handler] Handle() ...", "msg_in", msg.Id())
	r.limiter.Wait(1, 0)
	msg_out, err := r.handler.Handle(msg)
	if err != nil {
		r.Log.Error("[Handler] Handle() err", "msg_in", msg.Id(), "err", err)
		return nil, err
	}
	r.Log.Debug("[Handler] Handle() ok", "msg_in", msg.Id(), "msg_out", msg_out.Id())
	return msg_out, nil
}

// RetryHandler takes an Handler. When Handler.Handle() encounters an error,
// RetryHandler back off for some time and then retries.
type RetryHandler struct {
	Name     string
	Log      log15.Logger
	handler  Handler
	backoffs []time.Duration
}

func NewRetryHandler(name string, handler Handler, backoffs []time.Duration) *RetryHandler {
	if len(backoffs) == 0 {
		Log.Warn("NewRetryHandler() len(backoffs) == 0")
	}
	return &RetryHandler{
		Name:     name,
		Log:      Log.New("mixin", "handler_retry", "name", name),
		handler:  handler,
		backoffs: backoffs,
	}
}

func (r *RetryHandler) Handle(msg Message) (Message, error) {
	begin := time.Now()
{'fraction_non_alphanumeric': 0.13956204379562043, 'fraction_numerical': 0.0075912408759124085, 'mean_word_length': 4.159638554216867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3779715', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1200, 'n_words': 334}