<commit_before>package dispatch

import (
	"runtime/debug"
	"time"
)

type Mutex chan struct{}

func NewMutex() Mutex {
	l := make(chan struct{}, 1)
	l <- struct{}{}
	return l
}

type Context struct {
	m      chan Mutex
	cancel chan struct{}
}

func NewContext() *Context {
	ctx, _ := NewContextWithCancel()
	return ctx
}

func NewContextWithTimeOut(t time.Duration) *Context {
	ctx, cancel := NewContextWithCancel()
	time.AfterFunc(t, cancel)
	return ctx
}

func NewContextWithCancel() (ctx *Context, cancel func()) {
	m := make(chan Mutex, 1)
	c := make(chan struct{})
	return &Context{m: m, cancel: c}, func() { close(c) }
}

func (ctx *Context) AcquireOrCancel(m Mutex) bool {
	if len(ctx.m) > 0 {
		panic("Context has already locked a Mutex")
	}

	select {
	case <-m:
		ctx.m <- m
		return true
	case <-ctx.cancel:
		return false
	}
}

func (ctx *Context) Release() {
	if len(ctx.m) == 0 {
		panic("Context is not obtaining any Mutex")
	}

	<-ctx.m <- struct{}{}
}

type Handler interface {
	Serve(ctx *Context, m Mutex, r Request) Response
}

type HandlerFunc func(ctx *Context, m Mutex, r Request) Response

func (f HandlerFunc) Serve(ctx *Context, m Mutex, r Request) (rsp Response) {<commit_msg>Context may acquire more than one Mutex<commit_after>package dispatch

import (
	"runtime/debug"
	"time"
)

type Mutex chan struct{}

func NewMutex() Mutex {
	return make(chan struct{}, 1)
}

type Context struct {
	m      []Mutex
	cancel chan struct{}
}

func NewContext() *Context {
	ctx, _ := NewContextWithCancel()
	return ctx
}

func NewContextWithTimeOut(t time.Duration) *Context {
	ctx, cancel := NewContextWithCancel()
	time.AfterFunc(t, cancel)
	return ctx
}

func NewContextWithCancel() (ctx *Context, cancel func()) {
	c := make(chan struct{})
	return &Context{cancel: c}, func() { close(c) }
}

func (ctx *Context) AcquireOrCancel(m Mutex) bool {
	select {
	case m <- struct{}{}:
		ctx.m = append(ctx.m, m)
		return true
	case <-ctx.cancel:
		return false
	}
}

func (ctx *Context) Release() {
	if n := len(ctx.m); n > 0 {
		<-ctx.m[n-1]
		ctx.m = ctx.m[:n-1]
	} else {
		panic("Context is not obtaining any Mutex")
	}
}

type Handler interface {
	Serve(ctx *Context, m Mutex, r Request) Response
}

type HandlerFunc func(ctx *Context, m Mutex, r Request) Response

func (f HandlerFunc) Serve(ctx *Context, m Mutex, r Request) (rsp Response) {
{'fraction_non_alphanumeric': 0.1407376006782535, 'fraction_numerical': 0.003391267486222976, 'mean_word_length': 3.989429175475687, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 5, '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': '6379546', 'n_tokens_mistral': 879, 'n_tokens_neox': 842, 'n_words': 268}