Files
openfsd/postoffice/geohash_bucket.go
Reese Norris 57d54d6705 v0.1.0-alpha
Changes:
- Implement bootstrapping library for managing several concurrent internal services
- Refactor concurrency model for connections/logical clients and their associated I/O
- Refactor server context singleton
- Refactor error handling
    - Most errors are now gracefully sent to the FSD client directly encoded as an $ER packet,
      enhancing visibility and debugging
    - Most errors are now rightfully treated as non-fatal
- Refactor package/dependency graph
- Refactor calling conventions/interfaces for many packages
- Refactor database package
- Refactor post office

Features:
- Add VATSIM-esque HTTP/JSON "data feed"
- Add ephemeral in-memory database option
- Add user management REST API
- Add improved web interface
- Add MySQL support (drop SQLite support)
2024-10-07 12:50:39 -07:00

57 lines
1.4 KiB
Go

package postoffice
import (
"slices"
"sync"
)
// GeohashBucket is a contiguous & concurrency-safe array of addresses
type GeohashBucket struct {
lock sync.RWMutex
addresses []Address
}
// RLock locks this bucket for read-only access, then returns the slice of addresses to read.
// Conventionally, callers must use RUnlock() once done reading addresses.
func (e *GeohashBucket) RLock() (addresses []Address) {
e.lock.RLock()
return e.addresses
}
// RUnlock releases the read lock for this entry
func (e *GeohashBucket) RUnlock() {
e.lock.RUnlock()
}
// Delete deletes an address from this bucket's list.
// Returns the Len of the underlying list after deleting the value.
func (e *GeohashBucket) Delete(address Address) {
e.lock.Lock()
// Find index of entry to remove
i := slices.Index(e.addresses, address)
if i > -1 {
// Move last element into element we're deleting
e.addresses[i] = e.addresses[len(e.addresses)-1]
// Change slice header to reflect new Len
e.addresses = e.addresses[:len(e.addresses)-1]
// Reallocate the slice if the capacity is twice the Len
if len(e.addresses) > 0 && cap(e.addresses)/len(e.addresses) > 1 {
newSlice := make([]Address, len(e.addresses))
copy(newSlice, e.addresses)
e.addresses = newSlice
}
}
e.lock.Unlock()
}
// Add adds an address to this bucket's list
func (e *GeohashBucket) Add(address Address) {
e.lock.Lock()
e.addresses = append(e.addresses, address)
e.lock.Unlock()
}