Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed May 7, 2024
1 parent 227df4c commit 4fea784
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 52 deletions.
4 changes: 2 additions & 2 deletions fixchain/chainfix/chainfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func processChains(file string, fl *fixchain.FixAndLog) {
}
defer func() {
if err := f.Close(); err != nil {
log.Fatalf("Can't close file: %v\n", err)
log.Fatalf("Can't close file: %v", err)
}
}()

Expand Down Expand Up @@ -90,7 +90,7 @@ func contentStore(baseDir string, subDir string, content []byte) {
}
defer func() {
if err := f.Close(); err != nil {
log.Fatalf("Can't close file: %v\n", err)
log.Fatalf("Can't close file: %v", err)
}
}()
if _, err := f.Write(content); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion fixchain/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (rt postTestRoundTripper) RoundTrip(request *http.Request) (*http.Response,
rt.t.Error(errStr)
return nil, errors.New(errStr)
}

if err != nil {
errStr := fmt.Sprintf("#%d: Could not read request body: %s", rt.testIndex, err.Error())
rt.t.Error(errStr)
Expand Down
9 changes: 5 additions & 4 deletions fixchain/url_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package fixchain
import (
"fmt"
"io"
"log"
"net/http"
"sync"
"sync/atomic"
"time"

"k8s.io/klog/v2"
)

type lockedCache struct {
Expand Down Expand Up @@ -68,9 +69,9 @@ func (u *urlCache) getURL(url string) ([]byte, error) {
atomic.AddUint32(&u.errors, 1)
return nil, err
}
defer func(){
defer func() {
if err := c.Body.Close(); err != nil {
log.Fatalf("Operation to close response body failed: %v\n", err)
klog.Errorf("Operation to close response body failed: %v", err)
}
}()
// TODO(katjoyce): Add caching of permanent errors.
Expand All @@ -95,7 +96,7 @@ func newURLCache(c *http.Client, logStats bool) *urlCache {
t := time.NewTicker(time.Second)
go func() {
for range t.C {
log.Printf("url cache: %d hits, %d misses, %d errors, "+
klog.Infof("url cache: %d hits, %d misses, %d errors, "+
"%d bad status, %d read fail, %d cached", u.hit,
u.miss, u.errors, u.badStatus, u.readFail,
len(u.cache.m))
Expand Down
6 changes: 3 additions & 3 deletions internal/witness/client/http/witness_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"

wit_api "github.com/google/certificate-transparency-go/internal/witness/api"
"k8s.io/klog/v2"
)

// ErrSTHTooOld is returned if the STH passed to Update needs to be updated.
Expand All @@ -54,7 +54,7 @@ func (w Witness) GetLatestSTH(ctx context.Context, logID string) ([]byte, error)
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Fatalf("Failed to close response body: %v\n", err)
klog.Errorf("Failed to close response body: %v", err)
}
}()
if resp.StatusCode == 404 {
Expand Down Expand Up @@ -94,7 +94,7 @@ func (w Witness) Update(ctx context.Context, logID string, sth []byte, proof [][
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Fatalf("Failed to close response body: %v\n", err)
klog.Errorf("Failed to close response body: %v", err)
}
}()
body, err := io.ReadAll(resp.Body)
Expand Down
2 changes: 1 addition & 1 deletion internal/witness/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func readHTTP(u *url.URL) ([]byte, error) {
}
defer func() {
if err := resp.Body.Close(); err != nil {
klog.Exitf("failed to close response body %v\n", err)
klog.Errorf("failed to close response body %v", err)
}
}()
return io.ReadAll(resp.Body)
Expand Down
3 changes: 1 addition & 2 deletions internal/witness/cmd/feeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -261,7 +260,7 @@ func readHTTP(u *url.URL) ([]byte, error) {
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Fatalf("Failed to close response body: %v\n", err)
klog.Errorf("Failed to close response body: %v", err)
}
}()
return io.ReadAll(resp.Body)
Expand Down
3 changes: 1 addition & 2 deletions internal/witness/cmd/witness/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -87,7 +86,7 @@ func readHTTP(u *url.URL) ([]byte, error) {
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Fatalf("Operation to close response body failed: %v\n", err)
klog.Errorf("Operation to close response body failed: %v", err)
}
}()
return io.ReadAll(resp.Body)
Expand Down
5 changes: 2 additions & 3 deletions internal/witness/cmd/witness/internal/witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"encoding/pem"
"errors"
"fmt"
"log"
"reflect"

ct "github.com/google/certificate-transparency-go"
Expand Down Expand Up @@ -120,9 +119,9 @@ func (w *Witness) GetLogs() ([]string, error) {
if err != nil {
return nil, err
}
defer func(){
defer func() {
if err := rows.Close(); err != nil {
log.Fatalf("Operation to close rows failed: %v\n", err)
klog.Errorf("Operation to close rows failed: %v", err)
}
}()
var logs []string
Expand Down
8 changes: 4 additions & 4 deletions jsonclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ func MockServer(t *testing.T, failCount int, retryAfter int) *httptest.Server {
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&s)
if err != nil {
panic("Failed to decode: " + err.Error())
t.Fatalf("Failed to decode: " + err.Error())
}
defer func() {
if err := r.Body.Close(); err != nil {
panic("Failed to close request body: " + err.Error())
t.Fatalf("Failed to close request body: " + err.Error())
}
}()
}
Expand All @@ -162,11 +162,11 @@ func MockServer(t *testing.T, failCount int, retryAfter int) *httptest.Server {
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&params)
if err != nil {
panic("Failed to decode: " + err.Error())
t.Fatalf("Failed to decode: " + err.Error())
}
defer func() {
if err := r.Body.Close(); err != nil {
panic("Failed to close request body: " + err.Error())
t.Fatalf("Failed to close request body: " + err.Error())
}
}()
}
Expand Down
4 changes: 2 additions & 2 deletions submission/loglist_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestFirstRefresh(t *testing.T) {
}
defer func() {
if err := os.Remove(f); err != nil {
t.Fatalf("Operation to remove temp file failed: %v\n", err)
t.Fatalf("Operation to remove temp file failed: %v", err)
}
}()

Expand All @@ -81,7 +81,7 @@ func TestSecondRefresh(t *testing.T) {
}
defer func() {
if err := os.Remove(f); err != nil {
t.Fatalf("Operation to remove temp file failed: %v\n", err)
t.Fatalf("Operation to remove temp file failed: %v", err)
}
}()

Expand Down
13 changes: 5 additions & 8 deletions submission/loglist_refresher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ func createTempFile(data string) (string, error) {
if err != nil {
return "", err
}
defer func(){
defer func() {
if err := f.Close(); err != nil {
log.Fatalf("Operation to close file failed: %v\n", err)
log.Fatalf("Operation to close file failed: %v", err)
}
}()
if _, err := f.WriteString(data); err != nil {
return "", err
}
// if err := f.Close(); err != nil {
// return "", err
// }
return f.Name(), nil
}

Expand All @@ -61,7 +58,7 @@ func ExampleLogListRefresher() {
}
defer func() {
if err := os.Remove(f); err != nil {
log.Fatalf("Operation to remove temp file failed: %v\n", err)
log.Fatalf("Operation to remove temp file failed: %v", err)
}
}()

Expand Down Expand Up @@ -148,7 +145,7 @@ func TestNewLogListRefresher(t *testing.T) {
}
defer func() {
if err := os.Remove(f); err != nil {
log.Fatalf("Operation to remove temp file failed: %v\n", err)
t.Fatalf("Operation to remove temp file failed: %v", err)
}
}()

Expand Down Expand Up @@ -218,7 +215,7 @@ func TestNewLogListRefresherUpdate(t *testing.T) {
}
defer func() {
if err := os.Remove(f); err != nil {
log.Fatalf("Operation to remove temp file failed: %v\n", err)
t.Fatalf("Operation to remove temp file failed: %v", err)
}
}()

Expand Down
2 changes: 1 addition & 1 deletion submission/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestProxyInitState(t *testing.T) {
}
defer func() {
if err := os.Remove(f); err != nil {
t.Fatalf("Operation to remove temp file failed: %v\n", err)
t.Fatalf("Operation to remove temp file failed: %v", err)
}
}()

Expand Down
20 changes: 10 additions & 10 deletions trillian/ctfe/ct_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ func main() {
}

if err != nil {
klog.Errorf("Failed to read config: %v", err)
klog.Exitf("Failed to read config: %v", err)
}

beMap, err := ctfe.ValidateLogMultiConfig(cfg)
if err != nil {
klog.Errorf("Invalid config: %v", err)
klog.Exitf("Invalid config: %v", err)
}

klog.CopyStandardLogTo("WARNING")
Expand All @@ -134,16 +134,16 @@ func main() {
cfg := clientv3.Config{Endpoints: strings.Split(*etcdServers, ","), DialTimeout: 5 * time.Second}
client, err := clientv3.New(cfg)
if err != nil {
klog.Errorf("Failed to connect to etcd at %v: %v", *etcdServers, err)
klog.Exitf("Failed to connect to etcd at %v: %v", *etcdServers, err)
}

httpManager, err := endpoints.NewManager(client, *etcdHTTPService)
if err != nil {
klog.Errorf("Failed to create etcd http manager: %v", err)
klog.Exitf("Failed to create etcd http manager: %v", err)
}
metricsManager, err := endpoints.NewManager(client, *etcdMetricsService)
if err != nil {
klog.Errorf("Failed to create etcd metrics manager: %v", err)
klog.Exitf("Failed to create etcd metrics manager: %v", err)
}

etcdHTTPKey := fmt.Sprintf("%s/%s", *etcdHTTPService, *httpEndpoint)
Expand Down Expand Up @@ -197,13 +197,13 @@ func main() {
}
conn, err := grpc.Dial(be.BackendSpec, dialOpts...)
if err != nil {
klog.Errorf("Could not dial RPC server: %v: %v", be, err)
klog.Exitf("Could not dial RPC server: %v: %v", be, err)
}
defer func(){
defer func() {
if err := conn.Close(); err != nil {
klog.Errorf("Could not close RPC connection: %v", err)
}
}()
}()
clientMap[be.Name] = trillian.NewTrillianLogClient(conn)
}

Expand All @@ -220,7 +220,7 @@ func main() {
for _, c := range cfg.LogConfigs.Config {
inst, err := setupAndRegister(ctx, clientMap[c.LogBackendName], *rpcDeadline, c, corsMux, *handlerPrefix, *maskInternalErrors)
if err != nil {
klog.Errorf("Failed to set up log instance for %+v: %v", cfg, err)
klog.Exitf("Failed to set up log instance for %+v: %v", cfg, err)
}
if *getSTHInterval > 0 {
go inst.RunUpdateSTH(ctx, *getSTHInterval)
Expand Down Expand Up @@ -285,7 +285,7 @@ func main() {
if *tracing {
handler, err = opencensus.EnableHTTPServerTracing(*tracingProjectID, *tracingPercent)
if err != nil {
klog.Errorf("Failed to initialize stackdriver / opencensus tracing: %v", err)
klog.Exitf("Failed to initialize stackdriver / opencensus tracing: %v", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion trillian/integration/ct_hammer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func main() {
klog.Exitf("Failed to print banner!")
}
if err := r.Close(); err != nil {
klog.Exitf("Failed to close reader %v\n", err)
klog.Exitf("Failed to close reader %v", err)
}
fmt.Print("\n\nHammer Time\n\n")
}
Expand Down
7 changes: 3 additions & 4 deletions trillian/integration/ct_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"crypto/sha256"
"encoding/pem"
"fmt"
"log"
"math/rand"
"net"
"net/http"
Expand Down Expand Up @@ -914,7 +913,7 @@ func (ls *logStats) fromServer(ctx context.Context, servers string) (*logStats,
}
defer func() {
if err := httpRsp.Body.Close(); err != nil {
log.Fatalf("Operation to close http response body failed: %v\n", err)
fmt.Printf("Operation to close http response body failed: %v\n", err)
}
}()

Expand Down Expand Up @@ -988,9 +987,9 @@ func setTreeState(ctx context.Context, adminServer string, logID int64, state tr
if err != nil {
return err
}
defer func(){
defer func() {
if err := conn.Close(); err != nil {
log.Fatalf("Operation to close RPC connection failed: %v\n", err)
fmt.Printf("Operation to close RPC connection failed: %v\n", err)
}
}()

Expand Down
4 changes: 2 additions & 2 deletions trillian/integration/hammer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ func (s *fakeCTServer) addChain(w http.ResponseWriter, req *http.Request) {
func (s *fakeCTServer) close() {
if s.server != nil {
if err := s.server.Close(); err != nil {
klog.Errorf("Operation to close server failed: %v\n", err)
klog.Errorf("Operation to close server failed: %v", err)
}
}
if s.lis != nil {
if err := s.lis.Close(); err != nil {
klog.Errorf("Operation to close server listener failed: %v\n", err)
klog.Errorf("Operation to close server listener failed: %v", err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion trillian/integration/logenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func NewCTLogEnv(ctx context.Context, cfgs []*configpb.LogConfig, numSequencers
// Close shuts down the servers.
func (env *CTLogEnv) Close() {
if err := env.ctListener.Close(); err != nil {
log.Fatalf("Operation to close listener failed: %v\n", err)
log.Fatalf("Operation to close listener failed: %v", err)
}
env.wg.Wait()
env.logEnv.Close()
Expand Down
2 changes: 1 addition & 1 deletion trillian/migrillian/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {

defer func() {
if err := conn.Close(); err != nil {
klog.Exitf("Could not close RPC connection: %v\n", err)
klog.Errorf("Could not close RPC connection: %v", err)
}
}()

Expand Down

0 comments on commit 4fea784

Please sign in to comment.