-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cde0223
commit 3db7177
Showing
17 changed files
with
938 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,3 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
data.bin | ||
.idea | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
# This file contains all available configuration options | ||
# with their default values. | ||
|
||
# options for analysis running | ||
run: | ||
# default concurrency is a available CPU number | ||
concurrency: 4 | ||
|
||
# timeout for analysis, e.g. 30s, 5m, default is 1m | ||
deadline: 5m | ||
|
||
# exit code when at least one issue was found, default is 1 | ||
issues-exit-code: 1 | ||
|
||
# include test files or not, default is true | ||
tests: true | ||
|
||
# list of build tags, all linters use it. Default is empty list. | ||
build-tags: [] | ||
|
||
# which dirs to skip: they won't be analyzed; | ||
# can use regexp here: generated.*, regexp is applied on full path; | ||
# default value is empty list, but next dirs are always skipped independently | ||
# from this option's value: | ||
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ | ||
skip-dirs: | ||
- vendor | ||
|
||
# which files to skip: they will be analyzed, but issues from them | ||
# won't be reported. Default value is empty list, but there is | ||
# no need to include all autogenerated files, we confidently recognize | ||
# autogenerated files. If it's not please let us know. | ||
skip-files: | ||
- ".*easyjson.*" | ||
|
||
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules": | ||
# If invoked with -mod=readonly, the go command is disallowed from the implicit | ||
# automatic updating of go.mod described above. Instead, it fails when any changes | ||
# to go.mod are needed. This setting is most useful to check that go.mod does | ||
# not need updates, such as in a continuous integration and testing system. | ||
# If invoked with -mod=vendor, the go command assumes that the vendor | ||
# directory holds the correct copies of dependencies and ignores | ||
# the dependency descriptions in go.mod. | ||
# FIXME: | ||
# modules-download-mode: vendor | ||
|
||
|
||
# output configuration options | ||
output: | ||
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" | ||
format: colored-line-number | ||
|
||
# print lines of code with issue, default is true | ||
print-issued-lines: true | ||
|
||
# print linter name in the end of issue text, default is true | ||
print-linter-name: true | ||
|
||
|
||
# all available settings of specific linters | ||
linters-settings: | ||
errcheck: | ||
# report about not checking of errors in type assetions: `a := b.(MyStruct)`; | ||
# default is false: such cases aren't reported by default. | ||
check-type-assertions: true | ||
|
||
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; | ||
# default is false: such cases aren't reported by default. | ||
check-blank: false | ||
|
||
# [deprecated] comma-separated list of pairs of the form pkg:regex | ||
# the regex is used to ignore names within pkg. (default "fmt:.*"). | ||
# see https://github.com/kisielk/errcheck#the-deprecated-method for details | ||
ignore: fmt:.*,io/ioutil:^Read.* | ||
|
||
govet: | ||
# report about shadowed variables | ||
check-shadowing: true | ||
|
||
# settings per analyzer | ||
settings: | ||
printf: # analyzer name, run `go tool vet help` to see all analyzers | ||
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf | ||
golint: | ||
# minimal confidence for issues, default is 0.8 | ||
min-confidence: 0.8 | ||
gofmt: | ||
# simplify code: gofmt with `-s` option, true by default | ||
simplify: true | ||
goimports: | ||
# put imports beginning with prefix after 3rd-party packages; | ||
# it's a comma-separated list of prefixes | ||
# local-prefixes: | ||
gocyclo: | ||
# minimal code complexity to report, 30 by default (but we recommend 10-20) | ||
min-complexity: 30 | ||
goconst: | ||
# minimal length of string constant, 3 by default | ||
min-len: 3 | ||
# minimal occurrences count to trigger, 3 by default | ||
min-occurrences: 3 | ||
depguard: | ||
list-type: blacklist | ||
include-go-root: false | ||
misspell: | ||
# Correct spellings using locale preferences for US or UK. | ||
# Default is to use a neutral variety of English. | ||
# Setting locale to US will correct the British spelling of 'colour' to 'color'. | ||
locale: US | ||
ignore-words: [] | ||
lll: | ||
# max line length, lines longer will be reported. Default is 120. | ||
# '\t' is counted as 1 character by default, and can be changed with the tab-width option | ||
line-length: 150 | ||
# tab width in spaces. Default to 1. | ||
tab-width: 2 | ||
unused: | ||
# treat code as a program (not a library) and report unused exported identifiers; default is false. | ||
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors: | ||
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations | ||
# with golangci-lint call it on a directory with the changed file. | ||
check-exported: false | ||
unparam: | ||
# Inspect exported functions, default is false. Set to true if no external program/library imports your code. | ||
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: | ||
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations | ||
# with golangci-lint call it on a directory with the changed file. | ||
check-exported: false | ||
nakedret: | ||
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30 | ||
max-func-lines: 5 | ||
prealloc: | ||
# XXX: we don't recommend using this linter before doing performance profiling. | ||
# For most programs usage of prealloc will be a premature optimization. | ||
|
||
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. | ||
# True by default. | ||
simple: true | ||
range-loops: true # Report preallocation suggestions on range loops, true by default | ||
for-loops: false # Report preallocation suggestions on for loops, false by default | ||
gocritic: | ||
# Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty | ||
disabled-checks: | ||
- regexpMust | ||
|
||
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint` run to see all tags and checks. | ||
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". | ||
enabled-tags: | ||
- performance | ||
|
||
settings: # settings passed to gocritic | ||
captLocal: # must be valid enabled check name | ||
paramsOnly: true | ||
rangeValCopy: | ||
sizeThreshold: 32 | ||
|
||
linters: | ||
enable-all: true | ||
disable: | ||
- maligned | ||
# - dupl | ||
# - goimports # FIXME | ||
# - gocritic # FIXME | ||
disable-all: false | ||
fast: false | ||
|
||
issues: | ||
# Excluding configuration per-path, per-linter, per-text and per-source | ||
exclude-rules: | ||
# Exclude some linters from running on tests files. | ||
- path: _test\.go | ||
linters: | ||
- gocyclo | ||
- errcheck | ||
- dupl | ||
- gosec | ||
|
||
# Exclude lll issues for long lines with go:generate | ||
- linters: | ||
- lll | ||
text: "^//( )?go:generate " | ||
|
||
# Independently from option `exclude` we use default exclude patterns, | ||
# it can be disabled by this option. To list all | ||
# excluded by default patterns execute `golangci-lint run --help`. | ||
# Default value for this option is true. | ||
exclude-use-default: false | ||
|
||
# Maximum issues count per one linter. Set to 0 to disable. Default is 50. | ||
max-per-linter: 0 | ||
|
||
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3. | ||
max-same-issues: 0 | ||
|
||
# Show only new issues: if there are unstaged changes or untracked files, | ||
# only those changes are analyzed, else only changes in HEAD~ are analyzed. | ||
# It's a super-useful option for integration of golangci-lint into existing | ||
# large codebase. It's not practical to fix all existing issues at the moment | ||
# of integration: much better don't allow issues in new code. | ||
# Default is false. | ||
new: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.PHONY: build lint test vendor | ||
|
||
default: build | ||
|
||
build: | ||
go build -mod=vendor -o build/probe -ldflags "-s -w" cmd/main.go | ||
|
||
lint: | ||
golangci-lint run | ||
|
||
test: | ||
go test ./... | ||
|
||
vendor: | ||
go mod tidy && go mod vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
## Probe | ||
Is in-memory cache server with simple text-based protocol. | ||
|
||
### Features | ||
* TTL per key | ||
* Dump and Restore from disk | ||
* Thread-safety | ||
* Commands: | ||
* `set <key> <value> <ttl>` | ||
* `get <key>` | ||
* `del <key>` | ||
* `quit` | ||
|
||
### Why? | ||
For fun and profit :-) | ||
|
||
### How to use | ||
You need just start a server and connect to it from `nc`, like this: | ||
|
||
```bash | ||
nc 127.0.0.1 4000 | ||
``` | ||
|
||
Set value: | ||
```bash | ||
set mykey myvalue 300s | ||
OK | ||
``` | ||
|
||
Get value: | ||
```bash | ||
get mykey | ||
myvalue | ||
``` | ||
|
||
Delete value: | ||
```bash | ||
del mykey | ||
OK | ||
``` | ||
|
||
### ToDo | ||
* Tests and benchmarks | ||
* Metrics | ||
* Dump and restore without reflection | ||
* Build in GitHub Actions | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"golang.org/x/sync/errgroup" | ||
|
||
"probe/internal/cache" | ||
"probe/internal/server" | ||
) | ||
|
||
var errCanceled = errors.New("canceled") | ||
|
||
func main() { | ||
address := flag.String("addr", "127.0.0.1:4000", "TCP server address") | ||
dataFile := flag.String("path", "data.bin", "Path to data file") | ||
interval := flag.Duration("interval", time.Minute, "Eviction interval") | ||
flag.Parse() | ||
|
||
logger := log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile) | ||
|
||
file, err := os.OpenFile(*dataFile, os.O_RDWR|os.O_CREATE, 0600) | ||
if err != nil { | ||
logger.Fatalf("Failed to open file: %v", err) | ||
} | ||
defer func() { | ||
if err = file.Close(); err != nil { | ||
logger.Printf("Failed to close file: %v", err) | ||
} | ||
}() | ||
|
||
inMemCache := cache.New() | ||
|
||
if err = inMemCache.Restore(file); err != nil { | ||
logger.Printf("Warning. Failed to restore cache: %v", err) | ||
} | ||
|
||
gr, ctx := errgroup.WithContext(context.Background()) | ||
|
||
gr.Go(func() error { | ||
return server.New(*address, inMemCache, logger).Listen(ctx) | ||
}) | ||
|
||
gr.Go(func() error { | ||
inMemCache.Eviction(ctx, *interval) | ||
return nil | ||
}) | ||
|
||
gr.Go(func() error { | ||
signals := make(chan os.Signal, 1) | ||
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-signals: | ||
logger.Println("Caught stop signal. Exiting...") | ||
return errCanceled | ||
} | ||
} | ||
}) | ||
|
||
if err = gr.Wait(); err != nil && err != errCanceled { | ||
logger.Fatalf("Failed to start: %v", err) | ||
} | ||
|
||
if err := file.Truncate(0); err != nil { | ||
logger.Fatalf("Failed to truncate file: %v", err) | ||
} | ||
|
||
if err := inMemCache.Dump(file); err != nil { | ||
logger.Fatalf("Failed to dump cache: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module probe | ||
|
||
go 1.13 | ||
|
||
require golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= | ||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
Oops, something went wrong.