This commit is contained in:
matst80
2024-11-08 23:50:57 +01:00
parent 251a0b7bc7
commit 356f5effba
5 changed files with 44 additions and 94 deletions

View File

@@ -1,19 +1,20 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"time" "time"
) )
type GrainPool interface { type GrainPool interface {
Process(id CartId, messages ...Message) (interface{}, error) Process(id CartId, messages ...Message) ([]byte, error)
Get(id CartId) (Grain, error) Get(id CartId) ([]byte, error)
} }
type Ttl struct { type Ttl struct {
Expires time.Time Expires time.Time
Item *CartGrain Grain *CartGrain
} }
type GrainLocalPool struct { type GrainLocalPool struct {
@@ -46,13 +47,13 @@ func (p *GrainLocalPool) Purge() {
for i := 0; i < len(p.expiry); i++ { for i := 0; i < len(p.expiry); i++ {
item := p.expiry[i] item := p.expiry[i]
if item.Expires.Before(time.Now()) { if item.Expires.Before(time.Now()) {
if item.Item.GetLastChange() > keepChanged { if item.Grain.GetLastChange() > keepChanged {
log.Printf("Changed item %s expired, keeping", item.Item.GetId()) log.Printf("Expired item %s changed, keeping", item.Grain.GetId())
p.expiry = append(p.expiry[:i], p.expiry[i+1:]...) p.expiry = append(p.expiry[:i], p.expiry[i+1:]...)
p.expiry = append(p.expiry, item) p.expiry = append(p.expiry, item)
} else { } else {
log.Printf("Item %s expired", item.Item.GetId()) log.Printf("Item %s expired", item.Grain.GetId())
delete(p.grains, item.Item.GetId()) delete(p.grains, item.Grain.GetId())
p.expiry = append(p.expiry[:i], p.expiry[i+1:]...) p.expiry = append(p.expiry[:i], p.expiry[i+1:]...)
} }
} else { } else {
@@ -71,7 +72,7 @@ func (p *GrainLocalPool) GetGrain(id CartId) (*CartGrain, error) {
if !ok { if !ok {
if len(p.grains) >= p.PoolSize { if len(p.grains) >= p.PoolSize {
if p.expiry[0].Expires.Before(time.Now()) { if p.expiry[0].Expires.Before(time.Now()) {
delete(p.grains, p.expiry[0].Item.GetId()) delete(p.grains, p.expiry[0].Grain.GetId())
p.expiry = p.expiry[1:] p.expiry = p.expiry[1:]
} else { } else {
return nil, fmt.Errorf("pool is full") return nil, fmt.Errorf("pool is full")
@@ -84,16 +85,23 @@ func (p *GrainLocalPool) GetGrain(id CartId) (*CartGrain, error) {
return grain, err return grain, err
} }
func (p *GrainLocalPool) Process(id CartId, messages ...Message) (interface{}, error) { func (p *GrainLocalPool) Process(id CartId, messages ...Message) ([]byte, error) {
grain, err := p.GetGrain(id) grain, err := p.GetGrain(id)
if err == nil && grain != nil { if err == nil && grain != nil {
for _, message := range messages { for _, message := range messages {
_, err = grain.HandleMessage(&message, false) _, err = grain.HandleMessage(&message, false)
} }
} }
return grain, err if err != nil {
return nil, err
}
return json.Marshal(grain)
} }
func (p *GrainLocalPool) Get(id CartId) (Grain, error) { func (p *GrainLocalPool) Get(id CartId) ([]byte, error) {
return p.GetGrain(id) grain, err := p.GetGrain(id)
if err != nil {
return nil, err
}
return json.Marshal(grain)
} }

View File

@@ -1,23 +0,0 @@
package main
import (
"fmt"
"net"
"net/rpc"
)
type GrainServer struct {
Host string
}
func NewServer(hostname string) *GrainServer {
return &GrainServer{
Host: hostname,
}
}
func (s *GrainServer) Start(port int, instance Grain) (net.Listener, error) {
rpc.Register(instance)
rpc.HandleHTTP()
return net.Listen("tcp", fmt.Sprintf(":%d", port))
}

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"encoding/binary" "encoding/binary"
"encoding/json"
"io" "io"
) )
@@ -53,6 +54,17 @@ func SendPacket(conn io.Writer, messageType uint16, datafn func(w io.Writer) err
return err return err
} }
func SendProxyResponse(conn io.Writer, data any) error {
return SendPacket(conn, ResponseBody, func(w io.Writer) error {
data, err := json.Marshal(data)
if err != nil {
return err
}
w.Write(data)
return nil
})
}
// func ReceiveCartPacket(conn io.Reader) (CartPacket, []byte, error) { // func ReceiveCartPacket(conn io.Reader) (CartPacket, []byte, error) {
// var packet CartPacket // var packet CartPacket
// err := binary.Read(conn, binary.LittleEndian, &packet) // err := binary.Read(conn, binary.LittleEndian, &packet)

View File

@@ -2,7 +2,6 @@ package main
import ( import (
"encoding/binary" "encoding/binary"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net" "net"
@@ -10,11 +9,11 @@ import (
type GrainHandler struct { type GrainHandler struct {
listener net.Listener listener net.Listener
pool GrainPool pool *GrainLocalPool
} }
func (h *GrainHandler) GetState(id CartId, reply *Grain) error { func (h *GrainHandler) GetState(id CartId, reply *Grain) error {
grain, err := h.pool.Get(id) grain, err := h.pool.GetGrain(id)
if err != nil { if err != nil {
return err return err
} }
@@ -22,7 +21,7 @@ func (h *GrainHandler) GetState(id CartId, reply *Grain) error {
return nil return nil
} }
func NewGrainHandler(pool GrainPool, listen string) (*GrainHandler, error) { func NewGrainHandler(pool *GrainLocalPool, listen string) (*GrainHandler, error) {
handler := &GrainHandler{ handler := &GrainHandler{
pool: pool, pool: pool,
} }
@@ -33,33 +32,30 @@ func NewGrainHandler(pool GrainPool, listen string) (*GrainHandler, error) {
func (h *GrainHandler) Serve() { func (h *GrainHandler) Serve() {
for { for {
// Accept incoming connections
conn, err := h.listener.Accept() conn, err := h.listener.Accept()
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Println("Error accepting connection:", err)
continue continue
} }
// Handle client connection in a goroutine
go h.handleClient(conn) go h.handleClient(conn)
} }
} }
func (h *GrainHandler) handleClient(conn net.Conn) { func (h *GrainHandler) handleClient(conn net.Conn) {
var err error
fmt.Println("Handling client connection") fmt.Println("Handling client connection")
defer conn.Close() defer conn.Close()
var packet CartPacket var packet CartPacket
for { for {
for { for {
err := binary.Read(conn, binary.LittleEndian, &packet) err = binary.Read(conn, binary.LittleEndian, &packet)
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
break break
} }
fmt.Println("Error reading packet:", err) fmt.Println("Error in connection:", err)
} }
if packet.Version != 2 { if packet.Version != 2 {
fmt.Printf("Unknown version %d", packet.Version) fmt.Printf("Unknown version %d", packet.Version)
@@ -70,42 +66,24 @@ func (h *GrainHandler) handleClient(conn net.Conn) {
case RemoteHandleMessage: case RemoteHandleMessage:
fmt.Printf("Handling message\n") fmt.Printf("Handling message\n")
var msg Message var msg Message
err := MessageFromReader(conn, &msg) err = MessageFromReader(conn, &msg)
if err != nil { if err != nil {
fmt.Println("Error reading message:", err) fmt.Println("Error reading message:", err)
} }
fmt.Printf("Message: %s, %v\n", packet.Id.String(), msg) fmt.Printf("Message: %s, %v\n", packet.Id.String(), msg)
grain, err := h.pool.Get(packet.Id) grain, err := h.pool.Process(packet.Id, msg)
if err != nil {
fmt.Println("Error getting grain:", err)
}
_, err = grain.HandleMessage(&msg, false)
if err != nil { if err != nil {
fmt.Println("Error handling message:", err) fmt.Println("Error handling message:", err)
} }
SendPacket(conn, ResponseBody, func(w io.Writer) error { SendProxyResponse(conn, grain)
data, err := json.Marshal(grain)
if err != nil {
return err
}
w.Write(data)
return nil
})
case RemoteGetState:
case RemoteGetState:
fmt.Printf("Package: %s %v\n", packet.Id.String(), packet) fmt.Printf("Package: %s %v\n", packet.Id.String(), packet)
grain, err := h.pool.Get(packet.Id) grain, err := h.pool.Get(packet.Id)
if err != nil { if err != nil {
fmt.Println("Error getting grain:", err) fmt.Println("Error getting grain:", err)
} }
SendPacket(conn, ResponseBody, func(w io.Writer) error { SendProxyResponse(conn, grain)
data, err := json.Marshal(grain)
if err != nil {
return err
}
w.Write(data)
return nil
})
} }
} }

View File

@@ -1,25 +0,0 @@
package main
import "fmt"
type Registry interface {
Register(address string, id string) error
Get(id string) (*string, error)
}
type MemoryRegistry struct {
registry map[string]string
}
func (r *MemoryRegistry) Register(address string, id string) error {
r.registry[id] = address
return nil
}
func (r *MemoryRegistry) Get(id string) (*string, error) {
addr, ok := r.registry[id]
if !ok {
return nil, fmt.Errorf("id not found")
}
return &addr, nil
}