Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a9e36708e8 | |||
| c00df926a0 | |||
| 632d79dd57 | |||
| b296d73367 | |||
| 2200657ba7 | |||
| 07f7893a26 | |||
| 7f21b733ed | |||
| 9816426780 |
16
internal/format.go
Normal file
16
internal/format.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func FormatBytes(b uint64) string {
|
||||||
|
const unit = 1024
|
||||||
|
if b < unit {
|
||||||
|
return fmt.Sprintf("%d B", b)
|
||||||
|
}
|
||||||
|
div, exp := uint64(unit), 0
|
||||||
|
for n := b / unit; n >= unit; n /= unit {
|
||||||
|
div *= unit
|
||||||
|
exp++
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.2f %cB", float64(b)/float64(div), "KMGTPE"[exp])
|
||||||
|
}
|
||||||
167
internal/hub.go
167
internal/hub.go
@@ -1,8 +1,10 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -10,9 +12,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
writeWait = 10 * time.Second
|
writeWait = 10 * time.Second
|
||||||
pongWait = 60 * time.Second
|
pongWait = 60 * time.Second
|
||||||
pingPeriod = 25 * time.Second
|
pingPeriod = (pongWait * 9) / 10
|
||||||
|
maxMessageSize = 512
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
minHighWaterMarkMapRebuild = 100
|
||||||
|
mapRebuildThreshold = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -20,58 +28,118 @@ type Client struct {
|
|||||||
Conn *websocket.Conn
|
Conn *websocket.Conn
|
||||||
Send chan []byte
|
Send chan []byte
|
||||||
SubscribedPath string
|
SubscribedPath string
|
||||||
done chan struct{}
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(conn *websocket.Conn, subscribedPath string) *Client {
|
func NewClient(conn *websocket.Conn, subscribedPath string) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
Send: make(chan []byte, 1),
|
Send: make(chan []byte, 256),
|
||||||
SubscribedPath: subscribedPath,
|
SubscribedPath: subscribedPath,
|
||||||
done: make(chan struct{}, 1),
|
mu: sync.Mutex{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Hub struct {
|
type Hub struct {
|
||||||
Clients map[*Client]bool
|
path string
|
||||||
Broadcast chan []byte
|
maxClients int
|
||||||
Register chan *Client
|
Clients map[*Client]bool
|
||||||
Unregister chan *Client
|
Broadcast chan []byte
|
||||||
|
Register chan *Client
|
||||||
|
Unregister chan *Client
|
||||||
|
monitor *MemoryMonitor
|
||||||
|
highWaterMark int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHub() *Hub {
|
func NewHub(path string, maxClients int) *Hub {
|
||||||
|
log.Printf("[%s] Hub created with max clients: %d", path, maxClients)
|
||||||
return &Hub{
|
return &Hub{
|
||||||
Broadcast: make(chan []byte, 1),
|
path: path,
|
||||||
Register: make(chan *Client, 1),
|
maxClients: maxClients,
|
||||||
Unregister: make(chan *Client, 1),
|
monitor: NewMemoryMonitor(),
|
||||||
|
Broadcast: make(chan []byte, 256),
|
||||||
|
Register: make(chan *Client, maxClients),
|
||||||
|
Unregister: make(chan *Client, maxClients),
|
||||||
Clients: make(map[*Client]bool),
|
Clients: make(map[*Client]bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) Run() {
|
// Run starts the hub event loop. It exits when ctx is cancelled.
|
||||||
|
func (h *Hub) Run(ctx context.Context) {
|
||||||
|
monitorTicker := time.NewTicker(MonitorInterval)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
monitorTicker.Stop()
|
||||||
|
// On shutdown, close every client's Send channel so WritePump sends
|
||||||
|
// a WebSocket close frame and exits. Also expire the read deadline so
|
||||||
|
// blocked ReadMessage() calls in ReadPump return immediately instead
|
||||||
|
// of waiting up to pongWait (60 s).
|
||||||
|
for client := range h.Clients {
|
||||||
|
close(client.Send)
|
||||||
|
client.Conn.SetReadDeadline(time.Now())
|
||||||
|
}
|
||||||
|
log.Printf("[%s] Hub stopped\n", h.path)
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Printf("[%s] Hub shutting down\n", h.path)
|
||||||
|
return
|
||||||
|
|
||||||
case client := <-h.Register:
|
case client := <-h.Register:
|
||||||
h.Clients[client] = true
|
if len(h.Clients) >= h.maxClients {
|
||||||
log.Println("Client registered")
|
close(client.Send)
|
||||||
case c := <-h.Unregister:
|
client.Conn.Close()
|
||||||
if v, ok := h.Clients[c]; ok {
|
log.Printf("[%s] Rejected client %s (max %d reached)\n", h.path, client.ID, h.maxClients)
|
||||||
fmt.Println(v, c)
|
break
|
||||||
delete(h.Clients, c)
|
|
||||||
close(c.Send)
|
|
||||||
}
|
}
|
||||||
log.Println("Client Unregistered")
|
h.Clients[client] = true
|
||||||
|
if len(h.Clients) > h.highWaterMark {
|
||||||
|
h.highWaterMark = len(h.Clients)
|
||||||
|
}
|
||||||
|
log.Printf("[%s] Client registered %s\n", h.path, client.ID)
|
||||||
|
|
||||||
|
case client := <-h.Unregister:
|
||||||
|
if _, ok := h.Clients[client]; ok {
|
||||||
|
delete(h.Clients, client)
|
||||||
|
close(client.Send)
|
||||||
|
|
||||||
|
// Rebuild the map when the live set has dropped to less than
|
||||||
|
// 1/mapRebuildThreshold of the peak, so the old backing buckets
|
||||||
|
// are released to the GC.
|
||||||
|
if h.highWaterMark >= minHighWaterMarkMapRebuild &&
|
||||||
|
len(h.Clients) < h.highWaterMark/mapRebuildThreshold {
|
||||||
|
rebuilt := make(map[*Client]bool, len(h.Clients))
|
||||||
|
for c := range h.Clients {
|
||||||
|
rebuilt[c] = true
|
||||||
|
}
|
||||||
|
h.Clients = rebuilt
|
||||||
|
h.highWaterMark = len(h.Clients)
|
||||||
|
log.Printf("[%s] Clients map rebuilt: %d active clients\n", h.path, len(h.Clients))
|
||||||
|
}
|
||||||
|
log.Printf("[%s] Client Unregistered %s\n", h.path, client.ID)
|
||||||
|
}
|
||||||
|
|
||||||
case message := <-h.Broadcast:
|
case message := <-h.Broadcast:
|
||||||
for client := range h.Clients {
|
for client := range h.Clients {
|
||||||
client.Send <- message
|
select {
|
||||||
// select {
|
case client.Send <- message:
|
||||||
// case client.Send <- message:
|
default:
|
||||||
// default:
|
close(client.Send)
|
||||||
// close(client.Send)
|
delete(h.Clients, client)
|
||||||
// delete(h.Clients, client)
|
log.Printf("[%s] Client %s removed (slow/disconnected)\n", h.path, client.ID)
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case <-monitorTicker.C:
|
||||||
|
current, peak := h.monitor.Snapshot()
|
||||||
|
clientLength := len(h.Clients)
|
||||||
|
if clientLength > 0 {
|
||||||
|
log.Printf("[%s] connected clients: %d | heap alloc: %s | peak heap alloc: %s",
|
||||||
|
h.path, clientLength, FormatBytes(current), FormatBytes(peak),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,7 +149,10 @@ func (h *Hub) Run() {
|
|||||||
func WritePump(c *Client, h *Hub) {
|
func WritePump(c *Client, h *Hub) {
|
||||||
pingTicker := time.NewTicker(pingPeriod)
|
pingTicker := time.NewTicker(pingPeriod)
|
||||||
defer func() {
|
defer func() {
|
||||||
h.Unregister <- c
|
select {
|
||||||
|
case h.Unregister <- c:
|
||||||
|
default:
|
||||||
|
}
|
||||||
pingTicker.Stop()
|
pingTicker.Stop()
|
||||||
c.Conn.Close()
|
c.Conn.Close()
|
||||||
}()
|
}()
|
||||||
@@ -90,19 +161,31 @@ func WritePump(c *Client, h *Hub) {
|
|||||||
select {
|
select {
|
||||||
case message, ok := <-c.Send:
|
case message, ok := <-c.Send:
|
||||||
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
|
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
c.Conn.WriteMessage(websocket.CloseMessage, []byte{})
|
c.Conn.WriteMessage(websocket.CloseMessage, []byte{})
|
||||||
fmt.Println(ok)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
w, err := c.Conn.NextWriter(websocket.TextMessage)
|
||||||
fmt.Println(err)
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(message)
|
||||||
|
|
||||||
|
// Flush any messages that queued up while we were writing.
|
||||||
|
n := len(c.Send)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
w.Write(<-c.Send)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := w.Close(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-pingTicker.C:
|
case <-pingTicker.C:
|
||||||
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
|
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
if err := c.Conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
|
|
||||||
|
if err := c.Conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -112,19 +195,23 @@ func WritePump(c *Client, h *Hub) {
|
|||||||
|
|
||||||
func ReadPump(c *Client, h *Hub) {
|
func ReadPump(c *Client, h *Hub) {
|
||||||
defer func() {
|
defer func() {
|
||||||
h.Unregister <- c
|
select {
|
||||||
|
case h.Unregister <- c:
|
||||||
|
default:
|
||||||
|
}
|
||||||
c.Conn.Close()
|
c.Conn.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
c.Conn.SetReadLimit(1024)
|
c.Conn.SetReadLimit(maxMessageSize)
|
||||||
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
|
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
|
|
||||||
c.Conn.SetPongHandler(func(string) error {
|
c.Conn.SetPongHandler(func(string) error {
|
||||||
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
|
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
for {
|
for {
|
||||||
messageType, message, err := c.Conn.ReadMessage()
|
_, message, err := c.Conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||||
log.Printf("WebSocket error: %v", err)
|
log.Printf("WebSocket error: %v", err)
|
||||||
@@ -132,8 +219,6 @@ func ReadPump(c *Client, h *Hub) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if messageType == websocket.TextMessage {
|
log.Printf("Received: %s\n", message)
|
||||||
log.Printf("Received: %s\n", message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
60
internal/memory_monitor.go
Normal file
60
internal/memory_monitor.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"runtime"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const MonitorInterval = 30 * time.Second
|
||||||
|
|
||||||
|
type MemoryMonitor struct {
|
||||||
|
peakAlloc atomic.Uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMemoryMonitor() *MemoryMonitor {
|
||||||
|
return &MemoryMonitor{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Snapshot reads current heap allocation, updates the peak, and returns both.
|
||||||
|
func (m *MemoryMonitor) Snapshot() (currentAlloc, peakAlloc uint64) {
|
||||||
|
var ms runtime.MemStats
|
||||||
|
runtime.ReadMemStats(&ms)
|
||||||
|
currentAlloc = ms.HeapAlloc
|
||||||
|
for {
|
||||||
|
peak := m.peakAlloc.Load()
|
||||||
|
if currentAlloc <= peak {
|
||||||
|
return currentAlloc, peak
|
||||||
|
}
|
||||||
|
if m.peakAlloc.CompareAndSwap(peak, currentAlloc) {
|
||||||
|
return currentAlloc, currentAlloc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run starts a periodic monitor loop that calls logFn with each snapshot.
|
||||||
|
// It blocks until ctx is cancelled.
|
||||||
|
func (m *MemoryMonitor) Run(ctx context.Context, logFn func(currentAlloc, peakAlloc uint64)) {
|
||||||
|
ticker := time.NewTicker(MonitorInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
current, peak := m.Snapshot()
|
||||||
|
logFn(current, peak)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultLogFn returns a log function with the given prefix.
|
||||||
|
func DefaultLogFn(prefix string) func(currentAlloc, peakAlloc uint64) {
|
||||||
|
return func(currentAlloc, peakAlloc uint64) {
|
||||||
|
log.Printf("[%s] heap alloc: %s | peak heap alloc: %s",
|
||||||
|
prefix, FormatBytes(currentAlloc), FormatBytes(peakAlloc))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,19 +4,22 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
custom_rwmutex "git.neurocipta.com/rogerferdinan/custom-rwmutex"
|
customrwmutex "git.neurocipta.com/rogerferdinan/custom-rwmutex"
|
||||||
safemap "git.neurocipta.com/rogerferdinan/safe-map"
|
safemap "git.neurocipta.com/rogerferdinan/safe-map"
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pingPeriod = 10 * time.Second
|
pingPeriod = 10 * time.Second
|
||||||
readDeadline = 30 * time.Second
|
readDeadline = 30 * time.Second
|
||||||
|
writeDeadline = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageType uint
|
type MessageType uint
|
||||||
@@ -36,6 +39,7 @@ type Message struct {
|
|||||||
type SafeWebsocketClientBuilder struct {
|
type SafeWebsocketClientBuilder struct {
|
||||||
baseHost *string `nil_checker:"required"`
|
baseHost *string `nil_checker:"required"`
|
||||||
basePort *uint16 `nil_checker:"required"`
|
basePort *uint16 `nil_checker:"required"`
|
||||||
|
headers *map[string]string
|
||||||
path *string
|
path *string
|
||||||
rawQuery *string
|
rawQuery *string
|
||||||
isDrop *bool
|
isDrop *bool
|
||||||
@@ -58,6 +62,11 @@ func (b *SafeWebsocketClientBuilder) BasePort(port uint16) *SafeWebsocketClientB
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *SafeWebsocketClientBuilder) Headers(headers map[string]string) *SafeWebsocketClientBuilder {
|
||||||
|
b.headers = &headers
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketClientBuilder) UseTLS(useTLS bool) *SafeWebsocketClientBuilder {
|
func (b *SafeWebsocketClientBuilder) UseTLS(useTLS bool) *SafeWebsocketClientBuilder {
|
||||||
b.useTLS = &useTLS
|
b.useTLS = &useTLS
|
||||||
return b
|
return b
|
||||||
@@ -117,20 +126,23 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC
|
|||||||
wsClient := SafeWebsocketClient{
|
wsClient := SafeWebsocketClient{
|
||||||
baseHost: *b.baseHost,
|
baseHost: *b.baseHost,
|
||||||
basePort: *b.basePort,
|
basePort: *b.basePort,
|
||||||
|
headers: b.headers,
|
||||||
useTLS: *b.useTLS,
|
useTLS: *b.useTLS,
|
||||||
isDrop: *b.isDrop,
|
isDrop: *b.isDrop,
|
||||||
path: b.path,
|
path: b.path,
|
||||||
rawQuery: b.rawQuery,
|
rawQuery: b.rawQuery,
|
||||||
dataChannel: make(chan []byte, *b.channelSize),
|
dataChannel: make(chan []byte, *b.channelSize),
|
||||||
mu: custom_rwmutex.NewCustomRwMutex(),
|
mu: customrwmutex.NewCustomRwMutex(),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
reconnectCh: make(chan struct{}, 1),
|
reconnectCh: make(chan struct{}, 1),
|
||||||
isConnected: false,
|
isConnected: false,
|
||||||
doneMap: safemap.NewSafeMap[string, chan struct{}](),
|
doneMap: safemap.NewSafeMap[string, chan struct{}](),
|
||||||
writeChan: make(chan Message, *b.writeChannelSize),
|
writeChan: make(chan Message, *b.writeChannelSize),
|
||||||
|
monitor: internal.NewMemoryMonitor(),
|
||||||
}
|
}
|
||||||
|
|
||||||
go wsClient.reconnectHandler()
|
go wsClient.reconnectHandler()
|
||||||
|
go wsClient.monitor.Run(ctx, internal.DefaultLogFn("client-monitor"))
|
||||||
|
|
||||||
if err := wsClient.connect(); err != nil {
|
if err := wsClient.connect(); err != nil {
|
||||||
return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
return nil, fmt.Errorf("failed to establish initial connection: %v", err)
|
||||||
@@ -142,16 +154,19 @@ func (b *SafeWebsocketClientBuilder) Build(ctx context.Context) (*SafeWebsocketC
|
|||||||
type SafeWebsocketClient struct {
|
type SafeWebsocketClient struct {
|
||||||
baseHost string
|
baseHost string
|
||||||
basePort uint16
|
basePort uint16
|
||||||
|
headers *map[string]string
|
||||||
|
|
||||||
isDrop bool
|
isDrop bool
|
||||||
useTLS bool
|
useTLS bool
|
||||||
path *string
|
path *string
|
||||||
rawQuery *string
|
rawQuery *string
|
||||||
|
|
||||||
mu *custom_rwmutex.CustomRwMutex
|
mu *customrwmutex.CustomRwMutex
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancelFuncs []context.CancelFunc
|
cancelFuncs []context.CancelFunc
|
||||||
dataChannel chan []byte
|
dataChannel chan []byte
|
||||||
|
dataChannelOnce sync.Once
|
||||||
|
|
||||||
reconnectCh chan struct{}
|
reconnectCh chan struct{}
|
||||||
reconnectChans []chan struct{}
|
reconnectChans []chan struct{}
|
||||||
@@ -159,9 +174,10 @@ type SafeWebsocketClient struct {
|
|||||||
doneMap *safemap.SafeMap[string, chan struct{}]
|
doneMap *safemap.SafeMap[string, chan struct{}]
|
||||||
|
|
||||||
writeChan chan Message
|
writeChan chan Message
|
||||||
pongChan chan error
|
monitor *internal.MemoryMonitor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) connect() error {
|
func (wsClient *SafeWebsocketClient) connect() error {
|
||||||
var scheme string
|
var scheme string
|
||||||
if wsClient.useTLS {
|
if wsClient.useTLS {
|
||||||
@@ -173,219 +189,92 @@ func (wsClient *SafeWebsocketClient) connect() error {
|
|||||||
Scheme: scheme,
|
Scheme: scheme,
|
||||||
Host: fmt.Sprintf("%s:%d", wsClient.baseHost, wsClient.basePort),
|
Host: fmt.Sprintf("%s:%d", wsClient.baseHost, wsClient.basePort),
|
||||||
}
|
}
|
||||||
|
|
||||||
if wsClient.path != nil && strings.TrimSpace(*wsClient.path) != "" {
|
if wsClient.path != nil && strings.TrimSpace(*wsClient.path) != "" {
|
||||||
newURL.Path = *wsClient.path
|
newURL.Path = *wsClient.path
|
||||||
}
|
}
|
||||||
|
|
||||||
if wsClient.rawQuery != nil && strings.TrimSpace(*wsClient.rawQuery) != "" {
|
if wsClient.rawQuery != nil && strings.TrimSpace(*wsClient.rawQuery) != "" {
|
||||||
newURL.RawQuery = *wsClient.rawQuery
|
newURL.RawQuery = *wsClient.rawQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(newURL.String(), nil)
|
header := make(http.Header, 0)
|
||||||
|
if wsClient.headers != nil {
|
||||||
|
for k, v := range *wsClient.headers {
|
||||||
|
header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, _, err := websocket.DefaultDialer.Dial(newURL.String(), header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
return fmt.Errorf("failed to connect to %s: %w", wsClient.baseHost, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pingCtx, pingCancel := context.WithCancel(context.Background())
|
pingCtx, pingCancel := context.WithCancel(context.Background())
|
||||||
|
pumpCtx, pumpCancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel)
|
if wsClient.conn != nil {
|
||||||
|
wsClient.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
wsClient.conn = conn
|
||||||
|
|
||||||
|
wsClient.cancelFuncs = append(wsClient.cancelFuncs, pingCancel, pumpCancel)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
go wsClient.startPingTicker(pingCtx)
|
go wsClient.startPingTicker(pingCtx)
|
||||||
|
go wsClient.writePump(pumpCtx, conn)
|
||||||
|
go wsClient.readPump(pumpCtx, conn)
|
||||||
|
|
||||||
|
conn.SetPingHandler(func(pingData string) error {
|
||||||
|
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
||||||
|
log.Printf("error on read deadline: %v\n", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case wsClient.writeChan <- Message{
|
||||||
|
MessageType: MessageTypePong,
|
||||||
|
Data: []byte(pingData),
|
||||||
|
}:
|
||||||
|
default:
|
||||||
|
log.Println("writeChan full, dropping pong")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
conn.SetPongHandler(func(pingData string) error {
|
||||||
|
if err := conn.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
||||||
|
log.Printf("error on read deadline: %v\n", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
if wsClient.conn != nil {
|
|
||||||
wsClient.conn.Close()
|
|
||||||
}
|
|
||||||
wsClient.conn = conn
|
|
||||||
wsClient.isConnected = true
|
wsClient.isConnected = true
|
||||||
|
|
||||||
go wsClient.writePump()
|
|
||||||
go wsClient.readPump()
|
|
||||||
|
|
||||||
// conn.SetPingHandler(func(pingData string) error {
|
|
||||||
// wsClient.writeChan <- Message{
|
|
||||||
// MessageType: MessageTypePong,
|
|
||||||
// Data: []byte(pingData),
|
|
||||||
// }
|
|
||||||
|
|
||||||
// select {
|
|
||||||
// case err := <-wsClient.pongChan:
|
|
||||||
// return err
|
|
||||||
// default:
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
// })
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) writePump() {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
wsClient.mu.WriteHandler(func() error {
|
|
||||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
var c *websocket.Conn
|
|
||||||
wsClient.mu.ReadHandler(func() error {
|
|
||||||
c = wsClient.conn
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Println("Writer canceled by context")
|
|
||||||
return
|
|
||||||
case data := <-wsClient.writeChan:
|
|
||||||
if c == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
|
||||||
log.Printf("error on write message: %v\n", err)
|
|
||||||
if data.MessageType == MessageTypePong {
|
|
||||||
wsClient.pongChan <- err
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) readPump() {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
wsClient.mu.WriteHandler(func() error {
|
|
||||||
wsClient.cancelFuncs = append(wsClient.cancelFuncs, cancel)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
var c *websocket.Conn
|
|
||||||
wsClient.mu.ReadHandler(func() error {
|
|
||||||
c = wsClient.conn
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if wsClient.isDrop {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Println("Reader canceled by context")
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
if c == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
|
||||||
log.Printf("error on read deadline: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
messageType, data, err := c.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error on read message: %v\n", err)
|
|
||||||
wsClient.triggerReconnect()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if messageType != websocket.TextMessage {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case wsClient.dataChannel <- data:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
log.Println("Data channel full, dropping message")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Println("Reader canceled by context")
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
if c == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
|
||||||
log.Printf("error on read deadline: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
messageType, data, err := c.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error on read message: %v\n", err)
|
|
||||||
wsClient.triggerReconnect()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if messageType != websocket.TextMessage {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case wsClient.dataChannel <- data:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
|
||||||
ticker := time.NewTicker(pingPeriod)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Println("ping ticker canceled by context")
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
wsClient.writeChan <- Message{
|
|
||||||
MessageType: websocket.PingMessage,
|
|
||||||
Data: []byte{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
|
||||||
select {
|
|
||||||
case wsClient.reconnectCh <- struct{}{}:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
||||||
backoff := 1 * time.Second
|
backoff := 1 * time.Second
|
||||||
maxBackoff := 30 * time.Second
|
maxBackoff := 15 * time.Second
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-wsClient.reconnectCh:
|
case <-wsClient.reconnectCh:
|
||||||
log.Println("Reconnect triggered")
|
log.Println("Reconnect triggered")
|
||||||
|
|
||||||
wsClient.mu.ReadHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
if wsClient.cancelFuncs != nil {
|
if wsClient.cancelFuncs != nil {
|
||||||
for _, cancel := range wsClient.cancelFuncs {
|
for _, cancel := range wsClient.cancelFuncs {
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
wsClient.cancelFuncs = nil
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
wsClient.isConnected = false
|
wsClient.isConnected = false
|
||||||
|
|
||||||
isInnerLoop := true
|
isInnerLoop := true
|
||||||
for isInnerLoop {
|
for isInnerLoop {
|
||||||
log.Printf("Attempting reconnect in %v...", backoff)
|
log.Printf("Attempting reconnect in %v...", backoff)
|
||||||
@@ -410,8 +299,12 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|||||||
}
|
}
|
||||||
if wsClient.reconnectChans != nil {
|
if wsClient.reconnectChans != nil {
|
||||||
for _, reconnectCh := range wsClient.reconnectChans {
|
for _, reconnectCh := range wsClient.reconnectChans {
|
||||||
reconnectCh <- struct{}{}
|
select {
|
||||||
|
case reconnectCh <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
wsClient.reconnectChans = nil
|
||||||
}
|
}
|
||||||
case <-wsClient.ctx.Done():
|
case <-wsClient.ctx.Done():
|
||||||
log.Println("reconnect handler stopped due to client shutdown")
|
log.Println("reconnect handler stopped due to client shutdown")
|
||||||
@@ -421,6 +314,106 @@ func (wsClient *SafeWebsocketClient) reconnectHandler() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wsClient *SafeWebsocketClient) writePump(ctx context.Context, c *websocket.Conn) {
|
||||||
|
defer func() {
|
||||||
|
c.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("Writer canceled by context")
|
||||||
|
return
|
||||||
|
case data := <-wsClient.writeChan:
|
||||||
|
if err := c.SetWriteDeadline(time.Now().Add(writeDeadline)); err != nil {
|
||||||
|
log.Printf("error setting write deadline: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.WriteMessage(int(data.MessageType), data.Data); err != nil {
|
||||||
|
log.Printf("error on write message: %v\n", err)
|
||||||
|
wsClient.triggerReconnect() // Trigger reconnect on write failure
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wsClient *SafeWebsocketClient) readPump(ctx context.Context, c *websocket.Conn) {
|
||||||
|
canceled := false
|
||||||
|
defer func() {
|
||||||
|
if !canceled {
|
||||||
|
wsClient.triggerReconnect()
|
||||||
|
}
|
||||||
|
c.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("Reader canceled by context")
|
||||||
|
canceled = true
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
// Set read deadline
|
||||||
|
if err := c.SetReadDeadline(time.Now().Add(readDeadline)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
messageType, data, err := c.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error on read message: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if messageType != websocket.TextMessage {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case wsClient.dataChannel <- data:
|
||||||
|
case <-ctx.Done():
|
||||||
|
canceled = true
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
if wsClient.isDrop {
|
||||||
|
log.Println("Data channel full, dropping message")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wsClient *SafeWebsocketClient) startPingTicker(ctx context.Context) {
|
||||||
|
ticker := time.NewTicker(pingPeriod)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("ping ticker canceled by context")
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
select {
|
||||||
|
case wsClient.writeChan <- Message{
|
||||||
|
MessageType: websocket.PingMessage,
|
||||||
|
Data: []byte{},
|
||||||
|
}:
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Println("ping ticker canceled by context")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wsClient *SafeWebsocketClient) triggerReconnect() {
|
||||||
|
select {
|
||||||
|
case wsClient.reconnectCh <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) ReconnectChannel() <-chan struct{} {
|
func (wsClient *SafeWebsocketClient) ReconnectChannel() <-chan struct{} {
|
||||||
reconnectCh := make(chan struct{}, 1)
|
reconnectCh := make(chan struct{}, 1)
|
||||||
wsClient.mu.WriteHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
@@ -436,7 +429,9 @@ func (wsClient *SafeWebsocketClient) DataChannel() <-chan []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) CloseDataChannel() {
|
func (wsClient *SafeWebsocketClient) CloseDataChannel() {
|
||||||
close(wsClient.dataChannel)
|
wsClient.dataChannelOnce.Do(func() {
|
||||||
|
close(wsClient.dataChannel)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) Write(data []byte) error {
|
func (wsClient *SafeWebsocketClient) Write(data []byte) error {
|
||||||
@@ -448,7 +443,7 @@ func (wsClient *SafeWebsocketClient) Write(data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wsClient *SafeWebsocketClient) Close() error {
|
func (wsClient *SafeWebsocketClient) Close() error {
|
||||||
wsClient.mu.ReadHandler(func() error {
|
wsClient.mu.WriteHandler(func() error {
|
||||||
if wsClient.cancelFuncs != nil {
|
if wsClient.cancelFuncs != nil {
|
||||||
for _, cancel := range wsClient.cancelFuncs {
|
for _, cancel := range wsClient.cancelFuncs {
|
||||||
cancel()
|
cancel()
|
||||||
@@ -466,7 +461,10 @@ func (wsClient *SafeWebsocketClient) Close() error {
|
|||||||
wsClient.conn.Close()
|
wsClient.conn.Close()
|
||||||
}
|
}
|
||||||
wsClient.isConnected = false
|
wsClient.isConnected = false
|
||||||
close(wsClient.dataChannel)
|
|
||||||
|
wsClient.dataChannelOnce.Do(func() {
|
||||||
|
close(wsClient.dataChannel)
|
||||||
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,18 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
|
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go func() {
|
||||||
|
log.Println("Starting pprof server on :6060")
|
||||||
|
log.Println(http.ListenAndServe(":6060", nil))
|
||||||
|
}()
|
||||||
|
|
||||||
sigChan := make(chan os.Signal, 1)
|
sigChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
@@ -27,7 +35,9 @@ func main() {
|
|||||||
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
||||||
BaseHost("localhost").
|
BaseHost("localhost").
|
||||||
BasePort(8080).
|
BasePort(8080).
|
||||||
Path("/ws/test/data_1").
|
Headers(map[string]string{
|
||||||
|
"X-MBX-APIKEY": "abcd",
|
||||||
|
}).Path("/ws/test/data_1").
|
||||||
UseTLS(false).
|
UseTLS(false).
|
||||||
ChannelSize(1).
|
ChannelSize(1).
|
||||||
Build(ctx)
|
Build(ctx)
|
||||||
@@ -44,7 +54,7 @@ func main() {
|
|||||||
dataChannel := wsClient.DataChannel()
|
dataChannel := wsClient.DataChannel()
|
||||||
|
|
||||||
for data := range dataChannel {
|
for data := range dataChannel {
|
||||||
// _ = data
|
_ = data
|
||||||
fmt.Println(string(data))
|
// fmt.Println(string(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/server"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/server"
|
||||||
@@ -14,34 +18,66 @@ type ExampleData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
sigChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-sigChan
|
||||||
|
log.Println("Received shutdown signal")
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
|
||||||
s, err := server.NewSafeWebsocketServerBuilder().
|
s, err := server.NewSafeWebsocketServerBuilder().
|
||||||
BaseHost("localhost").
|
BaseHost("localhost").
|
||||||
BasePort(8080).
|
BasePort(8080).
|
||||||
ApiKey("").
|
ApiKey("abcd").
|
||||||
HandleFuncWebsocket("/ws/test/", "data_1", func(c chan []byte) {
|
Context(ctx).
|
||||||
ticker := time.NewTicker(10 * time.Millisecond)
|
HandleFuncWebsocket("/ws/test/", "data_1", 5_000, func(ctx context.Context, c chan []byte) {
|
||||||
for range ticker.C {
|
ticker := time.NewTicker(100 * time.Millisecond)
|
||||||
jsonBytes, err := json.Marshal(ExampleData{
|
defer ticker.Stop()
|
||||||
Time: time.Now(),
|
for {
|
||||||
Data: "data_1",
|
select {
|
||||||
})
|
case <-ctx.Done():
|
||||||
if err != nil {
|
return
|
||||||
continue
|
case <-ticker.C:
|
||||||
|
jsonBytes, err := json.Marshal(ExampleData{
|
||||||
|
Time: time.Now(),
|
||||||
|
Data: "data_1",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case c <- jsonBytes:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c <- jsonBytes
|
|
||||||
}
|
}
|
||||||
}).
|
}).
|
||||||
HandleFuncWebsocket("/ws/test/", "data_2", func(c chan []byte) {
|
HandleFuncWebsocket("/ws/test/", "data_2", 5_000, func(ctx context.Context, c chan []byte) {
|
||||||
ticker := time.NewTicker(10 * time.Millisecond)
|
ticker := time.NewTicker(100 * time.Millisecond)
|
||||||
for range ticker.C {
|
defer ticker.Stop()
|
||||||
jsonBytes, err := json.Marshal(ExampleData{
|
for {
|
||||||
Time: time.Now(),
|
select {
|
||||||
Data: "data_2",
|
case <-ctx.Done():
|
||||||
})
|
return
|
||||||
if err != nil {
|
case <-ticker.C:
|
||||||
continue
|
jsonBytes, err := json.Marshal(ExampleData{
|
||||||
|
Time: time.Now(),
|
||||||
|
Data: "data_2",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case c <- jsonBytes:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c <- jsonBytes
|
|
||||||
}
|
}
|
||||||
}).
|
}).
|
||||||
Build()
|
Build()
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/subtle"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
"git.neurocipta.com/rogerferdinan/safe-web-socket/internal"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@@ -28,6 +31,7 @@ type SafeWebsocketServerBuilder struct {
|
|||||||
apiKey *string `nil_checker:"required"`
|
apiKey *string `nil_checker:"required"`
|
||||||
upgrader *websocket.Upgrader `nil_checker:"required"`
|
upgrader *websocket.Upgrader `nil_checker:"required"`
|
||||||
mux *http.ServeMux `nil_checker:"required"`
|
mux *http.ServeMux `nil_checker:"required"`
|
||||||
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSafeWebsocketServerBuilder() *SafeWebsocketServerBuilder {
|
func NewSafeWebsocketServerBuilder() *SafeWebsocketServerBuilder {
|
||||||
@@ -40,6 +44,7 @@ func NewSafeWebsocketServerBuilder() *SafeWebsocketServerBuilder {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
|
ctx: context.Background(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,14 +63,26 @@ func (b *SafeWebsocketServerBuilder) ApiKey(apiKey string) *SafeWebsocketServerB
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Context sets the lifecycle context for all hub and writeFunc goroutines.
|
||||||
|
// When ctx is cancelled, hubs stop dispatching and all active connections
|
||||||
|
// are unblocked so their goroutines can exit cleanly.
|
||||||
|
// Call this before HandleFuncWebsocket. Defaults to context.Background().
|
||||||
|
func (b *SafeWebsocketServerBuilder) Context(ctx context.Context) *SafeWebsocketServerBuilder {
|
||||||
|
b.ctx = ctx
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketServerBuilder) HandleFunc(pattern string, fn func(http.ResponseWriter, *http.Request)) *SafeWebsocketServerBuilder {
|
func (b *SafeWebsocketServerBuilder) HandleFunc(pattern string, fn func(http.ResponseWriter, *http.Request)) *SafeWebsocketServerBuilder {
|
||||||
b.mux.HandleFunc(pattern, fn)
|
b.mux.HandleFunc(pattern, fn)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *SafeWebsocketServerBuilder) HandleFuncWebsocket(pattern string, subscribedPath string, writeFunc func(chan []byte)) *SafeWebsocketServerBuilder {
|
// HandleFuncWebsocket registers a WebSocket endpoint.
|
||||||
h := internal.NewHub()
|
func (b *SafeWebsocketServerBuilder) HandleFuncWebsocket(pattern string, subscribedPath string, maxClients int, writeFunc func(ctx context.Context, writeChannel chan []byte)) *SafeWebsocketServerBuilder {
|
||||||
h.Run()
|
h := internal.NewHub(pattern+subscribedPath, maxClients)
|
||||||
|
h.Run(b.ctx)
|
||||||
|
|
||||||
|
go writeFunc(b.ctx, h.Broadcast)
|
||||||
|
|
||||||
b.mux.HandleFunc(pattern+subscribedPath, func(w http.ResponseWriter, r *http.Request) {
|
b.mux.HandleFunc(pattern+subscribedPath, func(w http.ResponseWriter, r *http.Request) {
|
||||||
conn, err := b.upgrader.Upgrade(w, r, nil)
|
conn, err := b.upgrader.Upgrade(w, r, nil)
|
||||||
@@ -81,9 +98,9 @@ func (b *SafeWebsocketServerBuilder) HandleFuncWebsocket(pattern string, subscri
|
|||||||
}
|
}
|
||||||
c := internal.NewClient(conn, subscribedPath)
|
c := internal.NewClient(conn, subscribedPath)
|
||||||
h.Register <- c
|
h.Register <- c
|
||||||
|
|
||||||
go internal.WritePump(c, h)
|
go internal.WritePump(c, h)
|
||||||
go internal.ReadPump(c, h)
|
go internal.ReadPump(c, h)
|
||||||
go writeFunc(h.Broadcast)
|
|
||||||
})
|
})
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
@@ -98,6 +115,7 @@ func (b *SafeWebsocketServerBuilder) Build() (*SafeWebsocketServer, error) {
|
|||||||
mux: b.mux,
|
mux: b.mux,
|
||||||
url: fmt.Sprintf("%s:%d", *b.baseHost, *b.basePort),
|
url: fmt.Sprintf("%s:%d", *b.baseHost, *b.basePort),
|
||||||
apiKey: *b.apiKey,
|
apiKey: *b.apiKey,
|
||||||
|
ctx: b.ctx,
|
||||||
}
|
}
|
||||||
return &safeServer, nil
|
return &safeServer, nil
|
||||||
}
|
}
|
||||||
@@ -106,24 +124,51 @@ type SafeWebsocketServer struct {
|
|||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
url string
|
url string
|
||||||
apiKey string
|
apiKey string
|
||||||
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SafeWebsocketServer) AuthMiddleware(next http.Handler) http.Handler {
|
func (s *SafeWebsocketServer) AuthMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Header.Get("X-MBX-APIKEY") != s.apiKey {
|
providedKey := r.Header.Get("X-MBX-APIKEY")
|
||||||
|
expectedKey := s.apiKey
|
||||||
|
|
||||||
|
if subtle.ConstantTimeCompare([]byte(providedKey), []byte(expectedKey)) != 1 {
|
||||||
internal.ErrorResponse(w, internal.NewStatusMessage().
|
internal.ErrorResponse(w, internal.NewStatusMessage().
|
||||||
StatusCode(http.StatusForbidden).
|
StatusCode(http.StatusForbidden).
|
||||||
Message("X-MBX-APIKEY is missing").
|
Message("X-MBX-APIKEY is missing").
|
||||||
Build())
|
Build())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SafeWebsocketServer) ListenAndServe() error {
|
func (s *SafeWebsocketServer) ListenAndServe() error {
|
||||||
log.Printf("HTTP serve on %s\n", s.url)
|
srv := &http.Server{
|
||||||
if err := http.ListenAndServe(s.url, s.AuthMiddleware(s.mux)); err != nil {
|
Addr: s.url,
|
||||||
return fmt.Errorf("failed to serve websocket: %w", err)
|
Handler: s.AuthMiddleware(s.mux),
|
||||||
|
}
|
||||||
|
|
||||||
|
errCh := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
log.Printf("HTTP serve on %s\n", s.url)
|
||||||
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
errCh <- fmt.Errorf("failed to serve websocket: %w", err)
|
||||||
|
}
|
||||||
|
close(errCh)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-errCh:
|
||||||
|
return err
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
log.Println("Server shutting down...")
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||||
|
return fmt.Errorf("server shutdown: %w", err)
|
||||||
|
}
|
||||||
|
return <-errCh
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user