Skip to content

Instantly share code, notes, and snippets.

@MarcoPolo
Created January 18, 2023 16:49
Show Gist options
  • Save MarcoPolo/ecce2221f21f5897e378a0a35b427f2c to your computer and use it in GitHub Desktop.
Save MarcoPolo/ecce2221f21f5897e378a0a35b427f2c to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"math"
"testing"
)
// Package level var so that results aren't optimized away
var errCount = 0
type LimitsWithPtr struct {
InboundConns *int
OutboundConns *int
Memory *int
}
type currentLimits struct {
InboundConns int
OutboundConns int
Memory int
Limits LimitsWithPtr
}
func (l *currentLimits) CheckLimits(increaseInbound, increaseOutbound, increaseMemory int) error {
if l.Limits.InboundConns != nil && (l.InboundConns+increaseInbound) > *l.Limits.InboundConns {
return errors.New("inbound connections limit exceeded")
}
l.InboundConns += increaseInbound
if l.Limits.OutboundConns != nil && (l.OutboundConns+increaseOutbound) > *l.Limits.OutboundConns {
return errors.New("outbound connections limit exceeded")
}
l.OutboundConns += increaseOutbound
if l.Limits.Memory != nil && (l.Memory+increaseMemory) > *l.Limits.Memory {
return errors.New("memory limit exceeded")
}
l.Memory += increaseMemory
return nil
}
func buildLimits() *currentLimits {
inboundConns := math.MaxInt64 - 1
outboundConns := math.MaxInt64 - 1
memory := math.MaxInt64 - 1
l := currentLimits{
Limits: LimitsWithPtr{
InboundConns: &inboundConns,
OutboundConns: &outboundConns,
Memory: &memory,
},
}
return &l
}
func BenchmarkLimitsWithPtr(b *testing.B) {
for n := 0; n < b.N; n++ {
l := buildLimits()
err := l.CheckLimits(1, 1, 1)
if err != nil {
errCount++
}
}
}
type LimitsWithFlags struct {
InboundConns int
OutboundConns int
Memory int
IsSet uint8
}
type currentLimitsWithFlags struct {
InboundConns int
OutboundConns int
Memory int
Limits LimitsWithFlags
}
const (
InboundConnsSet = 1 << 0
OutboundConnsSet = 1 << 1
MemorySet = 1 << 2
)
func (l *currentLimitsWithFlags) CheckLimits(increaseInbound, increaseOutbound, increaseMemory int) error {
if l.Limits.IsSet&InboundConnsSet > 0 && (l.InboundConns+increaseInbound) > l.Limits.InboundConns {
return errors.New("inbound connections limit exceeded")
}
l.InboundConns += increaseInbound
if l.Limits.IsSet&OutboundConnsSet > 0 && (l.OutboundConns+increaseOutbound) > l.Limits.OutboundConns {
return errors.New("outbound connections limit exceeded")
}
l.OutboundConns += increaseOutbound
if l.Limits.IsSet&MemorySet > 0 && (l.Memory+increaseMemory) > l.Limits.Memory {
return errors.New("memory limit exceeded")
}
l.Memory += increaseMemory
return nil
}
func buildLimitsWithFlags() currentLimitsWithFlags {
l := currentLimitsWithFlags{
Limits: LimitsWithFlags{
InboundConns: math.MaxInt64 - 1,
OutboundConns: math.MaxInt64 - 1,
Memory: math.MaxInt64 - 1,
IsSet: InboundConnsSet | OutboundConnsSet | MemorySet,
},
}
return l
}
func BenchmarkLimitsWithFlag(b *testing.B) {
for n := 0; n < b.N; n++ {
l := buildLimitsWithFlags()
err := l.CheckLimits(1, 1, 1)
if err != nil {
errCount++
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment