8bit align struct fields

This commit is contained in:
mochi
2022-01-28 21:05:50 +00:00
parent a60c96c889
commit 6550b8d680
11 changed files with 64 additions and 79 deletions

View File

@@ -15,27 +15,25 @@ var (
ErrInsufficientBytes = errors.New("Insufficient bytes to return") ErrInsufficientBytes = errors.New("Insufficient bytes to return")
) )
// buffer contains core values and methods to be included in a reader or writer.
type Buffer struct { type Buffer struct {
Mu sync.RWMutex // the buffer needs it's own mutex to work properly.
ID string // the identifier of the buffer. This is used in debug output.
size int // the size of the buffer.
mask int // a bitmask of the buffer size (size-1).
block int // the size of the R/W block.
buf []byte // the bytes buffer. buf []byte // the bytes buffer.
tmp []byte // a temporary buffer. tmp []byte // a temporary buffer.
_ int32 // align the next fields to an 8-byte boundary for atomic access. Mu sync.RWMutex // the buffer needs its own mutex to work properly.
ID string // the identifier of the buffer. This is used in debug output.
head int64 // the current position in the sequence - a forever increasing index. head int64 // the current position in the sequence - a forever increasing index.
tail int64 // the committed position in the sequence - a forever increasing index. tail int64 // the committed position in the sequence - a forever increasing index.
rcond *sync.Cond // the sync condition for the buffer reader. rcond *sync.Cond // the sync condition for the buffer reader.
wcond *sync.Cond // the sync condition for the buffer writer. wcond *sync.Cond // the sync condition for the buffer writer.
size int // the size of the buffer.
mask int // a bitmask of the buffer size (size-1).
block int // the size of the R/W block.
done uint32 // indicates that the buffer is closed. done uint32 // indicates that the buffer is closed.
State uint32 // indicates whether the buffer is reading from (1) or writing to (2). State uint32 // indicates whether the buffer is reading from (1) or writing to (2).
} }
// NewBuffer returns a new instance of buffer. You should call NewReader or // NewBuffer returns a new instance of buffer. You should call NewReader or
// NewWriter instead of this function. // NewWriter instead of this function.
func NewBuffer(size, block int) Buffer { func NewBuffer(size, block int) *Buffer {
if size == 0 { if size == 0 {
size = DefaultBufferSize size = DefaultBufferSize
} }

View File

@@ -84,30 +84,30 @@ func (cl *Clients) GetByListener(id string) []*Client {
// Client contains information about a client known by the broker. // Client contains information about a client known by the broker.
type Client struct { type Client struct {
sync.RWMutex State State // the operational state of the client.
LWT LWT // the last will and testament for the client.
Inflight *Inflight // a map of in-flight qos messages.
sync.RWMutex // mutex
Username []byte // the username the client authenticated with.
AC auth.Controller // an auth controller inherited from the listener.
Listener string // the id of the listener the client is connected to.
ID string // the client id.
conn net.Conn // the net.Conn used to establish the connection. conn net.Conn // the net.Conn used to establish the connection.
r *circ.Reader // a reader for reading incoming bytes. r *circ.Reader // a reader for reading incoming bytes.
w *circ.Writer // a writer for writing outgoing bytes. w *circ.Writer // a writer for writing outgoing bytes.
ID string // the client id.
AC auth.Controller // an auth controller inherited from the listener.
Subscriptions topics.Subscriptions // a map of the subscription filters a client maintains. Subscriptions topics.Subscriptions // a map of the subscription filters a client maintains.
Listener string // the id of the listener the client is connected to. systemInfo *system.Info // pointers to server system info.
Inflight Inflight // a map of in-flight qos messages. packetID uint32 // the current highest packetID.
Username []byte // the username the client authenticated with.
keepalive uint16 // the number of seconds the connection can wait. keepalive uint16 // the number of seconds the connection can wait.
cleanSession bool // indicates if the client expects a clean-session. cleanSession bool // indicates if the client expects a clean-session.
packetID uint32 // the current highest packetID.
LWT LWT // the last will and testament for the client.
State State // the operational state of the client.
systemInfo *system.Info // pointers to server system info.
} }
// State tracks the state of the client. // State tracks the state of the client.
type State struct { type State struct {
Done uint32 // atomic counter which indicates that the client has closed.
started *sync.WaitGroup // tracks the goroutines which have been started. started *sync.WaitGroup // tracks the goroutines which have been started.
endedW *sync.WaitGroup // tracks when the writer has ended. endedW *sync.WaitGroup // tracks when the writer has ended.
endedR *sync.WaitGroup // tracks when the reader has ended. endedR *sync.WaitGroup // tracks when the reader has ended.
Done uint32 // atomic counter which indicates that the client has closed.
endOnce sync.Once // only end once. endOnce sync.Once // only end once.
} }
@@ -454,8 +454,8 @@ func (cl *Client) WritePacket(pk packets.Packet) (n int, err error) {
// LWT contains the last will and testament details for a client connection. // LWT contains the last will and testament details for a client connection.
type LWT struct { type LWT struct {
Topic string // the topic the will message shall be sent to.
Message []byte // the message that shall be sent when the client disconnects. Message []byte // the message that shall be sent when the client disconnects.
Topic string // the topic the will message shall be sent to.
Qos byte // the quality of service desired. Qos byte // the quality of service desired.
Retain bool // indicates whether the will message should be retained Retain bool // indicates whether the will message should be retained
} }

View File

@@ -6,11 +6,11 @@ import (
// FixedHeader contains the values of the fixed header portion of the MQTT packet. // FixedHeader contains the values of the fixed header portion of the MQTT packet.
type FixedHeader struct { type FixedHeader struct {
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
Dup bool // indicates if the packet was already sent at an earlier time.
Qos byte // indicates the quality of service expected.
Retain bool // whether the message should be retained.
Remaining int // the number of remaining bytes in the payload. Remaining int // the number of remaining bytes in the payload.
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
Qos byte // indicates the quality of service expected.
Dup bool // indicates if the packet was already sent at an earlier time.
Retain bool // whether the message should be retained.
} }
// Encode encodes the FixedHeader and returns a bytes buffer. // Encode encodes the FixedHeader and returns a bytes buffer.

View File

@@ -76,44 +76,31 @@ var (
// packet structs, this is a single concrete packet type to cover all packet // packet structs, this is a single concrete packet type to cover all packet
// types, which allows us to take advantage of various compiler optimizations. // types, which allows us to take advantage of various compiler optimizations.
type Packet struct { type Packet struct {
FixedHeader FixedHeader FixedHeader FixedHeader
AllowClients []string // For use with OnMessage event hook.
PacketID uint16 Topics []string
ReturnCodes []byte
// Connect
ProtocolName []byte ProtocolName []byte
Qoss []byte
Payload []byte
Username []byte
Password []byte
WillMessage []byte
ClientIdentifier string
TopicName string
WillTopic string
PacketID uint16
Keepalive uint16
ReturnCode byte
ProtocolVersion byte ProtocolVersion byte
WillQos byte
ReservedBit byte
CleanSession bool CleanSession bool
WillFlag bool WillFlag bool
WillQos byte
WillRetain bool WillRetain bool
UsernameFlag bool UsernameFlag bool
PasswordFlag bool PasswordFlag bool
ReservedBit byte SessionPresent bool
Keepalive uint16
ClientIdentifier string
WillTopic string
WillMessage []byte
Username []byte
Password []byte
// Connack
SessionPresent bool
ReturnCode byte
// Publish
TopicName string
Payload []byte
// Subscribe, Unsubscribe
Topics []string
Qoss []byte
// If AllowClients set, only deliver to clients in the client allow list.
// For use with the OnMessage event hook.
AllowClients []string
ReturnCodes []byte // Suback
} }
// ConnectEncode encodes a connect packet. // ConnectEncode encodes a connect packet.

View File

@@ -175,12 +175,12 @@ func (x *Index) Messages(filter string) []packets.Packet {
// Leaf is a child node on the tree. // Leaf is a child node on the tree.
type Leaf struct { type Leaf struct {
Message packets.Packet // a message which has been retained for a specific topic.
Key string // the key that was used to create the leaf. Key string // the key that was used to create the leaf.
Filter string // the path of the topic filter being matched.
Parent *Leaf // a pointer to the parent node for the leaf. Parent *Leaf // a pointer to the parent node for the leaf.
Leaves map[string]*Leaf // a map of child nodes, keyed on particle id. Leaves map[string]*Leaf // a map of child nodes, keyed on particle id.
Clients map[string]byte // a map of client ids subscribed to the topic. Clients map[string]byte // a map of client ids subscribed to the topic.
Filter string // the path of the topic filter being matched.
Message packets.Packet // a message which has been retained for a specific topic.
} }
// scanSubscribers recursively steps through a branch of leaves finding clients who // scanSubscribers recursively steps through a branch of leaves finding clients who

View File

@@ -18,9 +18,9 @@ import (
type HTTPStats struct { type HTTPStats struct {
sync.RWMutex sync.RWMutex
id string // the internal id of the listener. id string // the internal id of the listener.
address string // the network address to bind to.
config *Config // configuration values for the listener. config *Config // configuration values for the listener.
system *system.Info // pointers to the server data. system *system.Info // pointers to the server data.
address string // the network address to bind to.
listen *http.Server // the http server. listen *http.Server // the http server.
end uint32 // ensure the close methods are only called once.} end uint32 // ensure the close methods are only called once.}
} }

View File

@@ -38,10 +38,10 @@ type Listener interface {
// Listeners contains the network listeners for the broker. // Listeners contains the network listeners for the broker.
type Listeners struct { type Listeners struct {
sync.RWMutex
wg sync.WaitGroup // a waitgroup that waits for all listeners to finish. wg sync.WaitGroup // a waitgroup that waits for all listeners to finish.
internal map[string]Listener // a map of active listeners. internal map[string]Listener // a map of active listeners.
system *system.Info // pointers to system info. system *system.Info // pointers to system info.
sync.RWMutex
} }
// New returns a new instance of Listeners. // New returns a new instance of Listeners.

View File

@@ -22,11 +22,11 @@ func MockEstablisher(id string, c net.Conn, ac auth.Controller) error {
type MockListener struct { type MockListener struct {
sync.RWMutex sync.RWMutex
id string // the id of the listener. id string // the id of the listener.
Config *Config // configuration for the listener.
address string // the network address the listener binds to. address string // the network address the listener binds to.
Listening bool // indiciate the listener is listening. Config *Config // configuration for the listener.
Serving bool // indicate the listener is serving.
done chan bool // indicate the listener is done. done chan bool // indicate the listener is done.
Serving bool // indicate the listener is serving.
Listening bool // indiciate the listener is listening.
ErrListen bool // throw an error on listen. ErrListen bool // throw an error on listen.
} }

View File

@@ -14,10 +14,10 @@ import (
type TCP struct { type TCP struct {
sync.RWMutex sync.RWMutex
id string // the internal id of the listener. id string // the internal id of the listener.
config *Config // configuration values for the listener.
protocol string // the TCP protocol to use. protocol string // the TCP protocol to use.
address string // the network address to bind to. address string // the network address to bind to.
listen net.Listener // a net.Listener which will listen for new clients. listen net.Listener // a net.Listener which will listen for new clients.
config *Config // configuration values for the listener.
end uint32 // ensure the close methods are only called once. end uint32 // ensure the close methods are only called once.
} }

View File

@@ -31,11 +31,11 @@ var (
type Websocket struct { type Websocket struct {
sync.RWMutex sync.RWMutex
id string // the internal id of the listener. id string // the internal id of the listener.
config *Config // configuration values for the listener.
address string // the network address to bind to. address string // the network address to bind to.
config *Config // configuration values for the listener.
listen *http.Server // an http server for serving websocket connections. listen *http.Server // an http server for serving websocket connections.
end uint32 // ensure the close methods are only called once.
establish EstablishFunc // the server's establish conection handler. establish EstablishFunc // the server's establish conection handler.
end uint32 // ensure the close methods are only called once.
} }
// wsConn is a websocket connection which satisfies the net.Conn interface. // wsConn is a websocket connection which satisfies the net.Conn interface.

View File

@@ -53,50 +53,50 @@ type Subscription struct {
// Message contains the details of a retained or inflight message. // Message contains the details of a retained or inflight message.
type Message struct { type Message struct {
ID string // the storage key.
T string // the type of the stored data.
Client string // the id of the client who sent the message (if inflight).
FixedHeader FixedHeader // the header properties of the message.
PacketID uint16 // the unique id of the packet (if inflight).
TopicName string // the topic the message was sent to (if retained).
Payload []byte // the message payload (if retained). Payload []byte // the message payload (if retained).
FixedHeader FixedHeader // the header properties of the message.
T string // the type of the stored data.
ID string // the storage key.
Client string // the id of the client who sent the message (if inflight).
TopicName string // the topic the message was sent to (if retained).
Sent int64 // the last time the message was sent (for retries) in unixtime (if inflight). Sent int64 // the last time the message was sent (for retries) in unixtime (if inflight).
Resends int // the number of times the message was attempted to be sent (if inflight). Resends int // the number of times the message was attempted to be sent (if inflight).
PacketID uint16 // the unique id of the packet (if inflight).
} }
// FixedHeader contains the fixed header properties of a message. // FixedHeader contains the fixed header properties of a message.
type FixedHeader struct { type FixedHeader struct {
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
Dup bool // indicates if the packet was already sent at an earlier time.
Qos byte // indicates the quality of service expected.
Retain bool // whether the message should be retained.
Remaining int // the number of remaining bytes in the payload. Remaining int // the number of remaining bytes in the payload.
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
Qos byte // indicates the quality of service expected.
Dup bool // indicates if the packet was already sent at an earlier time.
Retain bool // whether the message should be retained.
} }
// Client contains client data that can be persistently stored. // Client contains client data that can be persistently stored.
type Client struct { type Client struct {
LWT LWT // the last-will-and-testament message for the client.
Username []byte // the username the client authenticated with.
ID string // the storage key. ID string // the storage key.
ClientID string // the id of the client. ClientID string // the id of the client.
T string // the type of the stored data. T string // the type of the stored data.
Listener string // the last known listener id for the client Listener string // the last known listener id for the client
Username []byte // the username the client authenticated with.
LWT LWT // the last-will-and-testament message for the client.
} }
// LWT contains details about a clients LWT payload. // LWT contains details about a clients LWT payload.
type LWT struct { type LWT struct {
Topic string // the topic the will message shall be sent to.
Message []byte // the message that shall be sent when the client disconnects. Message []byte // the message that shall be sent when the client disconnects.
Topic string // the topic the will message shall be sent to.
Qos byte // the quality of service desired. Qos byte // the quality of service desired.
Retain bool // indicates whether the will message should be retained Retain bool // indicates whether the will message should be retained
} }
// MockStore is a mock storage backend for testing. // MockStore is a mock storage backend for testing.
type MockStore struct { type MockStore struct {
Fail map[string]bool // issue errors for different methods.
FailOpen bool // error on open. FailOpen bool // error on open.
Closed bool // indicate mock store is closed. Closed bool // indicate mock store is closed.
Opened bool // indicate mock store is open. Opened bool // indicate mock store is open.
Fail map[string]bool // issue errors for different methods.
} }
// Open opens the storage instance. // Open opens the storage instance.