implement queue
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m47s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m47s
This commit is contained in:
31
rpc-pool.go
31
rpc-pool.go
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RemoteGrainPool struct {
|
||||
@@ -23,9 +24,10 @@ func ToCartId(id string) CartId {
|
||||
}
|
||||
|
||||
type RemoteGrain struct {
|
||||
client net.Conn
|
||||
Id CartId
|
||||
Address string
|
||||
connection net.Conn
|
||||
queue *PacketQueue
|
||||
Id CartId
|
||||
Address string
|
||||
}
|
||||
|
||||
func NewRemoteGrain(id CartId, address string) *RemoteGrain {
|
||||
@@ -36,23 +38,27 @@ func NewRemoteGrain(id CartId, address string) *RemoteGrain {
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) Connect() error {
|
||||
if g.client == nil {
|
||||
if g.connection == nil {
|
||||
client, err := net.Dial("tcp", g.Address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
g.client = client
|
||||
g.connection = client
|
||||
g.queue = NewPacketQueue(client)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) HandleMessage(message *Message, isReplay bool) ([]byte, error) {
|
||||
err := SendCartPacket(g.client, g.Id, RemoteHandleMessage, message.Write)
|
||||
err := SendCartPacket(g.connection, g.Id, RemoteHandleMessage, message.Write)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, data, err := ReceivePacket(g.client)
|
||||
return data, err
|
||||
packet, err := g.queue.Expect(ResponseBody, time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return packet.Data, err
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) GetId() CartId {
|
||||
@@ -60,14 +66,17 @@ func (g *RemoteGrain) GetId() CartId {
|
||||
}
|
||||
|
||||
func (g *RemoteGrain) GetCurrentState() ([]byte, error) {
|
||||
err := SendCartPacket(g.client, g.Id, RemoteGetState, func(w io.Writer) error {
|
||||
err := SendCartPacket(g.connection, g.Id, RemoteGetState, func(w io.Writer) error {
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, data, err := ReceivePacket(g.client)
|
||||
return data, err
|
||||
packet, err := g.queue.Expect(ResponseBody, time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return packet.Data, nil
|
||||
}
|
||||
|
||||
func NewRemoteGrainPool(addr string) *RemoteGrainPool {
|
||||
|
||||
Reference in New Issue
Block a user