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

Using JSON line format for QueryCapture #193

Merged
merged 4 commits into from
Jul 5, 2018
Merged

Using JSON line format for QueryCapture #193

merged 4 commits into from
Jul 5, 2018

Conversation

vixentael
Copy link
Collaborator

@vixentael vixentael commented Jul 5, 2018

As we discussed previously having large JSON file as Query blob might be not the best option [T605]

  • format changed from large json to multiline json
  • on each parsed query remember query in the buffer
  • dump buffer into file once a while (instead of writing into the file on each query)

return nil, err
}
// read existing queries from file
handler.ReadAllQueriesFromFile()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle error and close file if err != nil

log.WithError(ErrUnexpectedCaptureChannelClose).WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSecurityError)
}

// TODO: how to remove duplicate code?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Golang doesn't provide way to write two cases at once like in switch with fallthrough becase any read from channel block goroutine and for fallthrough it need to read second channel :) So we can hack it like:

  1. move logic to some function
func (handler *QueryCaptureHandler) finish(){
  handler.serializationTicker.Stop()
  err := handler.DumpAllQueriesToFile()
  if err != nil {
      log.WithError(ErrComplexSerializationError).WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSecurityError)
  }
  openedFile.Close()
}

and call it in two cases

case <-someCh:
....
   handler.finish()
   return
....
  1. use one more channel that will flag that we need to finish
exitCh := make(chan bool)
for {
  select {
     case <-signalBackgroundExit:
       exitCh <- true
     case <-signalShutdown:
       exitCh <- true
  }
  select {
    case <-exitCh:
       // exit code
    default:
      // do nothing, continue
  }
}

default: //channel is full
log.Errorf("Can't process too many queries. Skip query: %s", trimToN(query, LogQueryLength))
}
handler.BufferedQueries = append(handler.BufferedQueries, *queryInfo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to append pointers instead copy of struct. change type of BufferedQueries from QueryInfo to *QueryInfo and then append only pointers.

func (handler *QueryCaptureHandler) Serialize() error {
jsonFile, err := json.Marshal(handler.Queries)
func (handler *QueryCaptureHandler) DumpAllQueriesToFile() error {
// open or create file, NO APPEND
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about to truncate file and add O_TRUNC flag too?


// clean buffered queries
handler.BufferedQueries = nil
err := AppendQueries(buf, openedFile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just return AppendQueries(buf, openedFile)
or return err below without if

return nil, err
}
if len(jsonQueryInfo) > 0 {
jsonQueryInfo = append(jsonQueryInfo, "\n"...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change to append(jsonQueryInfo, '\n') (single quotes are one character literal, double quotes are string literal that iterable)? after `"\n"... I think about "is it only 1 character or it 2 characters and therefore we use ... ?" )

@vixentael
Copy link
Collaborator Author

@Lagovas thank you for your comments

take a look!

@Lagovas Lagovas merged commit 25f19e0 into master Jul 5, 2018
@vixentael vixentael deleted the vxtl/censor branch July 6, 2018 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants