diff --git a/.gitattributes b/.gitattributes index 0b16ea8..5cd6308 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -web/* linguist-vendored \ No newline at end of file +web/* linguist-vendored diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..de148d6 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,33 @@ +name: Build Docker Image + +on: + push: + # trigger on any tag push + tags: + - "**" + +concurrency: + group: ${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Get Tag Version + id: get_version + run: | + export TRUNCATED_GITHUB_SHA=$(echo ${{ github.sha }} | cut -c1-7); + echo "VERSION=${GITHUB_REF/refs\/tags\//}+${TRUNCATED_GITHUB_SHA}" >> $GITHUB_ENV + echo "GIT_TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV + + - name: Build and Publish to Registry + uses: elgohr/Publish-Docker-Github-Action@3.04 + with: + name: penguin-statistics/probe + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + registry: ghcr.io + tags: "latest,${{ env.GIT_TAG }}" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9f0c56b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +FROM golang:1.18.1-alpine AS base +WORKDIR /app + +# builder +FROM base AS gobuilder +ENV GOOS linux +ENV GOARCH amd64 + +# go 1.18 requires git +RUN apk add --no-cache git + +# modules: utilize build cache +COPY go.mod ./ +COPY go.sum ./ + +# RUN go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn,direct +RUN go mod download +COPY . . + +# build the binary +RUN go build -o probe . + +# runner +FROM base AS runner +RUN apk add --no-cache libc6-compat tini +# Tini is now available at /sbin/tini + +COPY --from=gobuilder /app/probe /app/probe +COPY --from=gobuilder /app/config.example.yml /app/config.yml + +ENTRYPOINT ["/sbin/tini", "--"] +CMD [ "/app/probe" ] diff --git a/Makefile b/Makefile index b026eba..0ba930d 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,8 @@ init: - go get -v ./... - go get github.com/cespare/reflex + go install github.com/mitranim/gow@latest dev: - reflex -d none -t 1s -s -R vendor. -r '\.(go|yml)$$' -- sh -c 'PENGUIN_PROBE_PPROF=1 go run ./cmd' + gow -c -e go,yml,mod run ./cmd protoc: protoc -I=internal/pkg/messages/ --go_out=internal/pkg/messages/ internal/pkg/messages/*.proto diff --git a/cmd/analytics.go b/cmd/analytics.go index 84cb419..0d54524 100644 --- a/cmd/analytics.go +++ b/cmd/analytics.go @@ -1,14 +1,15 @@ -package main +package cmd import ( "fmt" - "github.com/penguin-statistics/probe/internal/app/server" "net/http" _ "net/http/pprof" "os" + + "github.com/penguin-statistics/probe/internal/app/server" ) -func main() { +func Bootstrap() { if os.Getenv("PENGUIN_PROBE_PPROF") == "1" { go func() { fmt.Println("pprof enabled") diff --git a/config.yml b/config.example.yml similarity index 100% rename from config.yml rename to config.example.yml diff --git a/internal/pkg/wspool/client.go b/internal/pkg/wspool/client.go index e3b5e74..0c35a68 100644 --- a/internal/pkg/wspool/client.go +++ b/internal/pkg/wspool/client.go @@ -2,16 +2,18 @@ package wspool import ( "errors" + "time" + "github.com/gorilla/websocket" - "github.com/penguin-statistics/probe/internal/pkg/messages" "go.uber.org/ratelimit" "google.golang.org/protobuf/proto" - "time" + + "github.com/penguin-statistics/probe/internal/pkg/messages" ) const ( // Time allowed to write a message to the peer. - writeWait = 30 * time.Second + writeWait = 5 * time.Second // Time allowed to read the next pong message from the peer. pongWait = 60 * time.Minute @@ -26,6 +28,8 @@ const ( maxRPS = 20 ) +var ErrInvalidMessageType = errors.New("invalid message type") + // ClientRequest is the skeleton-unmarshalled client side request type ClientRequest struct { Skeleton *messages.Skeleton @@ -113,7 +117,7 @@ func (c *Client) readSkeleton() (s *messages.Skeleton, p []byte, err error) { } if typ != websocket.BinaryMessage && typ != websocket.PongMessage { c.Hub.logger.Debugln("unexpected message type that is not a BinaryMessage type", typ) - return &messages.Skeleton{}, nil, errors.New("unexpected message type") + return &messages.Skeleton{}, nil, ErrInvalidMessageType } var skeleton messages.Skeleton @@ -137,6 +141,7 @@ func (c *Client) Write() { c.Hub.logger.Traceln("starting ping ticker with period of", pingPeriod) pingTicker := time.NewTicker(pingPeriod) defer func() { + c.Hub.logger.Traceln("stopping writer") pingTicker.Stop() c.Close() }() @@ -149,7 +154,7 @@ func (c *Client) Write() { err := c.Conn.WritePreparedMessage(message) if err != nil { c.Hub.logger.Debugln("failed to send message", err) - c.InvalidCount++ + return } c.Hub.logger.Traceln("ws data sent") case <-pingTicker.C: diff --git a/main.go b/main.go new file mode 100644 index 0000000..b8a3463 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/penguin-statistics/probe/cmd" + +func main() { + cmd.Bootstrap() +}