Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package logdog | |
| 6 | |
| 7 import ( | |
| 8 "net/http" | |
| 9 "strings" | |
| 10 "sync" | |
| 11 | |
| 12 "github.com/julienschmidt/httprouter" | |
| 13 "github.com/luci/luci-go/appengine/cmd/milo/miloerror" | |
| 14 "github.com/luci/luci-go/appengine/cmd/milo/settings" | |
| 15 authClient "github.com/luci/luci-go/appengine/gaeauth/client" | |
| 16 "github.com/luci/luci-go/common/config" | |
| 17 log "github.com/luci/luci-go/common/logging" | |
| 18 "github.com/luci/luci-go/logdog/common/types" | |
| 19 "github.com/luci/luci-go/server/templates" | |
| 20 | |
| 21 "golang.org/x/net/context" | |
| 22 ) | |
| 23 | |
| 24 // AnnotationStream is a ThemedHandler that renders a LogDog Milo annotation | |
| 25 // protobuf stream. | |
| 26 // | |
| 27 // The protobuf stream is fetched live from LogDog and cached locally, either | |
| 28 // temporarily (if incomplete) or indefinitely (if complete). | |
| 29 type AnnotationStream struct { | |
| 30 // logDogClient is a reusable HTTP client to use for LogDog. | |
| 31 logDogClient *http.Client | |
| 32 | |
| 33 initMu sync.Mutex | |
| 34 } | |
| 35 | |
| 36 // init initializes the process-global state of the AnnotationStream. | |
| 37 func (s *AnnotationStream) init(c context.Context) *miloerror.Error { | |
| 38 s.initMu.Lock() | |
|
nodir
2016/07/29 23:00:32
insert a s.logdogClient!=nil check with read-lock
dnj
2016/07/29 23:24:43
Done.
| |
| 39 defer s.initMu.Unlock() | |
| 40 | |
| 41 if s.logDogClient != nil { | |
| 42 return nil | |
| 43 } | |
| 44 | |
| 45 // Initialize the LogDog client. | |
| 46 a, err := authClient.Transport(c, nil, nil) | |
| 47 if err != nil { | |
| 48 log.WithError(err).Errorf(c, "Failed to get transport for LogDog server.") | |
| 49 return &miloerror.Error{ | |
| 50 Code: http.StatusInternalServerError, | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 s.logDogClient = &http.Client{ | |
| 55 Transport: a, | |
| 56 } | |
| 57 return nil | |
| 58 } | |
| 59 | |
| 60 // GetTemplateName implements settings.ThemedHandler. | |
| 61 func (s *AnnotationStream) GetTemplateName(t settings.Theme) string { | |
| 62 return "build.html" | |
| 63 } | |
| 64 | |
| 65 // Render implements settings.ThemedHandler. | |
| 66 func (s *AnnotationStream) Render(c context.Context, req *http.Request, p httpro uter.Params) (*templates.Args, error) { | |
| 67 if err := s.init(c); err != nil { | |
| 68 return nil, err | |
| 69 } | |
| 70 | |
| 71 as := annotationStreamRequest{ | |
| 72 AnnotationStream: s, | |
| 73 | |
| 74 project: config.ProjectName(p.ByName("project")), | |
| 75 path: types.StreamPath(strings.Trim(p.ByName("path"), "/")), | |
| 76 host: req.FormValue("host"), | |
| 77 } | |
| 78 if err := as.normalize(); err != nil { | |
| 79 return nil, err | |
| 80 } | |
| 81 | |
| 82 // Load the Milo annotation protobuf from the annotation stream. | |
| 83 if err := as.load(c); err != nil { | |
| 84 return nil, err | |
| 85 } | |
| 86 | |
| 87 // Convert the Milo Annotation protobuf to Milo objects. | |
| 88 return &templates.Args{ | |
| 89 "Build": as.toMiloBuild(c), | |
| 90 }, nil | |
| 91 } | |
| OLD | NEW |