Comments (10)
Access to objects (such as games) from web(socket)handlers should be synchronized (think mutex) to prevent racing conditions.
from mwdns.
Only if the webserver itself is multithreaded, which is not necessarily the case if it's a select/poll/epoll/kqueue server! (TODO: figure this out)
from mwdns.
Good point, I was assuming it. There is concurrency in the handler execution, although there is said to be no parallelism (except when GOMAXPROCS is set to > 1). Testing code:
Golang:
package main
import (
"fmt"
"net/http"
"time"
)
func serve(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello there!")
time.Sleep(5 * time.Second)
}
func main() {
http.HandleFunc("/", serve)
http.ListenAndServe(":8090", nil)
}
Bash:
time for i in `seq 1 5`; do; curl localhost:8090&; done
All requests are finished at the same time and not 5 seconds after one another.
from mwdns.
So, should we just set GOMAXPROCS
to 1 at the beginning of main? That might make our life much easier and it's not really the case that anything is computationally heavy and we'd need parallelism anyways, is it?
from mwdns.
it is usually set to 1 (by convention, not default) already, but this does not solve the concurrency issues. However, the worst effects of not-minding this that I currently see would be: joining an already full game (when 2 dudes try at the same time and the thread execution gods are benevolent), or scoring the same pair of cards in rush more by two different players. I have not looked hard though.
from mwdns.
Hmm? As I understood it, "concurrent but not parallel" will only let "other" code run when you call something which relinquishes control, such as Select
or read/write a chan
or Sleep
. With this in mind, I don't see the scenarios you're talking about happening. Am I missing/misunderstanding something?
from mwdns.
I think my wording was off. By concurrent, I mean that the code may be executed interleaved. Assuming a handler does not just 'Sleep', but something like:
fmt.Println("Hello there")
time.Sleep((time.Duration)(rand.Int()%1000) * time.Nanosecond)
fmt.Println("fine sir")
time.Sleep((time.Duration)(rand.Int()%1000) * time.Nanosecond)
fmt.Println("how are you")
then the output for two requests can be interleaving ("Hello...", "Hello...", "fine...", "fine...", "how...", "how..."). As the simplest case: if handler code contains some if condition, it can happen that two requests get through the check at once, instead of just one (scoring the same card by two players for example).
I think our concurrency issues can be solved easily, just by mutex-ing all requests that originate in handlers.
from mwdns.
Yes, I agree, but what I meant to say is that this "context switch" (quoted, it's not really one) will not happen arbitrarily, but only where "sleeping" functions are called, such as the ones I mentioned previously. Thus, if we don't have any call to such a function, no context will be switched.
Having said that, I agree that we can just add mutexes to be on the safe side.
from mwdns.
Oh, I understand now what you meant. Derp, sry. Are you sure that there are no arbitrary "context switches" (does logging count?), but only sensible ones?
from mwdns.
I'm confident, but I cannot be sure since I'm no go expert. At the end of the day, your "better be safe than sorry" way is... safer :)
from mwdns.
Related Issues (20)
- Options screen constraints
- Player LEFT HOT 2
- Rush responsiveness HOT 1
- THIS WAS A GOOD GAME, HOW ABOUT ANOTHER ONE? HOT 3
- Only move open & scored cards in classic mode HOT 5
- Rush Turns == # of flips != Classic Turns
- Fetch score and #turns when joining
- Subtle info of keys/features
- Client-side card-open-close delay HOT 4
- Create a Makefile
- Card layout option as buttons HOT 1
- Server-side card color generation HOT 3
- FAVICON
- Game Manager
- Fancy Logging
- TileMap Cards Bug
- Both players highlighted as active HOT 2
- Wrong Score or winning condition HOT 2
- How about some nice tilemaps? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mwdns.