-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (108 loc) · 2.94 KB
/
main.go
File metadata and controls
125 lines (108 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// main.go
// Package main provides a demonstration of the memorystore package functionality.
package main
import (
"log"
"os"
"sync"
"time"
"github.com/BryceWayne/MemoryStore/memorystore"
"github.com/google/uuid"
)
// Person represents a sample data structure.
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
UID string `json:"uid"`
Created time.Time `json:"created,omitempty"`
}
// demonstrateBasicOperations shows basic Set/Get operations.
func demonstrateBasicOperations(ms *memorystore.MemoryStore) {
log.Println("=== Demonstrating Basic Operations ===")
person := Person{
Name: "Alice Smith",
Age: 30,
UID: uuid.New().String(),
Created: time.Now(),
}
err := ms.SetJSON(person.UID, person, 2*time.Second)
if err != nil {
log.Printf("Failed to store person: %v", err)
return
}
log.Printf("Stored person with UID: %s", person.UID)
var retrievedPerson Person
exists, err := ms.GetJSON(person.UID, &retrievedPerson)
if err != nil {
log.Printf("Error retrieving person: %v", err)
return
}
if exists {
log.Printf("Retrieved person: %+v", retrievedPerson)
}
}
// demonstratePubSub shows the publish/subscribe functionality.
// Note: We use exact topic names to ensure compatibility with GCP PubSub.
func demonstratePubSub(ms *memorystore.MemoryStore) {
log.Println("\n=== Demonstrating PubSub System ===")
var wg sync.WaitGroup
topics := []string{"updates", "alerts"}
chans := make(map[string]<-chan []byte)
// Subscribe
for _, topic := range topics {
ch, err := ms.Subscribe(topic)
if err != nil {
log.Printf("Failed to subscribe to %s: %v", topic, err)
continue
}
chans[topic] = ch
log.Printf("Subscribed to topic: %s", topic)
}
// Listeners
for topic, ch := range chans {
wg.Add(1)
go func(t string, c <-chan []byte) {
defer wg.Done()
for {
select {
case msg, ok := <-c:
if !ok {
return
}
log.Printf("[%s] Received: %s", t, string(msg))
case <-time.After(3 * time.Second):
return
}
}
}(topic, ch)
}
// Publish
time.Sleep(500 * time.Millisecond) // Wait for subscriptions
if err := ms.Publish("updates", []byte("System update available")); err != nil {
log.Printf("Error publishing to updates: %v", err)
}
if err := ms.Publish("alerts", []byte("High CPU usage")); err != nil {
log.Printf("Error publishing to alerts: %v", err)
}
wg.Wait()
// Unsubscribe
for _, topic := range topics {
if err := ms.Unsubscribe(topic); err != nil {
log.Printf("Error unsubscribing from %s: %v", topic, err)
}
}
}
func main() {
// Example of using GCP PubSub if env var is set
if os.Getenv("GOOGLE_CLOUD_PROJECT") == "" {
log.Println("Note: Set GOOGLE_CLOUD_PROJECT env var to test GCP PubSub backend")
}
ms := memorystore.NewMemoryStore()
defer func() {
if err := ms.Stop(); err != nil {
log.Printf("Error stopping store: %v", err)
}
}()
demonstrateBasicOperations(ms)
demonstratePubSub(ms)
}