Update dependencies, added SSE Helpers, and optimized Redis client connections
This commit is contained in:
parent
741e6e7b8e
commit
9c3e3b5ab7
4 changed files with 58 additions and 2 deletions
2
go.mod
2
go.mod
|
@ -35,7 +35,7 @@ require (
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/redis/go-redis/v9 v9.4.0
|
github.com/redis/go-redis/v9 v9.4.0
|
||||||
github.com/resendlabs/resend-go v1.7.0
|
github.com/resendlabs/resend-go v1.7.0
|
||||||
github.com/yuin/goldmark v1.6.0
|
github.com/yuin/goldmark v1.7.0
|
||||||
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
|
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
|
||||||
golang.org/x/net v0.20.0 // indirect
|
golang.org/x/net v0.20.0 // indirect
|
||||||
golang.org/x/sys v0.16.0 // indirect
|
golang.org/x/sys v0.16.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -65,6 +65,8 @@ github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+
|
||||||
github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
|
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
|
||||||
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
|
||||||
|
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||||
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ=
|
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ=
|
||||||
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I=
|
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I=
|
||||||
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
||||||
|
|
|
@ -14,16 +14,22 @@ var ctx = context.Background()
|
||||||
var RedisClient *redis.Client
|
var RedisClient *redis.Client
|
||||||
|
|
||||||
func NewClient() *redis.Client {
|
func NewClient() *redis.Client {
|
||||||
|
if RedisClient != nil {
|
||||||
|
return RedisClient
|
||||||
|
}
|
||||||
|
|
||||||
godotenv.Load(".env")
|
godotenv.Load(".env")
|
||||||
redis_host := os.Getenv("REDIS_HOST")
|
redis_host := os.Getenv("REDIS_HOST")
|
||||||
redis_password := os.Getenv("REDIS_PASSWORD")
|
redis_password := os.Getenv("REDIS_PASSWORD")
|
||||||
|
|
||||||
log.Printf("Connecting to Redis at %s", redis_host)
|
log.Printf("Connecting to Redis at %s", redis_host)
|
||||||
return redis.NewClient(&redis.Options{
|
RedisClient = redis.NewClient(&redis.Options{
|
||||||
Addr: redis_host,
|
Addr: redis_host,
|
||||||
Password: redis_password,
|
Password: redis_password,
|
||||||
DB: 0,
|
DB: 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return RedisClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func Publish(client *redis.Client, channel string, message string) error {
|
func Publish(client *redis.Client, channel string, message string) error {
|
||||||
|
|
48
lib/sse.go
48
lib/sse.go
|
@ -1,5 +1,53 @@
|
||||||
package lib
|
package lib
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type SSEServerType struct {
|
||||||
|
clients map[string]map[chan string]bool
|
||||||
|
mu sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
var SSEServer *SSEServerType
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
SSEServer = &SSEServerType{
|
||||||
|
clients: make(map[string]map[chan string]bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSSEServer() *SSEServerType {
|
||||||
|
return &SSEServerType{
|
||||||
|
clients: make(map[string]map[chan string]bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SSEServerType) AddClient(channel string, client chan string) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
if _, ok := s.clients[channel]; !ok {
|
||||||
|
s.clients[channel] = make(map[chan string]bool)
|
||||||
|
}
|
||||||
|
s.clients[channel][client] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SSEServerType) RemoveClient(channel string, client chan string) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
delete(s.clients[channel], client)
|
||||||
|
if len(s.clients[channel]) == 0 {
|
||||||
|
delete(s.clients, channel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SSEServerType) ClientCount(channel string) int {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
return len(s.clients[channel])
|
||||||
|
}
|
||||||
|
|
||||||
func SendSSE(channel string, message string) error {
|
func SendSSE(channel string, message string) error {
|
||||||
// Create a channel to receive an error from the goroutine
|
// Create a channel to receive an error from the goroutine
|
||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
|
|
Loading…
Add table
Reference in a new issue