-
Notifications
You must be signed in to change notification settings - Fork 0
/
myserver.go
44 lines (35 loc) · 805 Bytes
/
myserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package go_concurrency
import (
"context"
"fmt"
"net/http"
"sync/atomic"
"time"
"github.com/circleci/ex/httpserver/ginrouter"
"github.com/circleci/ex/o11y"
"github.com/gin-gonic/gin"
)
type MyServer struct {
router *gin.Engine
// mutable state
counter int64
}
func NewMyServer(ctx context.Context) *MyServer {
r := ginrouter.Default(ctx, "my-server")
s := &MyServer{router: r}
r.GET("/mypage", s.getMyPage)
return s
}
func (s *MyServer) Handler() http.Handler {
return s.router
}
func (s *MyServer) Counter() int {
return int(s.counter)
}
func (s *MyServer) getMyPage(c *gin.Context) {
ctx := c.Request.Context()
count := atomic.AddInt64(&s.counter, 1)
time.Sleep(10 * time.Millisecond)
o11y.AddField(ctx, "count", count)
c.String(http.StatusOK, fmt.Sprintf("%d", count))
}