Skip to content

Instantly share code, notes, and snippets.

@vincent6767
vincent6767 / stateprotection.go
Last active August 26, 2018 08:48
Mutex: User to protect the state or several assets example
import (
“sync"
)
type Player struct {
sync.Mutex
Health int
}
func (p *Player) DecreaseHealthPoint(damageTaken int)
{
@vincent6767
vincent6767 / mutexcacheinvalidation.go
Created August 26, 2018 08:27
Mutex: Cache Invalidation example
import (
“sync"
)
// TokenRevocationRecord represents the token revocation list record
type TokenRevocationRecord struct {}
// TokenRevocationListCache
type TokenRevocationListCache struct {
TokenRevocationList map[string]TokenRevocationRecord
sync.Mutex
@vincent6767
vincent6767 / match.go
Created August 26, 2018 08:36
Channel: Passing data to another Goroutine example
type MatchMaker {}
func (m *MatchMaker) CreateMatch(game *Game, party *Party) bool {
var serverStatusChan chan bool
gameSessionStatusChan := m.RequestGameSession(game, party) // gameSessionStatusChan returns a channel for signaling the status request.
fmt.Println(“Trying to request game session . . .”)
return <- serverStatusChan // It returns whatever the result asynchronous result operation
}
@vincent6767
vincent6767 / processor.go
Created August 26, 2018 08:50
Channel: Distributing tasks to Goroutines example
type StreamProcessor struct {}
func (sp *StreamProcessor) Consume(fn func(r *Record) error) {
for {
records, _ := sp.GetRecords() // error is not handled for simplicity purpose
for _, r := range records {
go handleRecord(fn, r) // distributing task across goroutines. the output channel is not included for simplicity purpose
}
}
}