Skip to content

Commit

Permalink
build docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Jun 5, 2024
1 parent d26f4f7 commit b801d29
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 13 deletions.
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM golang:1.22-alpine AS builder

LABEL stage=gobuilder \
mainatiner=https://github.com/xgzlucario/rotom

ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct

WORKDIR /build

COPY . .

RUN go build -o rotom .

FROM alpine:latest

ENV TZ Asia/Shanghai
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache ca-certificates tzdata && \
update-ca-certificates

RUN apk --no-cache add redis

VOLUME /data
WORKDIR /data

COPY --from=builder /build/rotom /data/rotom
COPY config.json /etc/rotom/config.json

EXPOSE 6969

CMD ["./rotom", "-config", "/etc/rotom/config.json"]
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ run-gc:
test-cover:
go test -race -v -coverprofile=coverage.txt -covermode=atomic
go tool cover -html=coverage.txt -o coverage.html
rm coverage.txt

pprof:
go tool pprof -http=:18081 "http://192.168.1.6:6060/debug/pprof/profile?seconds=30"

heap:
go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/heap

build-docker:
docker build -t rotom .

# rsync -av --exclude='.git' rotom/ 2:~/xgz/rotom
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## 介绍

你好,这里是 rotom,一个使用 Go 编写的 tiny Redis Server。 在 `v2` 版本中,项目废弃了之前版本的内嵌形式,后续将以 `net server` 的形式维护下去,一方面是为了在实践中学习 Linux 网络编程,另一方面也是为了兼容社区中大量成熟的 redis 相关工具来辅助开发。
这里是 rotom,一个使用 Go 编写的 tiny Redis Server。 在 `v2` 版本中,项目废弃了之前版本的内嵌形式,后续将以 `net server` 的形式维护下去,一方面是为了在实践中学习 Linux 网络编程,另一方面也是为了兼容社区中大量成熟的 redis 相关工具来辅助开发。

实现特性:

Expand All @@ -18,6 +18,8 @@

## 使用

**本机运行**

首先克隆项目到本地:

```bash
Expand All @@ -28,12 +30,28 @@ git clone https://github.com/xgzlucario/rotom

```
$ go run .
2024/06/04 17:53:09 read config file: {
2024/06/05 15:26:47 cmd arguments: config=config.json, debug=false
2024/06/05 15:26:47 read config file: {
"port": 6969,
"appendonly": false,
"appendfilename": "appendonly.aof"
}
2024/06/04 17:53:09 rotom server is ready to accept.
2024/06/05 15:26:47 rotom server is ready to accept.
```

**容器运行**

或者你也可以使用容器运行,首先运行 `make build-docker` 打包:

```
REPOSITORY TAG IMAGE ID CREATED SIZE
rotom latest 270888260e99 3 minutes ago 21.1MB
```

然后启动容器:

```bash
docker run --rm -p 6969:6969 --name rotom rotom:latest
```

## 性能测试
Expand Down
10 changes: 5 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func getCommand(args []Value) Value {

func hsetCommand(args []Value) Value {
hash := args[0].bulk
args = args[1:]

// check arguments number
exargs := args[1:]
if len(exargs) == 0 || len(exargs)%2 == 1 {
if len(args)%2 == 1 {
return newErrValue(ErrWrongNumberArgs("hset"))
}

Expand All @@ -68,9 +68,9 @@ func hsetCommand(args []Value) Value {
}

var newFields int
for i := 0; i < len(exargs); i += 2 {
key := exargs[i].bulk
value := exargs[i+1].bulk
for i := 0; i < len(args); i += 2 {
key := args[i].bulk
value := args[i+1].bulk
if hmap.Set(b2s(key), value) {
newFields++
}
Expand Down
14 changes: 14 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestHandler(t *testing.T) {
})

t.Run("hset", func(t *testing.T) {
// hset
res, _ := rdb.HSet(ctx, "map", "k1", "v1").Result()
assert.Equal(res, int64(1))

Expand All @@ -67,11 +68,24 @@ func TestHandler(t *testing.T) {
res, _ = rdb.HSet(ctx, "map", map[string]any{"k4": "v4", "k5": "v5"}).Result()
assert.Equal(res, int64(0))

// hget
{
res, _ := rdb.HGet(ctx, "map", "k1").Result()
assert.Equal(res, "v1")

res, err := rdb.HGet(ctx, "map", "k99").Result()
assert.Equal(err, redis.Nil)
assert.Equal(res, "")
}

resm, _ := rdb.HGetAll(ctx, "map").Result()
assert.Equal(resm, map[string]string{"k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4", "k5": "v5"})

// error
_, err := rdb.HSet(ctx, "map").Result()
assert.Equal(err.Error(), ErrWrongNumberArgs("hset").Error())

_, err = rdb.HSet(ctx, "map", "k1", "v1", "k2").Result()
assert.Equal(err.Error(), ErrWrongNumberArgs("hset").Error())
})
}
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
package main

import (
"flag"
"log"
"net/http"
_ "net/http/pprof"
)

func debug() {
func runDebug() {
go http.ListenAndServe(":6060", nil)

Check warning on line 11 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L10-L11

Added lines #L10 - L11 were not covered by tests
}

func main() {
var err error
config, err := LoadConfig("config.json")
var path string
var debug bool

Check warning on line 16 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L14-L16

Added lines #L14 - L16 were not covered by tests

flag.StringVar(&path, "config", "config.json", "default config file path.")
flag.BoolVar(&debug, "debug", false, "run with debug mode.")
flag.Parse()

Check warning on line 20 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L18-L20

Added lines #L18 - L20 were not covered by tests

log.Printf("cmd arguments: config=%s, debug=%v", path, debug)

Check warning on line 22 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L22

Added line #L22 was not covered by tests

config, err := LoadConfig(path)
if err != nil {
log.Panicf("load config error: %v\n", err)

Check warning on line 26 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L24-L26

Added lines #L24 - L26 were not covered by tests
}
if err = InitDB(config); err != nil {
log.Panicf("init db error: %v\n", err)

Check warning on line 29 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L28-L29

Added lines #L28 - L29 were not covered by tests
}
debug()
if debug {
runDebug()

Check warning on line 32 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}
server.config = config
server.RunServe()

Check warning on line 35 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}

0 comments on commit b801d29

Please sign in to comment.