-
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.
TRQ-002: Integrate service with ZooKeeper (#4)
* TRQ-002: Setup zookeeper via docker * TRQ-002: Integrate zookeeper with service * TRQ-002: Move service to docker * TRQ-002: Add retry for connecting to zookeeper * TRQ-002: (minor) remove comment
- Loading branch information
Showing
8 changed files
with
180 additions
and
9 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,2 +1,3 @@ | ||
PORT=8081 | ||
ENV=release | ||
ZOOKEEPER_SERVERS=127.0.0.1:2181 |
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 @@ | ||
FROM golang:alpine | ||
|
||
WORKDIR /app | ||
|
||
COPY go.mod go.sum ./ | ||
|
||
RUN go mod download | ||
|
||
COPY . . | ||
|
||
RUN go build -o main ./cmd/main.go | ||
|
||
ENV PORT=8080 | ||
|
||
CMD ["./main"] |
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
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,31 @@ | ||
version: '3.8' | ||
|
||
services: | ||
go-server-8080: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
ports: | ||
- "8080:8080" | ||
environment: | ||
- PORT=8080 | ||
- ZOOKEEPER_SERVERS=zookeeper:2181 | ||
depends_on: | ||
- zookeeper | ||
go-server-8081: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
ports: | ||
- "8081:8081" | ||
environment: | ||
- PORT=8081 | ||
- ZOOKEEPER_SERVERS=zookeeper:2181 | ||
depends_on: | ||
- zookeeper | ||
zookeeper: | ||
image: 'bitnami/zookeeper:latest' | ||
ports: | ||
- '2181:2181' | ||
environment: | ||
- ALLOW_ANONYMOUS_LOGIN=yes |
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
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
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,92 @@ | ||
package counter | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/go-zookeeper/zk" | ||
) | ||
|
||
type Counter struct { | ||
zkConn *zk.Conn | ||
zkPath string | ||
} | ||
|
||
func NewCounterWithRetry(servers []string, path string) (*Counter, error) { | ||
const RetryCount = 5 | ||
const RetryDelayS = 5 | ||
|
||
var ( | ||
c *Counter | ||
err error | ||
) | ||
|
||
for attempt := 1; attempt <= RetryCount; attempt++ { | ||
c, err = NewCounter(servers, path) | ||
|
||
if err != nil { | ||
log.Printf("ERROR: Failed to connect to ZooKeeper (attempt %d/%d): %v", attempt, RetryCount, err) | ||
time.Sleep(time.Second * RetryDelayS) | ||
continue | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
func NewCounter(servers []string, path string) (*Counter, error) { | ||
conn, _, err := zk.Connect(servers, time.Second*5) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &Counter{ | ||
zkConn: conn, | ||
zkPath: path, | ||
} | ||
|
||
if exists, _, err := conn.Exists(path); err != nil { | ||
return nil, err | ||
} else if !exists { | ||
_, err := conn.Create(path, []byte("0"), 0, zk.WorldACL(zk.PermAll)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *Counter) GetAndIncrement() (int, error) { | ||
for { | ||
data, stat, err := c.zkConn.Get(c.zkPath) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
currentValue, err := strconv.Atoi(string(data)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
newValue := currentValue + 1 | ||
newData := []byte(fmt.Sprintf("%d", newValue)) | ||
|
||
_, err = c.zkConn.Set(c.zkPath, newData, stat.Version) | ||
if err == zk.ErrBadVersion { | ||
continue | ||
} else if err != nil { | ||
return 0, err | ||
} | ||
|
||
return newValue, nil | ||
} | ||
} | ||
|
||
func (c *Counter) Close() { | ||
c.zkConn.Close() | ||
} |
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,26 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/godcrampy/torquay/pkg/counter" | ||
) | ||
|
||
type Handler struct { | ||
Counter *counter.Counter | ||
} | ||
|
||
func NewHandler(c *counter.Counter) *Handler { | ||
return &Handler{Counter: c} | ||
} | ||
|
||
func (h *Handler) GetToken(c *gin.Context) { | ||
newValue, err := h.Counter.GetAndIncrement() | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"token": newValue}) | ||
} |