cloud-game/pkg/coordinator/worker.go
sergystepanov 431d215eee
Update server HTTPS configuration (#337)
* Merge HTTP and HTTPS routes builder in coordinator

* Extract HTTP/S routes

* Rename config in coordinator

* Generalize child services

* Extract games library helper function

* Use string address instead of port for HTTP/S

* Use a dedicated port extractor function

* Add missing GA tag templating

* Rename shared server address and port params

* Introduce TLS config parameters

* Simplify HTTP/S server constructors

* Update server auto port roll

* Extract init function in worker

* Reorder return params of address type port fn

* Refactor config handler names

* Update TLS default config params

* Extract HTTP to HTTPS redirect function

* Use httpx in monitoring

* Don't log echo requests

* Remove error return from the abstract server

* Add WSS option to the worker-coordinator connection

* Change default worker config

* Make worker send its internal connection params

* Decouple gamelib from the coordinator

* Expose HTTP/S listener address

* Keep original HTTP/S addresses

* Remove no config handler in worker

* Use HTTP-HTTPS redirection

* Wrap net.Listener into a struct

* Clean http server creation fn

* Redirect to https with a generated address

* Use URL in the redirector

* Use zone address param in worker

* Make use of actual addr and port in the monitoring servers

* Use auto-justified monitoring addresses info

* Add the non-HTTPS worker to HTTPS coordinator connection warning

* Embed TLS struct

* Move connection API struct into cws package
2021-08-09 10:42:06 +03:00

63 lines
1.5 KiB
Go

package coordinator
import (
"fmt"
"log"
"sync"
"github.com/giongto35/cloud-game/v2/pkg/cws"
"github.com/gorilla/websocket"
)
type WorkerClient struct {
*cws.Client
WorkerID string
Address string // ip address of worker
// public server used for ping check
PingServer string
StunTurnServer string
userCount int // may be atomic
Zone string
mu sync.Mutex
}
// NewWorkerClient returns a client connecting to worker.
// This connection exchanges information between workers and server.
func NewWorkerClient(c *websocket.Conn, workerID string) *WorkerClient {
return &WorkerClient{
Client: cws.NewClient(c),
WorkerID: workerID,
}
}
// ChangeUserQuantityBy increases or decreases the total amount of
// users connected to the current worker.
// We count users to determine when the worker becomes new game ready.
func (wc *WorkerClient) ChangeUserQuantityBy(n int) {
wc.mu.Lock()
wc.userCount += n
// just to be on a safe side
if wc.userCount < 0 {
wc.userCount = 0
}
wc.mu.Unlock()
}
// HasGameSlot tells whether the current worker has a
// free slot to start a new game.
// Workers support only one game at a time.
func (wc *WorkerClient) HasGameSlot() bool {
wc.mu.Lock()
defer wc.mu.Unlock()
return wc.userCount == 0
}
func (wc *WorkerClient) Printf(format string, args ...interface{}) {
log.Printf(fmt.Sprintf("Worker %s] %s", wc.WorkerID, format), args...)
}
func (wc *WorkerClient) Println(args ...interface{}) {
log.Println(fmt.Sprintf("Worker %s] %s", wc.WorkerID, fmt.Sprint(args...)))
}