Merge pull request #1224 from slingamn/errors_again

minor refactoring
This commit is contained in:
Shivaram Lingamneni 2020-08-04 18:59:08 -07:00 committed by GitHub
commit 8f490ae298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 60 additions and 52 deletions

View File

@ -5,6 +5,8 @@ package irc
import (
"sync"
"github.com/oragono/oragono/irc/utils"
)
type channelManagerEntry struct {
@ -23,17 +25,17 @@ type ChannelManager struct {
sync.RWMutex // tier 2
// chans is the main data structure, mapping casefolded name -> *Channel
chans map[string]*channelManagerEntry
chansSkeletons StringSet // skeletons of *unregistered* chans
registeredChannels StringSet // casefolds of registered chans
registeredSkeletons StringSet // skeletons of registered chans
purgedChannels StringSet // casefolds of purged chans
chansSkeletons utils.StringSet // skeletons of *unregistered* chans
registeredChannels utils.StringSet // casefolds of registered chans
registeredSkeletons utils.StringSet // skeletons of registered chans
purgedChannels utils.StringSet // casefolds of purged chans
server *Server
}
// NewChannelManager returns a new ChannelManager.
func (cm *ChannelManager) Initialize(server *Server) {
cm.chans = make(map[string]*channelManagerEntry)
cm.chansSkeletons = make(StringSet)
cm.chansSkeletons = make(utils.StringSet)
cm.server = server
cm.loadRegisteredChannels(server.Config())
@ -47,8 +49,8 @@ func (cm *ChannelManager) loadRegisteredChannels(config *Config) {
}
rawNames := cm.server.channelRegistry.AllChannels()
registeredChannels := make(StringSet, len(rawNames))
registeredSkeletons := make(StringSet, len(rawNames))
registeredChannels := make(utils.StringSet, len(rawNames))
registeredSkeletons := make(utils.StringSet, len(rawNames))
for _, name := range rawNames {
cfname, err := CasefoldChannel(name)
if err == nil {

View File

@ -4,15 +4,16 @@
package irc
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"encoding/json"
"github.com/tidwall/buntdb"
"github.com/oragono/oragono/irc/modes"
"github.com/tidwall/buntdb"
"github.com/oragono/oragono/irc/utils"
)
// this is exclusively the *persistence* layer for channel registration;
@ -140,8 +141,8 @@ func (reg *ChannelRegistry) AllChannels() (result []string) {
}
// PurgedChannels returns the set of all casefolded channel names that have been purged
func (reg *ChannelRegistry) PurgedChannels() (result map[string]empty) {
result = make(map[string]empty)
func (reg *ChannelRegistry) PurgedChannels() (result utils.StringSet) {
result = make(utils.StringSet)
prefix := fmt.Sprintf(keyChannelPurged, "")
reg.server.store.View(func(tx *buntdb.Tx) error {
@ -150,7 +151,7 @@ func (reg *ChannelRegistry) PurgedChannels() (result map[string]empty) {
return false
}
channel := strings.TrimPrefix(key, prefix)
result[channel] = empty{}
result.Add(channel)
return true
})
})

View File

@ -64,7 +64,7 @@ type Client struct {
destroyed bool
modes modes.ModeSet
hostname string
invitedTo StringSet
invitedTo utils.StringSet
isSTSOnly bool
languages []string
lastActive time.Time // last time they sent a command that wasn't PONG or similar
@ -1641,7 +1641,7 @@ func (client *Client) Invite(casefoldedChannel string) {
defer client.stateMutex.Unlock()
if client.invitedTo == nil {
client.invitedTo = make(StringSet)
client.invitedTo = make(utils.StringSet)
}
client.invitedTo.Add(casefoldedChannel)

View File

@ -5,11 +5,13 @@ package irc
import (
"testing"
"github.com/oragono/oragono/irc/utils"
)
func TestGenerateBatchID(t *testing.T) {
var session Session
s := make(StringSet)
s := make(utils.StringSet)
count := 100000
for i := 0; i < count; i++ {

View File

@ -625,7 +625,7 @@ type Config struct {
type OperClass struct {
Title string
WhoisLine string `yaml:"whois-line"`
Capabilities StringSet // map to make lookups much easier
Capabilities utils.StringSet // map to make lookups much easier
}
// OperatorClasses returns a map of assembled operator classes from the given config.
@ -663,7 +663,7 @@ func (conf *Config) OperatorClasses() (map[string]*OperClass, error) {
// create new operclass
var oc OperClass
oc.Capabilities = make(StringSet)
oc.Capabilities = make(utils.StringSet)
// get inhereted info from other operclasses
if len(info.Extends) > 0 {

View File

@ -73,13 +73,6 @@ var (
errRegisteredOnly = errors.New("Cannot join registered-only channel without an account")
)
// Socket Errors
var (
errNoPeerCerts = errors.New("Client did not provide a certificate")
errNotTLS = errors.New("Not a TLS connection")
errReadQ = errors.New("ReadQ Exceeded")
)
// String Errors
var (
errCouldNotStabilize = errors.New("Could not stabilize string while casefolding")

View File

@ -2998,9 +2998,9 @@ func whoHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Respo
}
} else {
// Construct set of channels the client is in.
userChannels := make(map[*Channel]bool)
userChannels := make(ChannelSet)
for _, channel := range client.Channels() {
userChannels[channel] = true
userChannels[channel] = empty{}
}
// Another client is a friend if they share at least one channel, or they are the same client.
@ -3010,7 +3010,7 @@ func whoHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Respo
}
for _, channel := range otherClient.Channels() {
if userChannels[channel] {
if _, present := userChannels[channel]; present {
return true
}
}

View File

@ -3,6 +3,7 @@ package irc
import (
"bufio"
"bytes"
"errors"
"net"
"unicode/utf8"
@ -18,6 +19,7 @@ const (
var (
crlf = []byte{'\r', '\n'}
errReadQ = errors.New("ReadQ Exceeded")
)
// IRCConn abstracts away the distinction between a regular
@ -31,7 +33,7 @@ type IRCConn interface {
// these take an IRC line or lines, correctly terminated with CRLF:
WriteLine([]byte) error
WriteLines([][]byte) error
// this returns an IRC line without the terminating CRLF:
// this returns an IRC line, possibly terminated with CRLF, LF, or nothing:
ReadLine() (line []byte, err error)
Close() error
@ -127,6 +129,9 @@ func (wc IRCWSConn) ReadLine() (line []byte, err error) {
messageType, line, err = wc.conn.ReadMessage()
// on empty message or non-text message, try again, block if necessary
if err != nil || (messageType == websocket.TextMessage && len(line) != 0) {
if err == websocket.ErrReadLimit {
err = errReadQ
}
return
}
}

View File

@ -28,17 +28,6 @@ func (clients ClientSet) Has(client *Client) bool {
return ok
}
type StringSet map[string]empty
func (s StringSet) Has(str string) bool {
_, ok := s[str]
return ok
}
func (s StringSet) Add(str string) {
s[str] = empty{}
}
// MemberSet is a set of members with modes.
type MemberSet map[*Client]*modes.ModeSet

View File

@ -10,27 +10,25 @@ import (
"time"
)
type e struct{}
// Semaphore is a counting semaphore.
// A semaphore of capacity 1 can be used as a trylock.
type Semaphore (chan e)
type Semaphore (chan empty)
// Initialize initializes a semaphore to a given capacity.
func (semaphore *Semaphore) Initialize(capacity int) {
*semaphore = make(chan e, capacity)
*semaphore = make(chan empty, capacity)
}
// Acquire acquires a semaphore, blocking if necessary.
func (semaphore *Semaphore) Acquire() {
(*semaphore) <- e{}
(*semaphore) <- empty{}
}
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
// successful. It never blocks.
func (semaphore *Semaphore) TryAcquire() (acquired bool) {
select {
case (*semaphore) <- e{}:
case (*semaphore) <- empty{}:
return true
default:
return false
@ -47,7 +45,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
timer := time.NewTimer(timeout)
select {
case (*semaphore) <- e{}:
case (*semaphore) <- empty{}:
acquired = true
case <-timer.C:
acquired = false
@ -61,7 +59,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
// Note that if the context is already expired, the acquire may succeed anyway.
func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
select {
case (*semaphore) <- e{}:
case (*semaphore) <- empty{}:
acquired = true
case <-ctx.Done():
acquired = false

17
irc/utils/types.go Normal file
View File

@ -0,0 +1,17 @@
// Copyright (c) 2020 Shivaram Lingamneni
// released under the MIT license
package utils
type empty struct{}
type StringSet map[string]empty
func (s StringSet) Has(str string) bool {
_, ok := s[str]
return ok
}
func (s StringSet) Add(str string) {
s[str] = empty{}
}

View File

@ -10,6 +10,7 @@ import (
"time"
"github.com/oragono/oragono/irc/history"
"github.com/oragono/oragono/irc/utils"
)
const (
@ -62,7 +63,7 @@ func timeToZncWireTime(t time.Time) (result string) {
type zncPlaybackTimes struct {
start time.Time
end time.Time
targets StringSet // nil for "*" (everything), otherwise the channel names
targets utils.StringSet // nil for "*" (everything), otherwise the channel names
setAt time.Time
}
@ -122,7 +123,7 @@ func zncPlaybackPlayHandler(client *Client, command string, params []string, rb
end = zncWireTimeToTime(params[3])
}
var targets StringSet
var targets utils.StringSet
var nickTargets []string
// three cases:
@ -145,7 +146,7 @@ func zncPlaybackPlayHandler(client *Client, command string, params []string, rb
if params[1] == "*" {
playPrivmsgs = true // XXX nil `targets` means "every channel"
} else {
targets = make(StringSet)
targets = make(utils.StringSet)
for _, targetName := range strings.Split(targetString, ",") {
if targetName == "*self" {
playPrivmsgs = true