Debian Codegraph @

internal/web/broadcastmanager.go

raw symbols SCIP
1 package web
2
3 import (
4 "log"
5 "sync"
6
7 "github.com/d-Rickyy-b/certstream-server-go/internal/metrics"
8 "github.com/d-Rickyy-b/certstream-server-go/internal/models"
9 )
10
11 type BroadcastManager struct {
12 Broadcast chan models.Entry
13 clients []*client
14 clientLock sync.RWMutex
15 }
16
17 func NewBroadcastManager() *BroadcastManager {
18 bm := &BroadcastManager{}
19 metrics.Prometheus.RegisterGaugeMetricInt("certstreamservergo_clients_total{type=\"full\"}", bm.ClientFullCount)
20 metrics.Prometheus.RegisterGaugeMetricInt("certstreamservergo_clients_total{type=\"lite\"}", bm.ClientLiteCount)
21 metrics.Prometheus.RegisterGaugeMetricInt("certstreamservergo_clients_total{type=\"domain\"}", bm.ClientDomainsCount)
22 return bm
23 }
24
25 // registerClient adds a client to the list of clients of the BroadcastManager.
26 // The client will receive certificate broadcasts right after registration.
27 func (bm *BroadcastManager) registerClient(c *client) {
28 bm.clientLock.Lock()
29 bm.clients = append(bm.clients, c)
30 log.Printf("Clients: %d, Capacity: %d\n", len(bm.clients), cap(bm.clients))
31 metrics.Prometheus.RegisterClient(c.name, func() float64 { return float64(c.skippedCerts) })
32 bm.clientLock.Unlock()
33 }
34
35 // unregisterClient removes a client from the list of clients of the BroadcastManager.
36 // The client will no longer receive certificate broadcasts right after unregistering.
37 func (bm *BroadcastManager) unregisterClient(c *client) {
38 bm.clientLock.Lock()
39
40 for i, client := range bm.clients {
41 if c == client {
42 // Copy the last element of the slice to the position of the removed element
43 // Then remove the last element by re-slicing
44 bm.clients[i] = bm.clients[len(bm.clients)-1]
45 bm.clients[len(bm.clients)-1] = nil
46 bm.clients = bm.clients[:len(bm.clients)-1]
47
48 // Close the broadcast channel of the client, otherwise this leads to a memory leak
49 close(c.broadcastChan)
50
51 metrics.Prometheus.UnregisterClient(c.name)
52
53 break
54 }
55 }
56
57 bm.clientLock.Unlock()
58 }
59
60 // ClientFullCount returns the current number of clients connected to the service on the `full` endpoint.
61 func (bm *BroadcastManager) ClientFullCount() (count int64) {
62 return bm.clientCountByType(SubTypeFull)
63 }
64
65 // ClientLiteCount returns the current number of clients connected to the service on the `lite` endpoint.
66 func (bm *BroadcastManager) ClientLiteCount() (count int64) {
67 return bm.clientCountByType(SubTypeLite)
68 }
69
70 // ClientDomainsCount returns the current number of clients connected to the service on the `domains-only` endpoint.
71 func (bm *BroadcastManager) ClientDomainsCount() (count int64) {
72 return bm.clientCountByType(SubTypeDomain)
73 }
74
75 // clientCountByType returns the current number of clients connected to the service on the endpoint matching
76 // the specified SubscriptionType.
77 func (bm *BroadcastManager) clientCountByType(subType SubscriptionType) (count int64) {
78 bm.clientLock.RLock()
79 defer bm.clientLock.RUnlock()
80
81 for _, c := range bm.clients {
82 if c.subType == subType {
83 count++
84 }
85 }
86
87 return count
88 }
89
90 func (bm *BroadcastManager) GetSkippedCerts() map[string]uint64 {
91 bm.clientLock.RLock()
92 defer bm.clientLock.RUnlock()
93
94 skippedCerts := make(map[string]uint64, len(bm.clients))
95 for _, c := range bm.clients {
96 skippedCerts[c.name] = c.skippedCerts
97 }
98
99 return skippedCerts
100 }
101
102 // broadcaster is run in a goroutine and handles the dispatching of entries to clients.
103 func (bm *BroadcastManager) broadcaster() {
104 for {
105 var data []byte
106
107 entry := <-bm.Broadcast
108 dataLite := entry.JSONLite()
109 dataFull := entry.JSON()
110 dataDomain := entry.JSONDomains()
111
112 bm.clientLock.RLock()
113
114 for _, c := range bm.clients {
115 switch c.subType {
116 case SubTypeLite:
117 data = dataLite
118 case SubTypeFull:
119 data = dataFull
120 case SubTypeDomain:
121 data = dataDomain
122 default:
123 log.Printf("Unknown subscription type '%d' for client '%s'. Skipping this client!\n", c.subType, c.name)
124 continue
125 }
126
127 select {
128 case c.broadcastChan <- data:
129 default:
130 // Default case is executed if the client's broadcast channel is full.
131 c.skippedCerts++
132 if c.skippedCerts%1000 == 1 {
133 log.Printf("Not providing client '%s' with cert because client's buffer is full. The client can't keep up. Skipped certs: %d\n", c.name, c.skippedCerts)
134 }
135 }
136 }
137
138 bm.clientLock.RUnlock()
139 }
140 }