Skip to content

Commit

Permalink
Style fixes
Browse files Browse the repository at this point in the history
Dynom committed Jun 16, 2018
1 parent 1cf683b commit 675ac63
Showing 4 changed files with 17 additions and 11 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -32,8 +32,9 @@ type Config struct {
var config = Config{}

func main() {
var err error

config, err := buildConfig("config.yml")
config, err = buildConfig("config.yml")

if err != nil {
panic(err)
16 changes: 10 additions & 6 deletions server/http.go
Original file line number Diff line number Diff line change
@@ -35,6 +35,10 @@ var (
ErrInvalidRequestBody = errors.New("invalid request body")
)

const (
CtxRequestID = iota
)

type request struct {
Input string `json:"input"`
}
@@ -126,7 +130,7 @@ func NewHTTP(sr ServiceRegistry, mux *http.ServeMux, options ...Option) TySugSer

func createRequestIDHandler(h http.Handler) http.HandlerFunc {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
instanceId := rnd.Int31()
instanceID := rnd.Int31()

var requestCounter int
var lock = sync.Mutex{}
@@ -135,13 +139,13 @@ func createRequestIDHandler(h http.Handler) http.HandlerFunc {
lock.Lock()
requestCounter++
buf.Reset()
buf.WriteString(strconv.Itoa(int(instanceId)))
buf.WriteString(strconv.Itoa(int(instanceID)))
buf.WriteString("-")
buf.WriteString(strconv.Itoa(requestCounter))
reqId := buf.String()
requestID := buf.String()
lock.Unlock()

ctx := context.WithValue(r.Context(), "request_id", reqId)
ctx := context.WithValue(r.Context(), CtxRequestID, requestID)
h.ServeHTTP(w, r.WithContext(ctx))
}
}
@@ -170,12 +174,12 @@ func createRequestHandler(logger *logrus.Logger, svc Service) http.HandlerFunc {
"suggestion": res.Result,
"score": res.Score,
"duration_µs": time.Since(start) / time.Microsecond,
"request_id": ctx.Value("request_id"),
"request_id": ctx.Value(CtxRequestID),
}).Debug("Completed new ranking request")

if !t.Alive() {
logger.WithFields(logrus.Fields{
"request_id": ctx.Value("request_id"),
"request_id": ctx.Value(CtxRequestID),
}).Info("Request got cancelled")
}

4 changes: 0 additions & 4 deletions server/service/domain.go
Original file line number Diff line number Diff line change
@@ -35,10 +35,6 @@ func (s Service) Find(ctx context.Context, input string) (string, float64) {
return suggestion, score
}

func (s Service) Foo(input string, ctx context.Context) {

}

func algJaroWinkler() finder.Algorithm {
return func(a, b string) float64 {
return smetrics.JaroWinkler(a, b, .7, 4)
5 changes: 5 additions & 0 deletions server/service_registry.go
Original file line number Diff line number Diff line change
@@ -4,18 +4,21 @@ import (
"sync"
)

// ServiceRegistry holds (opinionated) service objects for handling specific requests
type ServiceRegistry struct {
services map[string]Service
lock *sync.RWMutex
}

// NewServiceRegistry creates a new registry
func NewServiceRegistry() ServiceRegistry {
return ServiceRegistry{
services: make(map[string]Service),
lock: &sync.RWMutex{},
}
}

// Register registers a new service with a list of references
func (rlh *ServiceRegistry) Register(listName string, svc Service) *ServiceRegistry {
rlh.lock.Lock()
defer rlh.lock.Unlock()
@@ -24,13 +27,15 @@ func (rlh *ServiceRegistry) Register(listName string, svc Service) *ServiceRegis
return rlh
}

// GetServiceForList returns a service able to handle a specific list
func (rlh ServiceRegistry) GetServiceForList(name string) Service {
rlh.lock.RLock()
defer rlh.lock.RUnlock()

return rlh.services[name]
}

// HasServiceForList returns true only if a service has been registered for a certain list
func (rlh ServiceRegistry) HasServiceForList(name string) bool {
rlh.lock.RLock()
defer rlh.lock.RUnlock()

0 comments on commit 675ac63

Please sign in to comment.