Skip to content

Commit

Permalink
Merge pull request #52 from CATechnologiesTest/websocket-status
Browse files Browse the repository at this point in the history
Websocket status
  • Loading branch information
jd3quist authored Jan 14, 2019
2 parents d0bf1b0 + 92dc64d commit dd49ee3
Show file tree
Hide file tree
Showing 8 changed files with 602 additions and 10 deletions.
38 changes: 32 additions & 6 deletions goapi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func (client *CacheClient) Remove(key string) interface{} {
return runOnServer(
func() interface{} {
if val, ok := client.cache[key]; ok {
val.timer.Stop()
if val.timer != nil {
val.timer.Stop()
}
delete(client.cache, key)
return val.obj
}
Expand All @@ -83,17 +85,41 @@ func (client *CacheClient) Remove(key string) interface{} {
func (client *CacheClient) Add(key string, obj interface{}) bool {
return runOnServer(
func() interface{} {
if len(client.cache) >= client.maxSize {
if !client.hasSpace() {
return false
}
t := time.AfterFunc(time.Duration(client.timeoutSecs)*time.Second,
func() {
client.Remove(key)
})
t := client.getTimeoutFun(key)
client.cache[key] = cacheEntry{t, obj}
return true
}, client.serverMbox).(bool)
}

func (client *CacheClient) ForEach(op func(key string, data interface{})) {
runOnServer(
func() interface{} {
for k, v := range client.cache {
op(k, v.obj)
}
return nil
}, client.serverMbox)
}

func (client *CacheClient) hasSpace() bool {
if client.maxSize > 0 {
return len(client.cache) < client.maxSize
}
return true
}

func (client *CacheClient) getTimeoutFun(key string) *time.Timer {
if client.timeoutSecs > 0 {
return time.AfterFunc(time.Duration(client.timeoutSecs)*time.Second,
func() {
client.Remove(key)
})
}
return nil
}

// compilation error if we don't implement the i/f properly
var _ clientif = (*CacheClient)(nil)
3 changes: 3 additions & 0 deletions goapi/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ module github.com/CATechnologiesTest/yipee-tiyay/goapi

require (
github.com/google/uuid v1.1.0
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2
github.com/gorilla/websocket v1.4.0
github.com/sirupsen/logrus v1.3.0
)
15 changes: 15 additions & 0 deletions goapi/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
9 changes: 5 additions & 4 deletions goapi/k8sapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ var k8sApiHost = liveApiHost

// "static" vars
var (
k8sClient *http.Client
k8sToken string
k8sonce sync.Once
k8sClient *http.Client
k8sWatchClient *http.Client
k8sToken string
k8sonce sync.Once
)

func readSecret(name string) []byte {
Expand Down Expand Up @@ -54,7 +55,7 @@ func k8sInit() {
ktrans = &http.Transport{TLSClientConfig: ktls}
}
k8sClient = &http.Client{Transport: ktrans, Timeout: DEFAULT_TIMEOUT}

k8sWatchClient = &http.Client{Transport: ktrans} // no timeout
k8sToken = ""
if tokbytes := readSecret("token"); tokbytes != nil {
k8sToken = string(tokbytes)
Expand Down
38 changes: 38 additions & 0 deletions goapi/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"net/http"
"sync"
)

var logOnce sync.Once

func initLogger(router *mux.Router) {
logOnce.Do(func() {
log.SetLevel(log.DebugLevel)

// XXX: use JSON formatter if/when we go to some kind of logstash-y approach
// Until then, this is easier to read.
log.SetFormatter(&log.TextFormatter{DisableColors: true})
router.HandleFunc("/setLog", func(w http.ResponseWriter, r *http.Request) {
qvals := r.URL.Query()
level := qvals["level"][0]
switch level {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
// XXX: we don't support/use "fatal" and "panic" levels
default:
log.Warnf("Invalid setting for loglevel: %v", level)
}
})

})
}
2 changes: 2 additions & 0 deletions goapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ var (
func Router() *mux.Router {
once.Do(func() {
router = mux.NewRouter()
initLogger(router)
initImports(router)
initConverts(router)
initNamespaces(router)
initStatus(router)
})
return router
}
Expand Down
Loading

0 comments on commit dd49ee3

Please sign in to comment.