用Go编写的程序打开许多与mongoDB的连接

时间:2016-08-10 16:53:58

标签: mongodb go

我有一个用Go编写的非常简单的HTP服务器,它通过API从mongodb实例提供我的AngularJS前端数据。

以下是代码:

// ReceiveData - used to handle incoming data
func ReceiveData(w http.ResponseWriter, r *http.Request) {
    if r.Method != "POST" {
        http.NotFound(w, r)
        return
    }

    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        panic(err)
    }

    // database
    session, err := mgo.Dial("localhost")
    if err != nil {
        panic(err)
    } else {
        fmt.Println("session created")
        database := session.DB("schedule_calculator")
        collection := database.C("schedule_save")
        num, err := collection.Count()
        if err == nil {
            fmt.Println("schedule_save collection count = ", num)
            mongodbData := SavedData{ID: bson.NewObjectId(), Data: string(body), Date: time.Now()}
            collection.Insert(mongodbData)
            num, _ := collection.Count()
            fmt.Println("new count: ", num)
        } else {
            fmt.Println("schedule_save error - ", err)
        }
    }

    if err := json.NewEncoder(w).Encode("todos"); err != nil {
        panic(err)
    }
}

type SavedData struct {
    ID   bson.ObjectId `bson:"_id"`
    Data string
    Date time.Time
}

// SendData - Called by UI to get saved data
func SendData(w http.ResponseWriter, r *http.Request) {
    fmt.Println("SendData function")
    session, err := mgo.Dial("localhost")
    defer closeSession(session)
    if err != nil {
        panic(err)
    } else {
        fmt.Println("session created")
        database := session.DB("schedule_calculator")
        collection := database.C("schedule_save")
        num, err := collection.Count()
        if err == nil {
            fmt.Println("schedule_save collection count = ", num)

            var myData SavedData
            dbSize, err2 := collection.Count()
            if err2 != nil {
                panic(err2)
            }

            if dbSize > 0 {
                // db not empty
                err2 = collection.Find(nil).Skip(dbSize - 1).One(&myData)
                if err2 != nil {
          // TODO: handle error
                    panic(err2)
                }

                // fmt.Println(myData.Data)

                w.Header().Set("Content-Type", "application/json; charset=UTF-8")
                w.WriteHeader(http.StatusOK)

                if err := json.NewEncoder(w).Encode(myData.Data); err != nil {
          // TODO: handle error
                    panic(err)
                }
            } else {
                // db empty
        fmt.Println("DB is empty")
            }

        } else {
            fmt.Println("schedule_save error - ", err)
        }
    }
}

// closes the mongodb session
// TODO: make it use only 1 session
func closeSession(session *mgo.Session) {
    session.Close()
    fmt.Println("session closed")
}

这是我在与API进行一些短暂交互后在控制台中得到的结果:

2016-08-10T19:22:59.734+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55401 #60 (6 connections now open)
2016-08-10T19:22:59.740+0300 I NETWORK  [conn60] end connection 127.0.0.1:55401 (5 connections now open)
2016-08-10T19:23:58.794+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55405 #61 (6 connections now open)
2016-08-10T19:23:58.800+0300 I NETWORK  [conn61] end connection 127.0.0.1:55405 (5 connections now open)
2016-08-10T19:24:24.219+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55411 #62 (6 connections now open)
2016-08-10T19:24:24.225+0300 I NETWORK  [conn62] end connection 127.0.0.1:55411 (5 connections now open)
2016-08-10T19:25:56.149+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55434 #63 (6 connections now open)
2016-08-10T19:25:56.155+0300 I NETWORK  [conn63] end connection 127.0.0.1:55434 (5 connections now open)
2016-08-10T19:33:54.127+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55460 #64 (6 connections now open)
2016-08-10T19:33:54.133+0300 I NETWORK  [conn64] end connection 127.0.0.1:55460 (5 connections now open)
2016-08-10T19:35:12.060+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55476 #65 (6 connections now open)
2016-08-10T19:35:12.066+0300 I NETWORK  [conn65] end connection 127.0.0.1:55476 (5 connections now open)
2016-08-10T19:35:22.827+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55477 #66 (6 connections now open)
2016-08-10T19:35:22.833+0300 I NETWORK  [conn66] end connection 127.0.0.1:55477 (5 connections now open)
2016-08-10T19:35:37.720+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55478 #67 (6 connections now open)
2016-08-10T19:35:52.725+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55487 #68 (7 connections now open)
2016-08-10T19:36:20.498+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55488 #69 (8 connections now open)
2016-08-10T19:36:20.508+0300 I NETWORK  [conn69] end connection 127.0.0.1:55488 (7 connections now open)
2016-08-10T19:36:33.100+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55490 #70 (8 connections now open)
2016-08-10T19:36:37.155+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55492 #71 (9 connections now open)
2016-08-10T19:36:48.105+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55493 #72 (10 connections now open)
2016-08-10T19:36:50.284+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55494 #73 (11 connections now open)
2016-08-10T19:36:52.157+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55495 #74 (12 connections now open)
2016-08-10T19:36:53.328+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55496 #75 (13 connections now open)
2016-08-10T19:37:01.375+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55497 #76 (14 connections now open)
2016-08-10T19:37:05.287+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55498 #77 (15 connections now open)
2016-08-10T19:37:05.827+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55499 #78 (16 connections now open)
2016-08-10T19:37:05.836+0300 I NETWORK  [conn78] end connection 127.0.0.1:55499 (15 connections now open)
2016-08-10T19:37:08.333+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55500 #79 (16 connections now open)
2016-08-10T19:37:16.376+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55521 #80 (17 connections now open)
2016-08-10T19:37:23.323+0300 W NETWORK  [HostnameCanonicalizationWorker] Failed to obtain name info for: [ (192.168.0.102, "nodename nor servname provided, or not known"), (192.168.0.102, "nodename nor servname provided, or not known") ]
2016-08-10T19:40:41.079+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:55546 #81 (18 connections now open)
2016-08-10T19:40:41.087+0300 I NETWORK  [conn81] end connection 127.0.0.1:55546 (17 connections now open)

我是Go的新手,所以这是我设法使其工作的最简单方法,但我真的想知道如何将打开的连接限制为1.

1 个答案:

答案 0 :(得分:0)

错过defer closeSession(session)

中的ReceiveData