dongshao1981 2016-08-10 16:53
浏览 10

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

I have a very simple HTP server written in Go which serves my AngularJS front end data from a mongodb instance through an API.

Here is the code:

// 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")
}

and this is what I get in the console after some short interaction with the 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)

I am very new to Go, so this was the simplest way I managed to make it work however I would really like to know how to limit the opened connections to 1.

  • 写回答

1条回答 默认 最新

  • dongtiao0657 2016-08-11 12:25
    关注

    Missed defer closeSession(session) in ReceiveData

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line