Skip to content

Commit

Permalink
maintner: simplify TailNetworkMutationSource API
Browse files Browse the repository at this point in the history
Document the property that MutationStreamEvent.Mutation is non-nil if
MutationStreamEvent.Err is nil. This is consistent with documentation
inside the MutationStreamEvent struct, and simplifies API for callers.
They can use the Mutation field directly after checking the Err field.

Use a generic temporary directory name in TailNetworkMutationSource,
since it can be used by anything, not just the maintwatch command.

Use the new godata.Server constant in maintwatch.

Change-Id: I39dd6918b5f21173a8f47a7359f489cab511295e
Reviewed-on: https://go-review.googlesource.com/c/build/+/221637
Reviewed-by: Alexander Rakoczy <[email protected]>
  • Loading branch information
dmitshur committed Mar 2, 2020
1 parent d927ce7 commit bb8466f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions maintner/maintner.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ type MutationSource interface {
}

// MutationStreamEvent represents one of three possible events while
// reading mutations from disk. An event is either a mutation, an
// error, or reaching the current end of the log. Only one of the
// fields will be non-zero.
// reading mutations from disk or another source.
// An event is either a mutation, an error, or reaching the current
// end of the log. Exactly one of the three fields will be non-zero.
type MutationStreamEvent struct {
Mutation *maintpb.Mutation

Expand Down
14 changes: 7 additions & 7 deletions maintner/maintwatch/maintwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ import (

"github.com/golang/protobuf/proto"
"golang.org/x/build/maintner"
"golang.org/x/build/maintner/godata"
)

var server = flag.String("server", "https://maintner.golang.org/logs", "maintner server's /logs URL")
var server = flag.String("server", godata.Server, "maintner server's /logs URL")

func main() {
flag.Parse()

tm := proto.TextMarshaler{Compact: false}
for {
err := maintner.TailNetworkMutationSource(context.Background(), *server, func(e maintner.MutationStreamEvent) error {
if e.Err != nil {
log.Printf("# ignoring err: %v\n", e.Err)
time.Sleep(5 * time.Second)
return nil
}
if e.Mutation != nil {
fmt.Println()
tm := proto.TextMarshaler{Compact: false}
tm.Marshal(os.Stdout, e.Mutation)
}
fmt.Println()
tm.Marshal(os.Stdout, e.Mutation)
return nil
})
log.Printf("tail error: %v; restarting", err)
log.Printf("tail error: %v; restarting\n", err)
time.Sleep(time.Second)
}
}
4 changes: 3 additions & 1 deletion maintner/netsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ func NewNetworkMutationSource(server, cacheDir string) MutationSource {
}

// TailNetworkMutationSource calls fn for all new mutations added to the log on server.
// Events with the End field set to true are not sent, so all events will
// have exactly one of Mutation or Err fields set to a non-zero value.
// It ignores prior events.
// If the server is restarted and its history diverges,
// TailNetworkMutationSource may return duplicate events. This therefore does not
// return a MutationSource, so it can't be accidentally misused for important things.
// TailNetworkMutationSource returns if fn returns an error, or if ctx expires.
func TailNetworkMutationSource(ctx context.Context, server string, fn func(MutationStreamEvent) error) error {
td, err := ioutil.TempDir("", "maintnerwatch")
td, err := ioutil.TempDir("", "maintnertail")
if err != nil {
return err
}
Expand Down

0 comments on commit bb8466f

Please sign in to comment.