Skip to content

Commit

Permalink
New Go runtime and Lua runtime local cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Sep 13, 2018
1 parent 2b8abb5 commit bb21e62
Show file tree
Hide file tree
Showing 52 changed files with 10,120 additions and 2,263 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com) and this project uses [semantic versioning](http://semver.org).

## [Unreleased]
### Added
- New Go code runtime for custom functions and authoritative match handlers.
- Lua runtime token generator function now returns a second value representing the token's expiry.
- Add local cache for in-memory storage to the Lua runtime.

### Fixed
- Correctly merge new friend records when importing from Facebook.
- Log correct registered hook names at startup.

## [2.0.3] - 2018-08-10
### Added
Expand Down
23 changes: 20 additions & 3 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# docker build . --rm --build-arg version=2.0.4 --build-arg commit=master -t heroiclabs/nakama:2.0.4

FROM golang:1.10-alpine3.7 as builder

ARG commit

RUN apk --no-cache add ca-certificates gcc musl-dev git

WORKDIR /go/src/github.com/heroiclabs/
RUN git config --global advice.detachedHead false && \
git clone -q -n https://github.com/heroiclabs/nakama

WORKDIR /go/src/github.com/heroiclabs/nakama
RUN git checkout -q "$commit" && \
GOOS=linux GOARCH=amd64 go build && \
mkdir -p /go/build && \
mv nakama /go/build

FROM alpine:3.7

MAINTAINER Heroic Labs <[email protected]>
Expand All @@ -12,12 +30,11 @@ RUN mkdir -p /nakama/data/modules && \
apk --no-cache add ca-certificates curl iproute2

WORKDIR /nakama/
COPY ./nakama ./nakama
COPY --from=builder "/go/build/nakama" /nakama/
EXPOSE 7349 7350 7351

# set entry point to nakama so that it can be invoked from the `docker run nakama`
ENTRYPOINT ["./nakama"]

# curl fails on non-200 HTTP status code
HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -f http://localhost:7350/ || exit 1

14 changes: 14 additions & 0 deletions build/plugin.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# docker build . --file ./plugin.Dockerfile --build-arg src=sample_go_module

FROM golang:1.10-alpine3.7 as builder

ARG src

WORKDIR /go/src/$src
COPY $src /go/src/$src

RUN apk --no-cache add ca-certificates gcc musl-dev git && \
go get -u github.com/heroiclabs/nakama && \
GOOS=linux go build -buildmode=plugin . && \
mkdir -p /go/build && \
mv "/go/src/$src/$src.so" /go/build/
8 changes: 4 additions & 4 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ func (fm *FlagMaker) enumerateAndCreate(prefix string, value reflect.Value, usag
}

usageDesc := fm.getUsage(optName, stField)
if len(usageDesc) == 0 {
optName = optName
}
//if len(usageDesc) == 0 {
// optName = optName
//}

fm.enumerateAndCreate(optName, field, usageDesc)
}
Expand Down Expand Up @@ -450,7 +450,7 @@ func (fm *FlagMaker) defineFlag(name string, value reflect.Value, usage string)
fm.fs.DurationVar(v, name, value.Interface().(time.Duration), usage)
default:
// (TODO) if one type defines time.Duration, we'll create a int64 flag for it.
// Find some acceptible way to deal with it.
// Find some acceptable way to deal with it.
vv := ptrValue.Convert(int64PtrType).Interface().(*int64)
fm.fs.Int64Var(vv, name, value.Int(), usage)
}
Expand Down
26 changes: 10 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -95,40 +94,35 @@ func main() {

// Access to social provider integrations.
socialClient := social.NewClient(5 * time.Second)
// Used to govern once-per-server-start executions in all Lua runtime instances, across both pooled and match VMs.
once := &sync.Once{}

// Start up server components.
matchmaker := server.NewLocalMatchmaker(startupLogger, config.GetName())
sessionRegistry := server.NewSessionRegistry()
tracker := server.StartLocalTracker(logger, sessionRegistry, jsonpbMarshaler, config.GetName())
router := server.NewLocalMessageRouter(sessionRegistry, tracker, jsonpbMarshaler)
stdLibs, modules, err := server.LoadRuntimeModules(startupLogger, config)
if err != nil {
startupLogger.Fatal("Failed reading runtime modules", zap.Error(err))
}
leaderboardCache := server.NewLocalLeaderboardCache(logger, startupLogger, db)
matchRegistry := server.NewLocalMatchRegistry(logger, db, config, socialClient, leaderboardCache, sessionRegistry, tracker, router, stdLibs, once, config.GetName())
matchRegistry := server.NewLocalMatchRegistry(logger, config, tracker, config.GetName())
tracker.SetMatchJoinListener(matchRegistry.Join)
tracker.SetMatchLeaveListener(matchRegistry.Leave)
// Separate module evaluation/validation from module loading.
// We need the match registry to be available to wire all functions exposed to the runtime, which in turn needs the modules at least cached first.
regCallbacks, err := server.ValidateRuntimeModules(logger, startupLogger, db, config, socialClient, leaderboardCache, sessionRegistry, matchRegistry, tracker, router, stdLibs, modules, once)
runtime, err := server.NewRuntime(logger, startupLogger, db, jsonpbMarshaler, jsonpbUnmarshaler, config, socialClient, leaderboardCache, sessionRegistry, matchRegistry, tracker, router)
if err != nil {
startupLogger.Fatal("Failed initializing runtime modules", zap.Error(err))
}
runtimePool := server.NewRuntimePool(logger, startupLogger, db, config, socialClient, leaderboardCache, sessionRegistry, matchRegistry, tracker, router, stdLibs, modules, regCallbacks, once)
pipeline := server.NewPipeline(config, db, jsonpbMarshaler, jsonpbUnmarshaler, sessionRegistry, matchRegistry, matchmaker, tracker, router, runtimePool)
pipeline := server.NewPipeline(logger, config, db, jsonpbMarshaler, jsonpbUnmarshaler, sessionRegistry, matchRegistry, matchmaker, tracker, router, runtime)
metrics := server.NewMetrics(logger, startupLogger, config)

consoleServer := server.StartConsoleServer(logger, startupLogger, config, db)
apiServer := server.StartApiServer(logger, startupLogger, db, jsonpbMarshaler, jsonpbUnmarshaler, config, socialClient, leaderboardCache, sessionRegistry, matchRegistry, matchmaker, tracker, router, pipeline, runtimePool)
apiServer := server.StartApiServer(logger, startupLogger, db, jsonpbMarshaler, jsonpbUnmarshaler, config, socialClient, leaderboardCache, sessionRegistry, matchRegistry, matchmaker, tracker, router, pipeline, runtime)

gaenabled := len(os.Getenv("NAKAMA_TELEMETRY")) < 1
cookie := newOrLoadCookie(config)
gacode := "UA-89792135-1"
var telemetryClient *http.Client
if gaenabled {
runTelemetry(http.DefaultClient, gacode, cookie)
telemetryClient = &http.Client{
Timeout: 1500 * time.Millisecond,
}
runTelemetry(telemetryClient, gacode, cookie)
}

// Respect OS stop signals.
Expand All @@ -150,7 +144,7 @@ func main() {
sessionRegistry.Stop()

if gaenabled {
ga.SendSessionStop(http.DefaultClient, gacode, cookie)
ga.SendSessionStop(telemetryClient, gacode, cookie)
}

os.Exit(0)
Expand Down
Loading

0 comments on commit bb21e62

Please sign in to comment.