Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Refactor agent/cache package #126

Merged
merged 2 commits into from
Mar 7, 2017

Conversation

vcabbage
Copy link
Member

  • Began converting over to use github.com/pkg/errors as discussed in Proposal: Error handling with pkg/errors #82.
  • Additional error handling added.
  • Replaced path formatting via fmt.Sprintf with filepath.Join, which handles cross-platform paths.
  • AgentCache only opens the database once at start instead of on every call. (sql.DB is a connection pool safe for concurrent use.)
  • AgentCache is initialized in cmd/todd-agent/main.go and injected into functions/objects which require it, namely Task.Run(). As a temporary measure, the setAgentCache() method was added to the comms package so todd-agent can inject the AgentCache. This is to avoid too many change in this patch and is removed in a later patch where task handling is largely extracted from comms.
  • Combined NewAgentCache() and AgentCache.Init() into New().
    • Usage appears as cache.New(cfg) instead of cache.NewAgentCache(), this is recommended when a package only exports one type. (As mentioned in Effective Go.)

      the function to make new instances of ring.Ring—which is the definition of a constructor in Go—would normally be called NewRing, but since Ring is the only type exported by the package, and since the package is called ring, it's called just New, which clients of the package see as ring.New.

    • The two steps were combined as there is no case of creating an AgentCache and not immediately initializing it.

  • SQL statements have been reformatted with SQL identifiers in upper case.
  • String formatting in SQL statements replaced with parameterized queries.
  • More efficient row counting in AgentCache.SetKeyValue().

@vcabbage vcabbage force-pushed the refactor-agent-cache branch 2 times, most recently from d484b84 to 5782c00 Compare December 26, 2016 22:21
@Mierdin
Copy link
Member

Mierdin commented Mar 6, 2017

As I've said in another issue, I apologize for the huge delay in looking at this. The day job has not only taken a lot of my attention, but also my ability to look at "not Python" 😄

I'll dive in and get this merged ASAP - as always, your contributions are 💯

@vcabbage
Copy link
Member Author

vcabbage commented Mar 6, 2017

No worries at all, I know how that goes.

I did a bunch of refactor branches back when I was still between jobs. They all build on one another so I was going to submit them one at a time. I can continue doing that or I can hold off if you prefer. I enjoy the refactoring process even if the changes are never merged, so I don't consider it wasted time or anything of that nature.

// NewAgentComms returns a comms instance configured for agent usages.
//
// TODO: accept an interface for cache instead of concrete type
func NewAgentComms(cfg config.Config, ac *cache.AgentCache) (*toddComms, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I noticed you changed the reference to NewToDDComms to point here instead (in https://github.com/toddproject/todd/pull/126/files#diff-8a0edf5a42325103a551ddd3ac9616c8R79) but left the old NewToDDComms function.

Is the intention to eventually retire NewToDDComms, but just didn't want to bloat this PR? If so, a comment indicating so would be appreciated

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly, I was trying to limit the scope of the changes to the agent/cache package as much as possible. At the time comms needed access to the agent cache, but only when being used by the agent, so I did the simple thing and added a special constructor just for the agent. The server and client still use NewToDDComms.

I didn't add a comment because I wasn't sure what was temporary and what wasn't. I did know that the Package.setAgentCache() method need to be removed eventually, so that did get a comment. As it turned out, when I got to the comms package in my refactor-comms branch, the agent cache dependency was decoupled from comms and I collapsed both constructors and renamed them to simply New().

Copy link
Member

@Mierdin Mierdin left a comment

Choose a reason for hiding this comment

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

Thanks so much for the refactoring efforts dude. Watching you remove all of those initializations and setup calls since you're doing them once at the beginning was like watching a hot knife go through butter. Fantastic

Just one inline comment, dependent on your plans for some of the changes. Other than that LGTM

@Mierdin Mierdin merged commit 75f4f92 into toddproject:master Mar 7, 2017
@vcabbage vcabbage deleted the refactor-agent-cache branch March 8, 2017 03:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants