Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store and use log offset #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/ohm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ func main() {

// Start the producer
go func() {
var err error
var retryCount int
var lineID string

// TODO move to constants
clampMaxRetrySleep := 60 * time.Second
resetAfter := 10 * time.Minute

lastCrash := time.Now()
for {
err := nextdns.StreamLogs(logC)
lineID, err = nextdns.StreamLogs(logC, lineID)
logStreamerRestarts.Inc()
if time.Since(lastCrash) > resetAfter {
retryCount = 0
Expand Down
36 changes: 25 additions & 11 deletions src/nextdns/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const (
)

var (
StreamingLogLineRegex = regexp.MustCompile(`^data:\s+`)
APIKey string
Profile string
StreamingLogLineIDRegex = regexp.MustCompile(`^id:\s+`)
StreamingLogLineRegex = regexp.MustCompile(`^data:\s+`)

APIKey string
Profile string
)

func MakeUrl(pathParts ...string) string {
Expand Down Expand Up @@ -50,30 +52,42 @@ func GetLogs() (*http.Response, error) {
return http.DefaultClient.Do(req)
}

func StreamLogs(logC chan types.LogData) error {
func StreamLogs(logC chan types.LogData, lastID string) (string, error) {
req, err := Get(MakeUrl("profiles", Profile, "logs", "stream"))
if lastID != "" {
log.Printf("streaming from id %s onwards", lastID)
q := req.URL.Query()
q.Add("id", lastID)
req.URL.RawQuery = q.Encode()
}

if err != nil {
return err
return lastID, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
return lastID, err
}

reader := bufio.NewReader(resp.Body)
var data []byte
// TODO cancel via context
for {
line, err := reader.ReadBytes('\n')
if err != nil {
return err
return lastID, err
}
prefix := StreamingLogLineRegex.FindSubmatchIndex(line)
// could be blank line or metadata
if len(prefix) == 0 {

if idPrefix := StreamingLogLineIDRegex.FindSubmatchIndex(line); len(idPrefix) > 0 {
lastID = strings.TrimSpace(string(line[idPrefix[1]:]))
continue
} else if prefix := StreamingLogLineRegex.FindSubmatchIndex(line); len(prefix) > 0 {
data = line[prefix[1]:]
} else {
// could be blank line or be other metadata
continue
}
data := line[prefix[1]:]

logData := types.LogData{}
err = json.Unmarshal(data, &logData)
Expand Down
2 changes: 1 addition & 1 deletion src/ohm/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Ready(key string, _, _, _ time.Duration, logC chan types.LogData) Handler {
}

func Monitoring(key string, allowance, cooldown, lockout time.Duration, logC chan types.LogData) Handler {
log.Printf("monitoring: %s (cooldown: %s, lockout: %s)", key, cooldown, lockout)
log.Printf("monitoring: %s (allowance: %s, cooldown: %s, lockout: %s)", key, allowance, cooldown, lockout)
end := time.Now().Add(allowance)

// if a cooldown timer is provided, use that
Expand Down
2 changes: 2 additions & 0 deletions src/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type Status string

const StatusBlocked Status = "blocked"

type LogEntryID string

type LogData struct {
Timestamp time.Time `json:"timestamp"`
Domain string `json:"domain"`
Expand Down