Skip to content

Commit

Permalink
Move from fmt to log for log stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
alienth committed Mar 8, 2017
1 parent 7647911 commit 063f5b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions hitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (hits *hitMap) syncIPsWithHook() {
}
if !noop {
if err := hook.Sync(limits); err != nil {
fmt.Printf("Error syncing banned IPs with hook service: %s\n", err)
logger.Printf("Error syncing banned IPs with hook service: %s\n", err)
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func (hits *hitMap) importIPRates(serviceDomains ServiceDomains) error {
if err != nil {
// We may not have created an entry, so ignore entries with
// comments that we don't recognize.
fmt.Printf("Found unrecognized ACL comment for IP %s on service %s. Ignoring.\ncomment:\n%s\nError:\n%s\n", ipr.ip.String(), entry.ServiceID, entry.Comment, err)
logger.Printf("Found unrecognized ACL comment for IP %s on service %s. Ignoring.\ncomment:\n%s\nError:\n%s\n", ipr.ip.String(), entry.ServiceID, entry.Comment, err)
continue
}
if ipr.LastHit.Before(placeholder.LastHit) {
Expand All @@ -131,6 +131,6 @@ func (hits *hitMap) Lock() {
ts := time.Now()
hits.RWMutex.Lock()
if d := ts.Sub(time.Now()); d > time.Duration(1)*time.Second {
fmt.Printf("Warning: Blocked for %d seconds waiting for hits lock\n", int(d.Seconds()))
logger.Printf("Warning: Blocked for %d seconds waiting for hits lock\n", int(d.Seconds()))
}
}
17 changes: 8 additions & 9 deletions iprate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
"fmt"
"net"
"sync"
"time"
Expand Down Expand Up @@ -178,14 +177,14 @@ func queueFanout() {
go func(ip net.IP) {
err := hook.Add(ip)
if err != nil {
fmt.Println("Error calling webhook on IP addition for %s: %s\n", ip.String(), err)
logger.Println("Error calling webhook on IP addition for %s: %s\n", ip.String(), err)
}
}(*msg.ipRate.ip)
} else if msg.operation == fastly.BatchOperationDelete && hook.RemoveIPsUri != "" {
go func(ip net.IP) {
err := hook.Remove(ip)
if err != nil {
fmt.Println("Error calling webhook on IP removal for %s: %s\n", ip.String(), err)
logger.Println("Error calling webhook on IP removal for %s: %s\n", ip.String(), err)
}
}(*msg.ipRate.ip)
}
Expand Down Expand Up @@ -260,7 +259,7 @@ func pushACLUpdates(service *fastly.Service, batch []*limitMessage) {

entries, _, err := client.ACLEntry.List(service.ID, acl.ID)
if err != nil {
fmt.Printf("Error fetching ACL Entry list for %s. Requeuing pending changes. Error: %s\n", service.Name, err)
logger.Printf("Error fetching ACL Entry list for %s. Requeuing pending changes. Error: %s\n", service.Name, err)
go requeueBatch(batch)
return
}
Expand All @@ -284,7 +283,7 @@ func pushACLUpdates(service *fastly.Service, batch []*limitMessage) {
if existingEntry == nil || deleting[ipr] {
continue
}
fmt.Printf("Unlimiting IP %s on service %s\n", ipr.ip.String(), service.Name)
logger.Printf("Unlimiting IP %s on service %s\n", ipr.ip.String(), service.Name)
deleting[ipr] = true
update.ID = existingEntry.ID
case fastly.BatchOperationCreate:
Expand Down Expand Up @@ -321,7 +320,7 @@ func pushACLUpdates(service *fastly.Service, batch []*limitMessage) {

limitDuration := ipr.list.LimitDuration.multiply(float64(ipr.Strikes))
ipr.LimitExpire = time.Now().Add(limitDuration.Duration)
fmt.Printf("Limiting IP %s for %d minutes on service %s\n", ipr.ip.String(), int(limitDuration.Minutes()), service.Name)
logger.Printf("Limiting IP %s for %d minutes on service %s\n", ipr.ip.String(), int(limitDuration.Minutes()), service.Name)
ipr.LastLimit = time.Now()
ipr.Expire = time.Now().Add(time.Duration(24) * time.Hour)
}
Expand All @@ -330,7 +329,7 @@ func pushACLUpdates(service *fastly.Service, batch []*limitMessage) {
comment, err := json.Marshal(ipr)
// This will probably never happen
if err != nil {
fmt.Println("Unable to prepare update for %s on %s: %s", ipr.ip.String(), service.Name, err)
logger.Println("Unable to prepare update for %s on %s: %s", ipr.ip.String(), service.Name, err)
continue
}
update.Comment = string(comment)
Expand All @@ -350,15 +349,15 @@ func pushACLUpdates(service *fastly.Service, batch []*limitMessage) {
}

if _, err := client.ACLEntry.BatchUpdate(service.ID, acl.ID, updates); err != nil {
fmt.Printf("Error updating ACL for %s. Requeuing pending changes. Error: %s\n", service.Name, err)
logger.Printf("Error updating ACL for %s. Requeuing pending changes. Error: %s\n", service.Name, err)
go requeueBatch(batch)
}

// If this fails, then a RemoveLimit() call might assume an entry is
// still in the ACL when it isn't. Not disasterous.
if err = ratesToUpdate.syncWithACLEntries(service); err != nil {
if err != nil {
fmt.Printf("Error syncing ip rates with ACL entries for service %s: %s\n", service.Name, err)
logger.Printf("Error syncing ip rates with ACL entries for service %s: %s\n", service.Name, err)
}
}

Expand Down
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
golog "log"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -30,6 +31,8 @@ const workers = 1

var hits = hitMap{m: make(map[string]*ipRate)}

var logger = golog.New(os.Stdout, "", 0)

func main() {
app := cli.NewApp()
app.Name = "fastly-ratelimit"
Expand Down Expand Up @@ -135,7 +138,7 @@ func readLogs(channel syslog.LogPartsChannel, serviceDomains ServiceDomains) {
continue
}
if time.Now().Sub(log.timestamp) > time.Duration(2)*time.Minute {
fmt.Printf("Warning: old log line. Log TS: %s, Current time: %s\n", log.timestamp.String(), time.Now().String())
logger.Printf("Warning: old log line. Log TS: %s, Current time: %s\n", log.timestamp.String(), time.Now().String())
}
var ipr *ipRate
var found bool
Expand All @@ -147,22 +150,22 @@ func readLogs(channel syslog.LogPartsChannel, serviceDomains ServiceDomains) {
hits.Unlock()
service, err := serviceDomains.getServiceByHost(log.host.Value)
if err != nil {
fmt.Printf("Error while finding fastly service for domain %s: %s\n.", log.host.Value, err)
logger.Printf("Error while finding fastly service for domain %s: %s\n.", log.host.Value, err)
}
if service == nil {
fmt.Printf("Found request for host %s which is not in fastly. Ignoring\n", log.host.Value)
logger.Printf("Found request for host %s which is not in fastly. Ignoring\n", log.host.Value)
continue
}
dimension := ipr.list.getDimension(log, service)
overLimit := ipr.Hit(log.timestamp, dimension)
if overLimit {
if err := ipr.Limit(service); err != nil {
fmt.Printf("Error limiting IP: %s\n", err)
logger.Printf("Error limiting IP: %s\n", err)
}
}

if len(channel) == syslogChannelBufferSize {
fmt.Println("Warning: log buffer full. We are dropping logs.")
logger.Println("Warning: log buffer full. We are dropping logs.")
}
}

Expand Down

0 comments on commit 063f5b2

Please sign in to comment.