Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github actions demo #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Go build test

on:
pull_request:
branches:
- master
schedule:
- cron: '0 20 * * Sat'
push:


env:
GLOBAL_ENV: global_env_value
DAY_OF_WEEK: ThursDay

jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4 # caching is enabled by default
with:
go-version-file: 'go.mod'
cache-dependency-path: go.sum
# go-version: '1.19.x'
# go-version: 'stable'

- name: Build ${{ github.ref_name }}
run: go build -v ./...

- name: Test
run: go test -v ./...

- name: Use variables
if: ${{ env.DAY_OF_WEEK == 'ThursDay' }}
# if [[ env.DAY_OF_WEEK == 'ThursDay' ]]; then
# fi
run: |
echo "repository variable : $REPOSITORY_VAR"
echo "secret var : $REPOSITORY_SECRET"
echo "global env : $GLOBAL_ENV"
env:
REPOSITORY_VAR: ${{ vars.REPOSITORY_VAR }}
REPOSITORY_SECRET: ${{ secrets.SECRET_TEST_NAME }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

cover.out
102 changes: 102 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
linters-settings:
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
goconst:
min-len: 2
min-occurrences: 2
gocritic:
settings:
hugeParam:
sizeThreshold: 100
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
- wrapperFunc
- unnecessaryBlock
gocyclo:
min-complexity: 15
goimports:
local-prefixes: github.com/monacohq/ncw-x,github.com/monacohq/ncw_nft
golint:
min-confidence: 0.8
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks:
- argument
- case
- condition
- return
lll:
line-length: 140
maligned:
suggest-new: true
misspell:
locale: US
linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- depguard
- dogsled
- dupl
- errcheck
- exportloopref
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- govet
- ineffassign
- lll
- misspell
- nakedret
- revive
- typecheck
- unconvert
- unused
- whitespace
- dupword
- unparam
- unconvert

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- linters:
- gomnd
source: "\\.New.*()"

- linters:
- gomnd
source: "strconv\\..*()"

- linters:
- gomnd
source: " make()"

run:
skip-dirs:
- docs
- tmp
timeout: 3m
allow-parallel-runners: true
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GO ?= go
GOFMT ?= gofmt "-s"
GO_ENV ?= local
VERSION ?= $(shell git describe --tags --always || git rev-parse --short HEAD)
IMAGE_TAG ?= $(GO_ENV)-$(VERSION)
APP_TAG ?= .APP_TAG

GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")

.PHONY: fmt
fmt:
$(GOFMT) -w $(GOFILES)

.PHONY: lint lint_install lint_check
lint: lint_install lint_check
@$(GOBIN)/golangci-lint version
@$(GOBIN)/golangci-lint run

LINTVERSION ?= v1.50.1
GOBIN = $(HOME)/go/bin
LINTINSTALL = $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@$(LINTVERSION)
lint_install:
@if [ ! -d $(GOBIN) ] ; then mkdir -p $(GOBIN) ; else echo "GOBIN: $(GOBIN)" ; fi
@if [ ! -f $(GOBIN)/golangci-lint ] ; then GOBIN=$(GOBIN) $(LINTINSTALL); else echo "golangci-lint exists" ; fi

CURRENT_VERSION := `$(GOBIN)/golangci-lint version | sed -nre 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p'`
lint_check:
@if [ "$(shell echo v$(CURRENT_VERSION))" != "$(LINTVERSION)" ] ; then GOBIN=$(GOBIN) $(LINTINSTALL) ; fi

.PHONY: test
test:
$(GO) test -race -v -p 4 -race -cover -coverprofile=cover.out ./...
24 changes: 22 additions & 2 deletions benchmark_timewheel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func BenchmarkTimeWheelTest(b *testing.B) {
const delay = 10 * time.Millisecond
num := int32(0)
tw, _ := NewTimeWheel(10*time.Millisecond, 3600)
tw, _ := NewTimeWheel(delay, 3600)
tw.activate()
defer tw.stop()
f := func() {
Expand All @@ -22,6 +22,26 @@ func BenchmarkTimeWheelTest(b *testing.B) {
for i := 0; i < b.N; i++ {
tw.addTimer(delay, f, false)
}
time.Sleep(100 * time.Millisecond)
time.Sleep(3 * delay)
require.EqualValues(b, b.N, atomic.LoadInt32(&num))
}

func BenchmarkTimeWheelParallelTest(b *testing.B) {
const delay = 10 * time.Millisecond
num := int32(0)
tw, _ := NewTimeWheel(delay, 3600)
tw.activate()
defer tw.stop()
f := func() {
atomic.AddInt32(&num, 1)
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
tw.addTimer(delay, f, false)
}
})
time.Sleep(3 * delay)
require.EqualValues(b, b.N, atomic.LoadInt32(&num))
}
6 changes: 6 additions & 0 deletions no_copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package timewheel

type noCopy struct{} //nolint: unused

func (*noCopy) Lock() {} //nolint: unused
func (*noCopy) Unlock() {} //nolint: unused
9 changes: 2 additions & 7 deletions timewheel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type CallBack func()

// based on https://github.com/ouqiang/timewheel
type TimeWheel struct {
noCopy noCopy
noCopy noCopy // nolint: unused

interval time.Duration // time interval to moving forward
ticker *time.Ticker
Expand Down Expand Up @@ -185,7 +185,7 @@ func (tw *TimeWheel) addTask(task *Task) {
}
}

func (tw *TimeWheel) getPositionAndCircle(d time.Duration) (pos int64, circle int64) {
func (tw *TimeWheel) getPositionAndCircle(d time.Duration) (pos, circle int64) {
delayNanoseconds := d.Nanoseconds()
intervalNanoseconds := tw.interval.Nanoseconds()
circle = delayNanoseconds / intervalNanoseconds / tw.slotNum
Expand Down Expand Up @@ -238,8 +238,3 @@ func RemoveTimer(key string) {
func Stop() {
tw.stop()
}

type noCopy struct{}

func (*noCopy) Lock() {}
func (*noCopy) Unlock() {}
1 change: 0 additions & 1 deletion timewheel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func TestTimeWheelRemove(t *testing.T) {
tw.addTimer(delay, wfun, false)

<-testChan

})
}

Expand Down