Hello there.
I need to construct an internet utility and there are a number of rooms in a corridor that may maintain a sport. The issue is plainly every room needs to be unbiased to deal with their enterprise, which implies concurrency, however I don’t understand how this might be executed with RPC.
I don’t suppose it’s good to open one RPC service for every room, which implies utilizing a number of port.
But when I simply use one RPC service and parse the room id within the args, then name the room’s operate, the logic is sequential.
Is there any recommend? Thanks.
Hello, @Pierre_Ke, welcome to the Go discussion board.
What a part of your internet utility wants RPC? Are you able to do what you need simply with requests and responses and internet sockets?
But when I simply use one RPC service and parse the room id within the args, then name the room’s operate, the logic is sequential.
Why not simply service every request in a brand new gorountine?
That is how most servers in Go work.
1 Like
One possibility could also be to make use of a number of databases. Relying on what every room ought to do or retailer. One for every room. Add database rooms when wanted. Join direct, through REST API or RPC API.
Let’s say the room is for board video games. Gamers on this sport ought to select to play a card or do some actions in flip.
And what I’m doing now’s open a RPC Service and register a HALL
struct. On this HALL
struct, I’ve a rooms
slice. Now, when the a RPC request is coming, the args is like ‘{participant: Pierre; roomID: 1; motion: skip}’, the corridor will parse the roomID then name this room’s dealing with operate.
So what do you imply use a number of databases? I do know I can use Redis
’s SUBSCRIBE&PUBHSIH to construct a easy chat system for every room. However I don’t know the way it works with RPC?
Positive. It’s potential to make use of internet sockets, however the issue is ought to I open a socket for every room? Does it use a number of ports?
It’s my understanding that internet sockets work equally to the HTTP protocol the place a number of shoppers all join over the identical port 443 and the multiplexing is dealt with at decrease layers of the OSI mannequin. There’s extra info on the WebSocket – Wikipedia article, together with examples in JavaScript and details about, e.g. the handshake and different implementation particulars.
As I perceive it’s about how lengthy the participant knowledge will likely be saved. Will it’s persistent, per web page or per session? Or is there no want for storing? roomID signifies that there will likely be some type of storing. Which implies you want some storing – localStorage, REDIS or an actual RDMS.
RPC in it self does no storing. It’s a must to use Redis, Postgresql and so on to retailer “room knowledge”. RPC can solely hearth a operate or make a request to a different program for using its service.
Let’s make it extra particular. On this board sport, when it’s my flip, I select ‘use this card’ commend. In my design, I need to use RPC to tell server my selection. However I’m pondering, possibly I don’t want RPC on this course of? If I exploit REDIS and SUBSCRIBE a ‘user_id#operation’ channel and a ‘room_id#operation’ channel, shoppers ship this ‘use this card’ commend to ‘room_id#operation’ channel then ready reply from ‘user_id#operation’. Do you suppose this can be a good implementation?
There’s a time period in database design that is known as “multi-tenant”. You may make this by isolating every tenant (room?) by creating a brand new sql database for every room. This the perfect isolation method. However you may as well assign a worth to every tenant in the identical database. Less complicated however not that remoted. Utilized by many internet hosting corporations AFAIK. I suppose that that is what you’re going to do?
I can’t consider any purpose why that wouldn’t work. The one potential draw back I see is that this ties you to Redis, however internet sockets will not be particular to databases. I checked out golang.org/x/internet/websocket and watched this tutorial on YouTube (although it’s for websocket package deal – github.com/gorilla/websocket – Go Packages) and, personally, I believe I’d go this route if I have been implementing an identical sport:
- Go channels and, maybe, a single per-game goroutine inside the server to deal with the sport occasions:
- Recreation seems to be one thing like this:
sort Recreation struct { incoming chan *GameEvent outgoing []chan<- *GameState // no matter different sport state you want } // ... // Subscribe an internet socket shopper to the sport. c is a channel into which // updates to the sport state will likely be despatched by the Recreation for the shopper to // obtain from and replace the browser. The returned channel needs to be // utilized by the shopper to ship updates from the net shopper to the Recreation. func (g *Recreation) Subscribe(c <-chan *GameState) chan<- *GameEvent { ... } func (g *Recreation) run() { s := newInitialGameState() defer func() { for _, out := vary g.outgoing { shut(out) } }() for ev := vary g.incoming { s = s.createNewStateFromEvent(ev) for _, out := vary g.outgoing { out<- s } }
- Recreation seems to be one thing like this:
- WebSockets for interplay between customers’ browsers and the server:
- Server-side, for every internet socket, have one goroutine for a reader and one other for a author:
func createGameWebSocketHandler(g *Recreation) websocket.Handler { outgoingFromGame := make(chan *GameState) incomingToGame := g.Subscribe(outgoingFromGame) return func(ws *websocket.Conn) { go func() { for { ev := &GameEvent{} if err := websocket.JSON.Obtain(ws, ev); err != nil { wsErr(ws, err) return } incomingToGame <- ev } }() for st := vary outgoingFromGame { if err := websocket.JSON.Ship(ws, st); err != nil { wsErr(ws, err) return } } } }
- Server-side, for every internet socket, have one goroutine for a reader and one other for a author:
Hello skillian,
You might be so type to make such an in depth reply. And yeah, I believe I’ll attempt to use sub/pub.
Many thinks
Pierre
This subject was routinely closed 90 days after the final reply. New replies are not allowed.