-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.go
More file actions
139 lines (119 loc) · 2.93 KB
/
cache.go
File metadata and controls
139 lines (119 loc) · 2.93 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
// Domain types
type User struct {
ID string \`json:"id"\`
Name string \`json:"name"\`
Email string \`json:"email"\`
Role string \`json:"role"\`
CreatedAt time.Time \`json:"created_at"\`
}
type APIResponse struct {
Success bool \`json:"success"\`
Data interface{} \`json:"data,omitempty"\`
Error string \`json:"error,omitempty"\`
}
// Thread-safe store
type Store struct {
mu sync.RWMutex
users map[string]*User
}
func NewStore() *Store {
return &Store{users: make(map[string]*User)}
}
func (s *Store) Get(id string) (*User, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
u, ok := s.users[id]
return u, ok
}
func (s *Store) List() []*User {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]*User, 0, len(s.users))
for _, u := range s.users {
result = append(result, u)
}
return result
}
func (s *Store) Create(u *User) {
s.mu.Lock()
defer s.mu.Unlock()
u.CreatedAt = time.Now()
s.users[u.ID] = u
}
// Background worker with channels
func startWorker(ctx context.Context, jobs <-chan string, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case <-ctx.Done():
log.Println("Worker shutting down")
return
case job, ok := <-jobs:
if !ok {
return
}
log.Printf("Processing job: %s", job)
time.Sleep(100 * time.Millisecond)
}
}
}
// HTTP handlers
func jsonResponse(w http.ResponseWriter, status int, resp APIResponse) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(resp)
}
func main() {
store := NewStore()
store.Create(&User{ID: "1", Name: "Alice", Email: "alice@example.com", Role: "admin"})
store.Create(&User{ID: "2", Name: "Bob", Email: "bob@example.com", Role: "user"})
// Start background workers
ctx, cancel := context.WithCancel(context.Background())
jobs := make(chan string, 100)
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go startWorker(ctx, jobs, &wg)
}
mux := http.NewServeMux()
mux.HandleFunc("GET /api/users", func(w http.ResponseWriter, r *http.Request) {
jsonResponse(w, http.StatusOK, APIResponse{Success: true, Data: store.List()})
})
mux.HandleFunc("GET /api/users/{id}", func(w http.ResponseWriter, r *http.Request) {
user, ok := store.Get(r.PathValue("id"))
if !ok {
jsonResponse(w, 404, APIResponse{Error: "not found"})
return
}
jsonResponse(w, 200, APIResponse{Success: true, Data: user})
})
srv := &http.Server{Addr: ":8080", Handler: mux}
// Graceful shutdown
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
log.Println("Shutting down...")
cancel()
close(jobs)
wg.Wait()
srv.Shutdown(context.Background())
}()
fmt.Println("Server listening on :8080")
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal(err)
}
}